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

Dear Monks,

How does one do something like this?

echo one two | PERLSCRIPT

@ARGV doesn't seem to work this way.

Thanks

#!/usr/bin/perl use strict; use warnings; foreach(@ARGV) { print "passed $_\n"; }

Replies are listed 'Best First'.
Re: Passing parameters to Perl Script
by gaal (Parson) on Feb 18, 2005 at 08:26 UTC
    If you give your input to the script in a via pipe, it will be acessible on standard input, not in the argument list.

    # your above script will work with this invocation: perlscript one two

    Alternatively Use the -n switch to perl itself, which causes your code to run in a loop with $_ set to a line of input every iteration (see perlrun for more details):

    #!/usr/bin/perl -n use strict; use warnings; my @words = split; # "split /\s+/, $_", more or less print "passed $_\n" for @words;

    Update: Added missing my keyword, thanks K_M_McMahon.

      If that is to be run under Windows, you will need to
      - wrap your script into a batch-file using perl2bat
      c:\> pl2bat scriptname c:\> echo 1 2 3|scripname.bat
      - or pipe to perl.exe
      echo 1 2 3|perl.exe scriptname
      because of some limitations of the Windows-shell


      holli, /regexed monk/
        If that is to be run under Windows, you will need to - wrap your script into a batch-file using perl2bat
        Does it really work? I'm not saying it doesn't, I just have a doubt. More specifically I've been using pl2bat and much to my surprise and disappointment I've noticed that I cannot use redirection of output (I had to add '-o' cmd line switches to "utilities" only moderately more complex than one-liners!). Don't know about input...

        I would check by myself but I'm under Linux now and I won't have access to a Windoze machine up until this evening...

Re: Passing parameters to Perl Script
by Thilosophy (Curate) on Feb 18, 2005 at 09:04 UTC
    You can use xargs to turn STDIN into command line arguments:
    echo one two | xargs PERLSCRIPT
    is the same as
    PERLSCRIPT one two
Re: Passing parameters to Perl Script
by Anonymous Monk on Feb 18, 2005 at 09:37 UTC
    With backticks (where do you think Larry got the idea of backticks from?)
    PERLSCRIPT `echo one two`
    But your question isn't a Perl question. It's a shell question,