Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Join a file piped to a perl script

by tradez (Pilgrim)
on Apr 12, 2002 at 19:49 UTC ( #158653=perlquestion: print w/replies, xml ) Need Help??
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.

"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,

      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 (Chancellor) 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$:./ somefile was ran.

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://158653]
Approved by jlk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2018-06-22 04:43 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (121 votes). Check out past polls.