« Clearcase Merging/CQ Time Stamp problem/CQ Help problem | Main | PQA CDO.Message »

Perl test

  • Ray wanted an easy/medium/hard Perl test

Perl Test

Easy

  1. What does Perl stand for?
  2. Practical Extraction and Reporting Language

  3. What basic datatypes does Perl support?
  4. Scalar, Array, Hash, Typeglob

  5. What does the following do:
  6. my @output = <STDIN>;
    
    foreach (@output) {
      print "$_\n" if /\d+/;
    }
    

    Echoes lines that have numbers in them)

Medium

  1. What is use strict and why is it important to use?
  2. Strict requires that variable references are scoped - e.g. my $var)

  3. What does the following code do:
  4. 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

  5. What do you do to enable Perl to find user written modules?
  6. You need to modify the @INC array to include the path to your modules)

Hard

  1. What is the difference between require and use?
  2. See this article for the answer

  3. What is a better way to do the following and why:
  4. undef $/;
    

    See this article) for the answer

  5. What does the following print out when executed:
  6. 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:

    • 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.