Chapter 17 -------------------------------------- #!/usr/bin/perl -w # Classic “Hello World” as done with Perl print “Hello World!\n”; -------------------------------------- #!/usr/bin/perl -w # Create a file that will have a directory listing of user’s home dir print “About to read user’s home directory\n”; # open the home directory and read it opendir(HOMEDIR, “.”); @ls = readdir HOMEDIR; closedir(HOMEDIR); print “About to create file dirlist.txt with a directory listing of user’s home dir\n”; # open a file and write the directory listing to the file open(FILE, “>dirlist.txt”); foreach $item (@ls) { print FILE $item .”\n”; } close(FILE); print “All done\n\n”; } -------------------------------------- # Global variable $name is given a name $name = Paul # Enter our loop foreach (@filedata) { # declare a new variable for just the loop my $current_file_file; # create a local version of name to temporarily assign values within the # loop to local $name ... } -------------------------------------- while ( $counter < 10 ) { print $counter . “\n”; $counter++; } -------------------------------------- do { print $counter . “\n”; $counter++; } while ( $counter < 10); -------------------------------------- foreach $temp (@authors) { print $temp . “\n”; } -------------------------------------- if ( $name eq ‘Paul’ ) { print “Hi Paul\n”; } elsif ($name eq ‘Joe’) ( print “Hi Joe\n”; } elsif ($name eq ‘Jeremy’) { print “Hi Jeremy\n”; } else { print “Sorry, have we meet before? “; } -------------------------------------- #!/usr/bin/perl -T # check.pl # A Perl script that checks the input to determine if # it contains numeric information # First set up your Perl environment and import some useful Perl packages use warnings; use diagnostics; use strict; my $result; # Next check for any input and then call the proper subroutine # based on the test if (@ARGV) { # If the test condition for data from standard in is true run # the test subroutine on the input data $result = &test; } else { # else the test condition is false and you should run an error routine. &error; } # Now print the results print $ARGV[0].” has “.$result.” elements\n\n”; exit (0); sub test { # Get the first array element from the global array ARVG # assign it to a local scalar and then test the local variable # with a regular expression my $cmd_arg = $ARGV[0]; if($cmd_arg =~ m/[0-9]/) { return (‘numeric’); } else { # There was a error in the input; generate an error message warn “Unknown input”; } } sub error { # There was no input; generate an error message and quit die “Usage: check.pl, ()”; } --------------------------------------