Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

checking for piped input data

by Anonymous Monk
on May 02, 2023 at 21:22 UTC ( [id://11151958]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I have a perl script which takes numerous arguments and, depending on those arguments, dumps a table to stdout. I would like to take that table from the first script and use it as input for the second etc etc. So, for example, in the below commands, I would like to use the table output by "script.pl -y goodbye" as the argument of -x. It seems I need to open a pipe, check for data and if none exists, continue with the script. If there is data, parse it, and pass it as an argument to -x. I feel this is how it should work in my head, but not sure whether this is correct in reality. I'm looking at Pipes and IPC on perl web pages via google, but feeling a bit overwhelmed. Can someone point me to a particular section I should focus on?

script.pl -x hello script.pl -y goodbye | script.pl -x

Thank you very much for you time !

Replies are listed 'Best First'.
Re: checking for piped input data
by hv (Prior) on May 02, 2023 at 23:20 UTC

    In script.pl -y goodbye | script.pl -x, the effect of the "|" in the shell command is to provide the STDOUT of the first script as the STDIN of the second script, just as if you'd written:

    script.pl -y goodbye >tempfile script.pl -x <tempfile

    So for this case you don't need to know anything about pipes and IPC, just read from STDIN.

      Thank you hv! This is super helpful and resolved my confusion. I was able to write a small test script to do what I need.
Re: checking for piped input data
by parv (Parson) on May 03, 2023 at 05:53 UTC

    Use -p function to check if STDIN is a pipe (to read from standard input for the option; else require an argument for the option) ...

    # Some time later, changed the code to collect all the input "do { ... + };", # not just the first line of STDIN. #!/usr/local/bin/perl use v5.26; use warnings; use Data::Dumper qw[ Dumper ]; use Getopt::Long; my $opt; GetOptions( 'opt:s' => \$opt ) or die $!; say Dumper( { 'Initial, opt' => $opt } ); if ( defined $opt ) { if ( -p STDIN ) { say q[Any option argument will be abandoned; collecting STDIN .. +.]; # Could process standard input line by line. # Here, all is collected at once. do { local $/; $opt = <STDIN> }; say Dumper( { ' ...after collecting STDIN, opt' => $opt } ); } elsif ( $opt eq '' ) { die qq[Neither a value was given for \$opt, nor was specified on + standard input.]; } else { say qq[STDIN is not a pipe; using existing \$opt = $opt.]; } } else { say qq[\$opt was not used, so STDIN was ignored.]; } say Dumper( { 'Final, opt' => $opt } );

    Later shoved the output of various cases under readmore ...

      Thank you parv! Let me try out the -p option. That's very interesting...

        Personally I would use the option specification that requires an argument, opt=s (instead of leaving it optional, opt:s, as rot earlier). If the option value would be -, then would switch to reading from standard input unconditionally (without using -p function). That special value of opt would be mentioned in the POD.

Re: checking for piped input data
by shmem (Chancellor) on May 03, 2023 at 01:54 UTC

    I don't smell perl, I smell shell.

    If script.pl -y goodbye yields a single string, use backticks:

    $ script.pl -x `script.pl -y goodbye`

    If the output of script.pl -y goodbye produces more, and each token is to be processed in turn with script.pl -x, use a loop:

    $ for arg in `script.pl -y goodbye`; do script.pl -x $arg; done

    Also, man xargs(1).

    If all arguments from perl -y goodbye are to be processed by a single call to perl -x and appear on a single line, have perl -x check @ARGV, probably using Getopt::Std. If they are on multiple lines and piped into script.pl -x, use the -n switch (again, see perlrun), probably checking for the switch -x in a BEGIN block.

    This is all I can come up with, given your question. You know, as in "garbage in - garbage out" :)

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: checking for piped input data
by jwkrahn (Abbot) on May 02, 2023 at 21:34 UTC
    I would like to use the table output by "script.pl -y goodbye" as the argument of -x.

    I believe this is what you want, but I haven't tested it.

    script.pl -x `script.pl -y goodbye`
    Naked blocks are fun! -- Randal L. Schwartz, Perl hacker
      Right, that would literally be passing the entire table as the argument which is not really intuitive in terms of my script. I thought I could use pipes instead to pass the table, but the underlying script would parse and concatenate the correct column which would then be passed it to the argument of -x. I also want to be able to chain these like all unix commands...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-19 10:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found