http://www.perlmonks.org?node_id=158653

tradez has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks, I recently needed to write some code to join a file piped to a perl script. Rather then coding the whole
my $file = $argv[0]; open (MYFILE, "$argv[0]"); while(<MYFILE){ .....
I remembered there being a special perl way of handling files piped to a script. a $_ type thing ( special character) that fills a data structure with the array for you, saving me precious key strokes. Please inform.

Tradez
"Never underestimate the predicability of stupidity"
- Bullet Tooth Tony, Snatch (2001)

Edit kudra, 2002-04-15 Changed title

Replies are listed 'Best First'.
Re: Lazy Coder
by dws (Chancellor) on Apr 12, 2002 at 20:27 UTC
    open (MYFILE, "$argv[0]");

    Unless this script is for controlled use, you might consider untainting $argv[0] before using it. Otherwise, you risk having a day ruined when some lamer passes    'rm -rf * |' as an argument.

Re: Lazy Coder
by Zaxo (Archbishop) on Apr 12, 2002 at 20:11 UTC

    You may be looking for the diamond operator, <>. see perldoc perlop.

    After Compline,
    Zaxo

      Lazy in a good way:
      my $input = join ('', <>);
      Or, if you prefer named arguments:
      my $input = open (IN, $ARGV[0]) && join ('', <IN>);
      Don't forget that it's @ARGV and not @argv.

      If you're using this as a CGI application, it virtually, but not entirely, goes without saying:
      use CGI;
False lazyness
by Fletch (Bishop) on Apr 12, 2002 at 19:54 UTC

    perldoc perlopentut, perldoc -f open

      Not really what I was going for, i meant if like prompt$:./perlscript.pl somefile was ran.

      Tradez
      "Never underestimate the predicability of stupidity"
      - Bullet Tooth Tony, Snatch (2001)