Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How do I use the output of one perls script as the input of another on the commandline? like a pipeline

by Gretter (Novice)
on May 14, 2009 at 19:00 UTC ( #764143=perlquestion: print w/replies, xml ) Need Help??
Gretter has asked for the wisdom of the Perl Monks concerning the following question:

SOLVED:
Thanks for all the comments. Especially to thelonius.

Original question:
I've done a lot of searches on google, but apparently I don't know the terms to search for.
I hope this gives and idea of what I want. Script one:
#!/usr/bin/perl $arg shift @ARGV; print $arg;
Script two:
#!/usr/bin/perl $arg shift @ARGV; print $arg." and two";
In bash I would then write:<br  ./one.pl "one" | ./two.pl ... and hope for the output of: "one and two"
I would pass @ and not $ in the real code.
  • Comment on How do I use the output of one perls script as the input of another on the commandline? like a pipeline
  • Select or Download Code

Replies are listed 'Best First'.
Re: How do I use the output of one perls script as the input of another on the commandline? like a pipeline
by ikegami (Pope) on May 14, 2009 at 22:11 UTC
    Pipes sends the data to the program's standard input, so you need:
    #!/usr/bin/perl while (<STDIN>) { chomp; print "$_ and two\n"; }

    But it's better to use the following since <> is more flexible. It reads from STDIN unless file names are specified on the command line (in which case it reads the named files instead).

    #!/usr/bin/perl while (<>) { chomp; print "$_ and two\n"; }
Re: How do I use the output of one perls script as the input of another on the commandline? like a pipeline
by Corion (Pope) on May 14, 2009 at 19:07 UTC

    I'm sure you have read perlrun. It says:

    Upon startup, Perl looks for your program in one of the following places:
    1. Specified line by line via -e or -E switches on the command line.
    2. Contained in the file specified by the first filename on the command line. (Note that systems supporting the #! notation invoke interpreters this way. See "Location of Perl".)
    3. Passed in implicitly via standard input. This works only if there are no filename arguments--to pass arguments to a STDIN-read program you must explicitly specify a "-" for the program name.

    Maybe you can help us improve the documentation and tell how to express this more clear?

Re: How do I use the output of one perls script as the input of another on the commandline? like a pipeline
by Thelonius (Priest) on May 14, 2009 at 20:07 UTC
    I think you're confusing ARGV with standard input, but it's not clear. If you want (in bash), the output of one program to be the arguments of another, you can use the backticks `` or $( ).

    If you fix your programs by changing $arg shift @ARGV to $arg = shift @ARGV, then you can get the effect that I think you want by doing (in bash):

    ./two.pl $(./one.pl "one")
Re: How do I use the output of one perls script as the input of another on the commandline? like a pipeline
by sathiya.sw (Monk) on May 15, 2009 at 07:48 UTC
    As others suggested, you need to take care of the following two things as
    1. $arg shift @ARGV;
      to
      $arg = shift @ARGV;
    2. You are confused about the stdin, and the command line input.
    Another thing which i understand is, you wanted the output to be fed as command line argument instead of input. For that you can use the "xargs" command as
    ./one.pl "one" | xargs ./two.pl
    which will give your expected output. Hope this helps.

    Sathiyamoorthy
Re: How do I use the output of one perls script as the input of another on the commandline? like a pipeline
by sergstesh (Novice) on May 14, 2009 at 19:13 UTC
    There are many IPC-related modules; I really liked

    http://search.cpan.org/~glai/IPC-Exe-1.008/lib/IPC/Exe.pm

    .

    It requires advanced Perl knowledge (references and friends), but it's really worth it anyway

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://764143]
Approved by linuxer
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2018-10-22 16:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (119 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!