in reply to Which way is recommended ?

Using lexical scope variable instead of global FILE you minimize unintentional name collisions and make the code more clear. The code reader will not be thinking about is FILE used anywhere else.

Using human readable name $line instead of $_ you make the code more readable.

Replies are listed 'Best First'.
Re^2: Which way is recommended ?
by matrixmadhan (Beadle) on Nov 21, 2008 at 18:32 UTC
    Thanks for the reply
    So, for one-off codes/small codes it doesn't make sense right? Any form could be used.

      I would suggest that using the lexical variable for the filehandle is a good idea even in small one-off scripts. A very wise person once told me

      There is nothing more permanent than temporary code.

      Quite often quick-and-dirty scripts grow and become critical. Also, using the lexical filehandles is a good habit to cultivate.

      On the other hand, I often prefer to use $_ rather than an explicit variable in small loops. For me that's more of a style issue than anything else.

      G. Wade

      Exactly. If you are feeling (over)confident you can just code this:

      open (FILE, $filename) or die $!; print while <FILE>;

      Or on the command line:

      perl -pe '$_' file.txt

      UPDATE: thanks for the correction, quester

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        Pardon me, but even one liners need to enable warnings...
        $ perl -wpe '$_' file.txt Useless use of a variable in void context at -e line 1. ...
        It should actually just be
        $ perl -wpe '' file.txt