-
True or False. A subroutine
can be called from an external perl program?
True
-
A subroutine follows all of the rules of
Perl?
Of course. Subroutines require a "new" perl rule for
calling
(getting into) the subroutine and returning
from (getting out of) the subroutine. But internally
a subroutine is just a fragment of perl code and must
follow all of the rules of perl.
-
True or False. subroutine Mike { perl code... } is the
proper method to define a subroutine?
False. One does not start with "subroutine" but with the
keyword "sub". (Apparently, perl programmers don't like
to type a lot.)
-
There are several ways to invoke a subroutine, what
is the proper way?
To call one's own subroutine, it is considered a good
practice to put an ampersand (&) in front of the name in the
definition; this prevents it from being confused with
any function perl provides. The arguments of the subroutine
are separated by commas and placed in parentheses (optional) after
the name of subroutine. Finally, the subroutine's return
value may be assigned to some variable. For example,
$test_ave = &sub_ave($test1, $test2, $test3);
-
A subroutine always returns a value, how exactly,
does that happen?
The last expression evaluated in a subroutine automatically becomes the subroutine's
return value. For example, the subroutine sub sub_ave
{
my $sum =0;
foreach (@_){$sum+=$_}
$sum/@_;
}
returns the last expression to be evaluated which is the sum
of the arguments divided by the number of arguments. i.e.,
the average.
-
True or False. Perl is the only language to take
advantage of regular expressions?
False
-
In a sentence or two, what are regular expressions?
A string equality comparison tests for an exact match;
for example, with
if($phone eq "867-5309")
the variable $phone must be exactly the phone number shown
with every number and hyphen in exactly the right spot to
give a result of true. A regular expression comparison, on
the other hand, has more flexibility and can be used to test
whether a string has a particular pattern, obeys certain
rules, follows some format, etc.;
for example, with
if($phone =~ m/\d{3}(-| )\d{4}/)
the variable $phone need only have three numbers followed by
a hyphen or a space followed by four numbers to give a
result of true, that is, it must have the format
of a phone number (without an area code).
-
True or False. Regular expressions are used to calculate
numerical values?
False. Regular expressions are used to calculate Boolean values in that they
are used to determine if a string matches a particular pattern with a result of
true or false.
-
When referring to matching a regular expression, what
is the basic syntax used?
Within the condition, for example of an "if", one places the
variable to be tested for the regular expression followed by an equals and a tilde, i.e. "=~", followed by space and an "m", and
followed by the regular expression contained between two
delimiters often forward slashes "/". For example,
if($phone =~ m/\d{3}(-| )\d{4}/)
As usual with perl, there is a shortened version
if(\d{3}(-| )\d{4}/)
where the variable is understood to be the default variable
$_ and it is also understood that one is looking for a match
of a regular expression, thus no "=~" and no "m".
-
How would one separate a string into several parts
without using regular expressions?
The function split takes as arguments a delimiter
and a string that uses that delimiter to separate various
items. The split function returns an array having the
items as elements. For example,
$to_parse = "Groucho,Chico,Harpo,Zeppo,Gummo";
@marx_brothers = split(',', $to_parse);
foreach $bro (@marx_brothers)
{
print "$bro Marx\n";
}