in reply to Need assistance updating text files with Perl
#!/usr/bin/perl -w use strict; foreach (1..3) { printx2 ($_); sub printx2 { my $num = shift; print $num *2,"\n"; } } # printx2() works here also! printx2 (32); # the above did NOT make a "private sub" to # the foreach statement. A subroutine name # has package scope. __END__ Prints: 2 4 6 64 Move sub printx2() out of the curly braces. This reduces the amount of indenting. The meaning doesn't change. foreach (1..3) { printx2 ($_); } printx2 (32); sub printx2 { my $num = shift; print $num *2,"\n"; }
|
|---|