Functions Please use speaker notes for additional information!

17
Functions Please use speaker notes for additional information!

Transcript of Functions Please use speaker notes for additional information!

Page 1: Functions Please use speaker notes for additional information!

Functions

Please use speaker notes for additional information!

Page 2: 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>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>

Page 3: Functions Please use speaker notes for additional information!

#!/usr/bin/perlprint "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 ****

The user defined functions are shown on the next slide.

The exit; statement is used to terminate the code. It is not required in Perl.

Depending on the result of the if comparison, different functions are called. The functions are shown on the next slide.

Page 4: Functions Please use speaker notes for additional information!

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";

}

Page 5: Functions Please use speaker notes for additional information!

#!/usr/bin/perlprint "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;

# ***** 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; }

Continued on the top of the next column.

Page 6: Functions Please use speaker notes for additional information!

#!/usr/bin/perlprint "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 ****

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"; }

Continued on the top of the next column.

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.

Page 7: Functions Please use speaker notes for additional information!

payroll.txt:

Susan Ash, 12 Monument Court, Hingham, MA, 02043, S, 1, , 50000Jonathan English, 6 Madison Rd, Hingham, MA, 02043, S, 2, , 45000Jennifer Ames, 62 Ravens Place, Braintree, MA, 02184, S, 3, , 60000Stephen Daniels, 786 Meyer St, Hingham, MA, 02043, S, 1, , 60000David 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, , 45000Charles 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,

Page 8: Functions Please use speaker notes for additional information!

#!/usr/bin/perl#stringfunc.cgi - reads the payroll file and processes with functionsuse CGI::Carp qw(fatalsToBrowser);print "Content-type: text/html\n\n";use CGI qw(:standard);use strict;#declare variablesmy ($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 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, , 50000This is the output: The id is: sus ma 1 1 1ng

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

Page 9: Functions Please use speaker notes for additional information!
Page 10: Functions Please use speaker notes for additional information!

payroll1.txt:

Susan Ash,12 Monument Court,Hingham,MA,02043,S,1,,50000Jonathan English,6 Madison Rd,Hingham,MA,02043,S,2,,45000Jennifer Ames,62 Ravens Place,Braintree,MA,02184,S,3,,60000Stephen Daniels,786 Meyer St,Hingham,MA,02043,S,1,,60000David 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,,45000Charles 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,

Page 11: Functions Please use speaker notes for additional information!

#!/usr/bin/perl#stringfunc.cgi - reads the payroll file and processes with functionsuse CGI::Carp qw(fatalsToBrowser);print "Content-type: text/html\n\n";use CGI qw(:standard);use strict;#declare variablesmy ($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; }

Page 12: Functions Please use speaker notes for additional information!
Page 13: Functions Please use speaker notes for additional information!

#!/usr/bin/perl#stringfuncmore.cgi - reads the payroll file and processes with functionsuse CGI::Carp qw(fatalsToBrowser);print "Content-type: text/html\n\n";use CGI qw(:standard);use strict;#declare variablesmy ($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.

Page 14: Functions Please use speaker notes for additional information!

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.

Page 15: Functions Please use speaker notes for additional information!

#!/usr/bin/perl#stringfuncmore.cgi - reads the payroll file and processes with functionsuse CGI::Carp qw(fatalsToBrowser);print "Content-type: text/html\n\n";use CGI qw(:standard);use strict;#declare variablesmy ($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.

Page 16: Functions Please use speaker notes for additional information!

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";

Page 17: Functions Please use speaker notes for additional information!