Workshop

The workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try to understand the quiz and exercise answers before you go on to tomorrow's lesson.

Quiz

1:What is %ENV used for? Why is it useful?
A1: The %ENV hash holds the variable names and values for the script's environment. On Unix and Windows, the values in this hash can be useful for finding out information about the system environment (or for changing it and passing it on to other processes). On the Mac %ENV doesn't serve any useful purpose.
2:What's the difference between using system and backquotes?
A2: The system command runs some other program or script from inside your Perl script and sends the output to the standard output. Backquotes also run an external program, but they capture the input to a scalar or list value (depending on context).
3:Why would you want to use multiple processes in your Perl script?
A3: Multiple processes are useful for portioning different parts of your script that might need to be run simultaneously, or for splitting up the work your script needs to do.
4:What does fork do?
A4: The fork function creates a new process, a clone of the original process. Both the new processes continue executing the script at the point where the fork occurred.
5:What's the difference between system and exec?
A5: system and exec are closely related; both are used to execute an external program. The difference is that system does a fork first; exec stops running the current script in the current process and runs the external program instead.
6:Can Win32 processes be used interchangeably with fork?
A6: Win32::Process and fork are not interchangeable. The fork function ties very strongly to processes on Unix; Win32::Process is more analogous to a fork followed immediately to an exec.

Exercises

1:(Unix only) Create a script that takes a single number as an argument. Fork that number of child processes, and make sure the parent waits for each to finish. Each process should:
  • Generate a random number between 1 and 100,000.

  • Sum all the numbers between 1 and that random number.

  • Print the result.

A1: Here's one answer:
#!/usr/local/bin/perl -w
use strict;

if (@ARGV > 1) {
    die "Only one argument, please.
";
}
 elsif ($ARGV[0] !~ /^d+/) {
    die "Argument should be a number.
";
}

my $pid;
my $procs = pop;

foreach my $i (1..$procs) {
    if (defined($pid = fork)) {
        if ($pid) { #parent
            print "Parent: forked child $i
";
        }  else {    #child
            srand;
            my $top = int(rand 100000);
            my $sum;
            for (1..$top) {
                $sum += $_;
            }
            print "Finished child $i:  Sum of $top is $sum
";
            exit;
        }
    }
}

while ($procs > 0) {
    wait;
    $procs--;
}

2:(Unix only) Modify the img.pl script from Day 10 (the one that printed information about the images in an HTML file) such that the output is e-mailed to you instead of displayed on the screen. HINT: this command will send a message:
mail [email protected] < bodyofmessage

A2: All you need are three changes. First, create and open a temporary file:
my $tmp = "tempfile.$$";                # temporary file;
open(TMP,">$tmp") || die "Can't open temporary file $tmp
";

Second, make sure all print statements write to that file:

if (exists($atts{$key} )) {
   $atts{$key}  =~ s/[s]*
/ /g;
   print TMP "   $key: $atts{$key} 
";
}

Finally, use system to mail the temporary file and remove it:

close TMP;
my $me = "[email protected]";
system("mail $me <$tmp");
unlink $tmp

3:(Windows only) Create a script that takes a directory listing (using the dir command) and prints only the filenames, one per line (you don't have to sort them).
A3: Here's one way to do it (start the substr function from character 44 if you're on Windows 95):
#!c:/perl/bin/perl -w
use strict;

my @list = `dir`;

foreach (@list) {
   if (/^w/) {
      print substr($_,39);
   }
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset