Perl Style: Use $_ in Short Code

  • Contrary to beginners’ belief, $_ improves legibility. Compare:

        while ($line = <>) {
            next if $line =~ /^#/;
            $line =~ s/left/right/g;
            $line =~ tr/A-Z/a-z/;
            print "$ARGV:";
            print $line;
        }
    

    with:

        while ( <> ) {
            next if /^#/;
            s/left/right/g;
            tr/A-Z/a-z/;
            print "$ARGV:";
            print;
        }
    

Forward to Use foreach() Loops
Back to Use Hashes of Records, not Parallel Arrays
Up to index

Copyright © 1998, Tom Christiansen All rights reserved.

Tags

Feedback

Something wrong with this article? Help us out by opening an issue or pull request on GitHub