1 / 17

Functions

Functions. Please use speaker notes for additional information!. <html> <head><title>Converter</title></head> <body> <h1>Converter: Inches and Centimeters</h1> Using this converter you can convert centimeters into inches and inches into centimeters.<br>

milt
Download Presentation

Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions Please use speaker notes for additional information!

  2. <html> <head><title>Converter</title></head> <body> <h1>Converter: Inches and Centimeters</h1> Using this converter you can convert centimeters into inches and inches into centimeters.<br> The formula I am using to convert centimeters to inches is multiply the cm by .39.<br> The formula I am using to convert inches to centimeters is multiply in by 2.54.<br> <form action="http://www.pgrocer.com/cgi-bin/func/cmin.cgi" method=post> Enter the type of conversion:<br><br> <input type=radio name=type value="cm">Centimeters to Inches<br> <input type=radio name=type value="in">Inches to Centimeters<br> Enter the number to convert: <input type=text name=num size=4> <input type=submit value="Convert"> <input type=reset value="Reset"> </form> </body> </html>

  3. #!/usr/bin/perl print "Content-type: text/html\n\n"; use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard); use strict; my ($num, $type, $ans); $num = param('num'); $type = param('type'); print "<html>\n"; print "<head><title>Convert</title><basefont size=4></head>\n"; print "<h1>Convert</h1>\n"; if ($type eq "cm") { toinches(); } elsif ($type eq "in") { tocm(); } else { print "No selection was made - try again!<br><br>\n"; } print "<a href='http://www.pgrocer.com/func/cmin.html'>Click here to return to converter</a>\n"; print "</body></html>\n"; exit; # ***** user defined functions **** Depending on the result of the if comparison, different functions are called. The functions are shown on the next slide. The exit; statement is used to terminate the code. It is not required in Perl. The user defined functions are shown on the next slide.

  4. sub toinches { $ans = ($num * .39); print "$num centimeters is equal to $ans inches<br><br>\n"; } sub tocm { $ans = ($num * 2.54); print "$num inches is equal to $ans centimeters<br><br>\n"; }

  5. # ***** user defined functions **** sub toinches { $ans = ($num * .39); $from = "centimeters"; $to = "inches"; return $ans, $from, $to; } sub tocm { $ans = ($num * 2.54); $from = "inches"; $to = "centimeters"; return $ans, $from, $to; } #!/usr/bin/perl print "Content-type: text/html\n\n"; use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard); use strict; my ($num, $type, $ans, $from, $to); $num = param('num'); $type = param('type'); print "<html>\n"; print "<head><title>Convert</title><basefont size=4></head>\n"; print "<h1>Convert</h1>\n"; if ($type eq "cm") { toinches(); } elsif ($type eq "in") { tocm(); } else { print "No selection was made - try again!<br><br>\n"; } print "$num $from is equal to $ans $to<br><br>\n"; print "<a href='http://www.pgrocer.com/func/cmin1.html'>Click here to return to converter</a>\n"; print "</body></html>\n"; exit; Continued on the top of the next column.

  6. sub toinches { my ($ans, $from, $to); $ans = ($num * .39); $from = "centimeters"; $to = "inches"; return $ans, $from, $to; } sub tocm { my ($ans, $from, $to); $ans = ($num * 2.54); $from = "inches"; $to = "centimeters"; } #!/usr/bin/perl print "Content-type: text/html\n\n"; use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard); use strict; my ($num, $type); $num = param('num'); $type = param('type'); print "<html>\n"; print "<head><title>Convert</title><basefont size=4></head>\n"; print "<h1>Convert</h1>\n"; if ($type eq "cm") { toinches(); } elsif ($type eq "in") { tocm(); } else { print "No selection was made - try again!<br><br>\n"; } print "$num $from is equal to $ans $to<br><br>\n"; print "<a href='http://www.pgrocer.com/func/cmin.html'>Click here to return to converter</a>\n"; print "</body></html>\n"; exit; # ***** user defined functions **** • Software error: • Global symbol "$from" requires explicit package name at cmin2.cgi line 24. • Global symbol "$ans" requires explicit package name at cmin2.cgi line 24. • Global symbol "$to" requires explicit package name at cmin2.cgi line 24. • Execution of cmin2.cgi aborted due to compilation errors. Continued on the top of the next column.

  7. payroll.txt: Susan Ash, 12 Monument Court, Hingham, MA, 02043, S, 1, , 50000 Jonathan English, 6 Madison Rd, Hingham, MA, 02043, S, 2, , 45000 Jennifer Ames, 62 Ravens Place, Braintree, MA, 02184, S, 3, , 60000 Stephen Daniels, 786 Meyer St, Hingham, MA, 02043, S, 1, , 60000 David Souza, 54 High St, Braintree, MA, 02184, F, 4, 45, Ann Rogers, 5123 Main St, Fall River, MA, 02720, P, 3, 25, Cynthia Adams, 60 Elm St, Fall River, MA, 02720, F, 1, 35, Eric Wong, 7 Bailey Circle, Fall River, MA, 02720, S, 3, , 45000 Charles Fredericks, 45 Main St, Hingham, MA, 02043, P, 3, 75, Sarah Grant, 498 East St, Hingham, MA, 02043, F, 3, 60, Carl Henry, 48 Tremont St, Braintree, MA, 02184, P, 2, 75, Jill Elliot, 23 Walnut St, Braintree, MA, 02184, F, 3, 22,

  8. This is the first record in payroll.txt that I am going to use for this example: Susan Ash, 12 Monument Court, Hingham, MA, 02043, S, 1, , 50000 This is the output:The id is: sus ma 1 1 1ng #!/usr/bin/perl #stringfunc.cgi - reads the payroll file and processes with functions use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($id, $name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary); open(INF, "<", "payroll.txt") or die "file error: payroll.txt. $!, stopped"; while(<INF>) { chomp($_); ($name,$stadr,$city,$state,$zip,$jobtype,$jobcode,$payperhr,$salary) = split(/,/,$_); createid(); print "The id is: $id <br> The name is: $name<br>\n"; } close(INF); print "</body></html>\n"; exit; #********user defined functions**************** sub createid { my($namefst3, $statelc, $citymid,$jc3); $namefst3 = lcfirst(substr($name,0,3)); $statelc = lc($state); $citymid = lc(substr($city,3,2)); $jc3 = $jobcode x 3; $id = $namefst3.$statelc.$jc3.$citymid; return $id; } This code: substr($name,0,3) starts with character 0 and takes 3 characters in $name giving Sus. It is embedded in lcfirst which converts the first character to lower case which gives sus This converts $state to lower case giving ma This takes the substr starting with the third character and taking 2. It is nested with the lc function which converts to lower case giving ng This returns $jobcode 3 times which gives 1 1 1

  9. payroll1.txt: Susan Ash,12 Monument Court,Hingham,MA,02043,S,1,,50000 Jonathan English,6 Madison Rd,Hingham,MA,02043,S,2,,45000 Jennifer Ames,62 Ravens Place,Braintree,MA,02184,S,3,,60000 Stephen Daniels,786 Meyer St,Hingham,MA,02043,S,1,,60000 David Souza,54 High St,Braintree,MA,02184,F,4,45, Ann Rogers,5123 Main St,Fall River,MA,02720,P,3,25, Cynthia Adams,60 Elm St,Fall River,MA,02720,F,1,35, Eric Wong,7 Bailey Circle,Fall River,MA,02720,S,3,,45000 Charles Fredericks,45 Main St,Hingham,MA,02043,P,3,75, Sarah Grant,498 East St,Hingham,MA,02043,F,3,60, Carl Henry,48 Tremont St,Braintree,MA,02184,P,2,75, Jill Elliot,23 Walnut St,Braintree,MA,02184,F,3,22,

  10. #!/usr/bin/perl #stringfunc.cgi - reads the payroll file and processes with functions use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($id, $name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary); open(INF, "<", "payroll1.txt") or die "file error: payroll1.txt. $!, stopped"; while(<INF>) { chomp($_); ($name,$stadr,$city,$state,$zip,$jobtype,$jobcode,$payperhr,$salary) = split(/,/,$_); createid(); print "The id is: $id <br> The name is: $name<br>\n"; } close(INF); print "</body></html>\n"; exit; #********user defined functions**************** sub createid { my($namefst3, $statelc, $citymid,$jc3); $namefst3 = lcfirst(substr($name,0,3)); $statelc = lc($state); $citymid = lc(substr($city,3,2)); $jc3 = $jobcode x 3; $id = $namefst3.$statelc.$jc3.$citymid; return $id; }

  11. #!/usr/bin/perl #stringfuncmore.cgi - reads the payroll file and processes with functions use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($wherea, $name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary); open(INF, "<", "payroll1.txt") or die "file error: payroll1.txt. $!, stopped"; while(<INF>) { chomp($_); ($name,$stadr,$city,$state,$zip,$jobtype,$jobcode,$payperhr,$salary) = split(/,/,$_); finda(); print "The city is: $city The a is located at position: $wherea<br>\n"; } close(INF); print "</body></html>\n"; exit; #********user defined functions**************** sub finda { $wherea = index($city,"a"); return $wherea; } I am using index to check $city and tell me the location of the first a. Note that the answer will use a base 0.

  12. The F of Fall River is in the 0th position and the a is in the 1st postion. The B of Braintree is in the 0th position, the r is in the 1st position and the a is in the second position.

  13. #!/usr/bin/perl #stringfuncmore.cgi - reads the payroll file and processes with functions use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($sec,$min,$hour,$mday,$mond,$year,$wday,$yday,$isdst,$myhour,$mymond, $myyear); print "<html><head><title>Date</title></head>\n"; print "<body>\n"; print "<h1>Bristol Community College</h1>\n"; checkdate(); print "</body></html>\n"; exit; #********user defined functions**************** Note that I did all the right things here - starting with html etc, you can get away with out doing this as I did on some of the previous examples. This program is going to perform the checkdate() user defined function which is on the next slide.

  14. sub checkdate { ($sec,$min,$hour,$mday,$mond,$year,$wday,$yday,$isdst) = localtime(time); $myhour = $hour; $mymond = $mond +1; $myyear = $year + 1900; print "<h2>The date is: $mymond/$mday/$myyear</h2>\n"; print "<font size=4>"; if ($myhour >=8 and $myhour < 16) { print "Day school schedule\n"; } else { if ($myhour >=16 and $myhour < 22) { print "Evening schedule\n"; } else { print "The campus is closed\n"; } } } print "</font";

More Related