Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Difference between piped-in and invocation arguments to a Perl script?

by pat_mc (Pilgrim)
on Jan 23, 2009 at 12:00 UTC ( [id://738446]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, Monks -

I came across a very, very basic observation today which illustrated to me that I am lacking some basic understanding on Perl's behaviour in a Unix-family operating system environment. My apologies therefore for bothering you with this question.

So far, I thought that piping some input into a Perl programme was the same as handing over that input as an invocation argument to the programme. I found, however, that this is not the case:

Here's my test programme test.pl:
#! /usr/bin/perl -w print $ARGV[0];
My oberservations were:
$ test.pl Hello Hello
while
echo "Hello" | test.pl Use of uninitialized value in print at ./test.pl line 2

I thought that the pipe was passing on the arguments to the next element in the pipe via STDIN ... can anybody therefore please explain to me why this is not working?

Thanks for your help in advance!

Cheers - Pat

Replies are listed 'Best First'.
Re: Difference between piped-in and invocation arguments to a Perl script?
by cdarke (Prior) on Jan 23, 2009 at 12:09 UTC
    Passing a value as an argument (@ARGV) is not the same as reading from stdin, try:
    #!/usr/bin/perl use warnings; use strict; my $value; if (defined $ARGV[0]) { $value = $ARGV[0]; } else { $value = <STDIN>; } print "<$value>\n";
    Run that using your two methods, and note the difference. Also lookup chomp.
    I hope that helps.
Re: Difference between piped-in and invocation arguments to a Perl script?
by Corion (Patriarch) on Jan 23, 2009 at 12:18 UTC
Re: Difference between piped-in and invocation arguments to a Perl script?
by targetsmart (Curate) on Jan 23, 2009 at 12:05 UTC
    You have to use 'xargs' command for your requirement, for help put 'man xargs' in your shell.
    if you include xargs it will work
    echo "hello"|xargs perl -e 'print $ARGV[0]'
Re: Difference between piped-in and invocation arguments to a Perl script?
by jrw (Monk) on Jan 23, 2009 at 17:25 UTC
    Pat, actually your misunderstanding is at the unix level and has nothing to do with perl. Stdin is a totally different concept from command line arguments. The two are somewhat related by the standard use of filter programs which can be called like this:
    filter_program FILE1 FILE2 generate_data | filter_program
    The first form will read its input from the named files; the second form (with no command line arguments) will read its input from stdin. Both forms write their filtered output to stdout.
Re: Difference between piped-in and invocation arguments to a Perl script?
by rekoil (Novice) on Jan 24, 2009 at 04:50 UTC
    Simple but subtle = @ARGV != STDIN. The difference is that arguments are fed to the program at initialization time = they make up the values of the automatic @ARGV array - while STDIN is what is fed into a program *after* initialization via pipes, user input, or other redirects. So, if you want to pipe output to a perl app, use the <STDIN> filehandle to capture it, like so:
    #!/usr/bin/perl -w my $line = <STDIN>; print "Line is: " . $line; $ echo 'echo' | ./teststdin.pl Line is: echo
    This also works for redirecting files:
    $ cat echo.txt echoed file $ ./teststdin.pl < echo.txt Line is: echoed file
    Hope this helps!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://738446]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-20 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found