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

Re: Thoughts on maintaining perl

Thread Previous | Thread Next
Tom Christiansen
May 26, 2000 08:02
Re: Thoughts on maintaining perl
Message ID:
>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 = ('-');

	while (@ARGV != 0) {
	    $ARGV = shift(@ARGV);        
	    unless (open(ARGV, $ARGV)) {
		print STDERR "Can't open $ARGV: $!\n";
		next ARGUMENT;
	    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. :-)


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About