FreeBSD 6 Unleashed

The script in Listing 11.1 uses most of the techniques covered thus far in the chapter, as well as a few that haven't been covered (and that's about all it does). This script is available on the included Disc 3 as simpledemo.pl.

Listing 11.1. Simple Demo Perl Script (simpledemo.pl)

#!/usr/bin/env perl # <STDIN> is an I/O handle referring to the standard input. In this context, # we're using it to read in text input from a prompt. The chomp operator removes # any trailing newline/return characters from the end of the input. print "Please enter your name: "; chomp ($name = <STDIN>); srand; # Initialize the random number generator @namelist = ("Bob","Jane","Frank"); @colorlist = ("green","red","blue","yellow"); foreach $testname (@namelist) { # You can select any random element of an array by using the rand() operator # on it. $colors{$testname} = $colorlist[rand(@colorlist)]; } while (($name,$color) = each(%colors)) { print "$name: $color\n"; undef ($n); # The undef() function undefines a variable. Works like NULL. do { $color = @colorlist[$n+1]; $n++; } until ($color eq "blue"); }

Категории