texh has asked for the wisdom of the Perl Monks concerning the following question:
When I run this with a single command line parameter of "1", I'd expect it to get to that poor-man's switch at the bottom and do a process() and finalize(), however only the "2" matches. By the time it gets to the "3", $runlvl is empty. The output of the above is:#!/usr/bin/perl # I know there should be # use strict; # and # use warnings; sub process { my ($fileName) = @_; open FILE, $fileName or die $!; while (<FILE>) { chomp $_; my @parts = split(',', $_); for (@parts) { # Do some stuff } } } sub finalize { print "Finished\n"; } my $runlvl = $ARGV[0]; if ($runlvl == 1) { # Do everything $runlvl = "23"; } die "Invalid arguments" unless ($runlvl =~ /^\d+$/); # For conversations sake, lets just read this script my $filename = $0; print "Run level: $runlvl\n"; # Do some stuff based on the runlevel for ($runlvl) { /2/ and process($filename); /3/ and finalize(); } print "Run level: $runlvl\n";
$ perl broken.pl 1
Run level: 23
Run level:
Wahoo! This time it outputs:#!/usr/bin/perl sub process { my ($fileName) = @_; open FILE, $fileName or die $!; while (my $line = <FILE>) { chomp $line; my @parts = split(',', $line); for (@parts) { # Do some stuff } } } sub finalize { print "Finished\n"; } my $runlvl = $ARGV[0]; if ($runlvl == 1) { # Do everything $runlvl = "23"; } die "Invalid arguments" unless ($runlvl =~ /^\d+$/); # For conversations sake, lets just read this script my $filename = $0; print "Run level: $runlvl\n"; # Do some stuff based on the runlevel for ($runlvl) { /2/ and process($filename); /3/ and finalize(); } print "Run level: $runlvl\n";
$ perl not_broken.pl 1
Run level: 23
Finished
Run level: 23
|
|---|