- Ray wanted an easy/medium/hard Perl test
Perl Test
Easy
- What does Perl stand for?
- What basic datatypes does Perl support?
- What does the following do:
Practical Extraction and Reporting Language
Scalar, Array, Hash, Typeglob
my @output = <STDIN>; foreach (@output) { print "$_\n" if /\d+/; }
Echoes lines that have numbers in them)
Medium
- What is use strict and why is it important to use?
- What does the following code do:
- What do you do to enable Perl to find user written modules?
Strict requires that variable references are scoped - e.g. my $var)
sub d { my %p = @_; print "\np:\n"; foreach (sort (keys (%p))) { if (/password/) { print "$_: <password>\n"; } else { print "$_: ${p {$_}}\n"; } # if } # foreach } # d
Displays the contents of the passed in hash %p substituting "<password>" if the hash happens to have a key of password
You need to modify the @INC array to include the path to your modules)
Hard
- What is the difference between require and use?
- What is a better way to do the following and why:
- What does the following print out when executed:
- Passing parameters to subroutines. Two parameters are passed in
- Parameter return: Two parameters are returned
- Evaluation of parameters: $y in foo is not passed in thus the print... foreach doesn't execute>
- Evaluation of true/false: foo returns $y which is errno, which is not 0 thus not true but the !$d evaluates to true and the print "Worked!\n" gets executed.
See this article for the answer
undef $/;
See this article) for the answer
sub foo { my ($x, $y) = @_; my @z = `$x 2>&1`; chomp @z; my $s = $?; if ($s ne 0) { if (defined $y) { print "$_\n" foreach (@z); } } return ($y, @z); } # foo my @a; my $c="ls /temp"; my $d; ($d, @a) = foo $c; print "Worked!\n" if !$d;
(It will output:
Worked!
Yet nothing really worked at all! Key issues are: