# 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 = }; 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 } ); #### > perl stdin-pipe.pl --opt Neither a value was given for $opt, nor was specified on standard input. at stdin-pipe.pl line 25. # This is interesting for "say Dumper ... Initial" still runs # though "die" happens earlier. $VAR1 = { 'Initial, opt' => '' }; #### > perl stdin-pipe.pl --opt Value $VAR1 = { 'Initial, opt' => 'Value' }; STDIN is not a pipe; using existing $opt = Value. $VAR1 = { 'Final, opt' => 'Value' }; #### > 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 UTC 2023 ' }; $VAR1 = { 'Final, opt' => 'Wed May 3 05:44:43 UTC 2023 ' }; #### > 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 UTC 2023 ' }; $VAR1 = { 'Final, opt' => 'Wed May 3 06:24:29 UTC 2023 ' };