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 ...

Missing value for the option & no pipe ...

> perl stdin-pipe.pl --opt Neither a value was given for $opt, nor was specified on standard inpu +t. at stdin-pipe.pl line 25. # This is interesting for "say Dumper ... Initial" still runs # though "die" happens earlier. $VAR1 = { 'Initial, opt' => '' };

Only the value ...

> perl stdin-pipe.pl --opt Value $VAR1 = { 'Initial, opt' => 'Value' }; STDIN is not a pipe; using existing $opt = Value. $VAR1 = { 'Final, opt' => 'Value' };

Option value of X is ignored in preference to standard input ...

> date -u | perl stdin-pipe.pl --opt X $VAR1 = { 'Initial, opt' => 'X' }; Any option argument will be abandoned; collecting STDIN ... $VAR1 = { ' ...after collecting STDIN, opt' => 'Wed May 3 05:44:43 UT +C 2023 ' }; $VAR1 = { 'Final, opt' => 'Wed May 3 05:44:43 UTC 2023 ' };

No value for the option, with standard input ...

> date -u | perl stdin-pipe.pl --opt $VAR1 = { 'Initial, opt' => '' }; Any option argument will be abandoned; collecting STDIN ... $VAR1 = { ' ...after collecting STDIN, opt' => 'Wed May 3 06:24:29 UT +C 2023 ' }; $VAR1 = { 'Final, opt' => 'Wed May 3 06:24:29 UTC 2023 ' };

In reply to Re: checking for piped input data by parv
in thread checking for piped input data by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.