develooper Front page | perl.perl5.porters | Postings from May 2000

Re: Thoughts on maintaining perl

Thread Previous | Thread Next
From:
Tom Christiansen
Date:
May 26, 2000 08:02
Subject:
Re: Thoughts on maintaining perl
Message ID:
5226.959353339@chthon
>Things like ... implicit $_ or @_

Used judiciously, implicit $_ produces more readable code
than explicit variable use.  It's a pronoun.  Imagine speaking
English without any pronouns.  But long stretches without
antecedents will confuse just as short ones without pronouns
will annoy.

For example, this is *incredibly* clearer:

    while (<>) {
        next if /^=for\s+(index|later)/;
        $chars += length;
        $words += split;
        $lines += y/\n//;
    }

than using the the utterly obfuscated IMPLICIT NONE version:

	if (@ARGV == 0) { 
	    @ARGV = ('-');
	}

    ARGUMENT:
	while (@ARGV != 0) {
	    $ARGV = shift(@ARGV);        
	    unless (open(ARGV, $ARGV)) {
		print STDERR "Can't open $ARGV: $!\n";
		next ARGUMENT;
	    }
    LINE: 
	    while (defined($line = readline(*ARGV))) {
		if ($line =~ /^=for\s+(index|later)/) {
		    next LINE;
		} 
		$chars = $chars + length($line);
		$words = $words + split(' ', $line);
		$lines = $lines + ($line =~ y/\n//);
	    }
	}

The former is incredibly easier to read, to write, and to maintain
than the latter, which is the kind of painful crap you'd expect a
Pascal or Python programmer who didn't actually know Perl to produce.

Use $_ as the implicit pronoun.  It's good for you.  Counteridiomatic
Perl is *not* helpful to anyone.

next ARGUMENT, please. :-)

--tom

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About