in reply to help with the diamond operator

<$foo> reads from the file. So you're reading the first line in the file, then in print reading another one and printing it. Try using $_ instead.

For clarity sake, especially in situations where speed is not as important, it's best to say something like:

while (my $line = <foo>) { print $line, "\n"; }

The code is a bit more self-explanatory to Perl newbies (and sleep/food-deprived programmers) and is less likely to be buggy if you start using things like grep that twiddle with the value of $_.

Replies are listed 'Best First'.
Re: Re: help with
by HyperZonk (Friar) on Jul 27, 2001 at 07:19 UTC
    The code you offer is certainly clearer to an individual who has perhaps first started writing in Perl, coming from another language, but I have to quibble with your statement that "it's best."

    Part of learning a new language is learning its idioms. The implicit $_ is probably one of the most widely used idioms in Perl, and appears in almost every well-written script. By encouraging a new programmer to "pass by" that idiom, I think you make it less likely that the person will learn its use.

    I would say that it is actually "best to say" what the new programmer will have to learn to program efficiently and to read scripts by other programmers. After all, particularly in this case, the implicit $_ is not a difficult concept. If the new programmer doesn't learn it here, then learning grep and map will only be that much more difficult.

    As for the argument that one may introduce bugs if one starts using things like grep, it only becomes that much more likely that a programmer who is unfamiliar with the implicit $_ will make such errors if you try to "hide" its use from him/her in simpler structures.

    -HZ