Placing my supreme opposition to obfuscation and other unecessarily dense code aside (every script should be clearly written and placed in a nice, well named little file with good documentation), why would you need a complete lack of proper scoping to create useful one liners? Could you provide a few examples? Thanks.
| [reply] |
Hm, I think I'm doomed to fail with that parenthetical remark. Still, compare this program:
perl -pe -i.bak 's/PERL/Perl/g' *.txt
with this one:
perl -e 'use File::Copy; foreach my $file (@args) { open my $fh, $file or die "Cannot open $file: $!\n"; my @lines; copy( $file, $file . ".bak"; while (defined( my $line = <$fh> )) { $line =~ s/PERL/Perl/; push @lines, $line; } close $fh; open $fh, '>', $file or die "Cannot write to $file: $!\n"; print $fh, @lines; }'
The first uses global variables. The second uses lexicals. I typed both straight off the top of my head, just as I would at a command line. I'm willing to bet that the first doesn't have any errors. I'll be pleasantly surprised if the second doesn't have at least one typo.
Update: Five days later, someone does spot a typo! Well done, coolmichael! (completely unintentional on my part)
| [reply] [d/l] [select] |