in reply to Re: Getting data from either ARGV or STDIN
in thread Getting data from either ARGV or STDIN

Just according to -X is -t defaulting to STDIN so you could spare a word.
True, but I'd probably have to keep looking that up to remind myself what it's doing. I was actually thinking of spending another char and switching to !-p STDIN since that's a bit more self-documenting.

An alternative approach which seems cleaner to me could be to create an iterator which either implements:
Excellent suggestion! It's both more succinct and understandable.
my $iter = (@ARGV || -t STDIN) ? sub { shift @ARGV } : sub { <STDIN> } +; while (defined(my $in = $iter->())) { }

Thanks!

Replies are listed 'Best First'.
Re^3: Getting data from either ARGV or STDIN
by LanX (Saint) on Mar 11, 2021 at 23:43 UTC
    No matter which solution you choose in the end, you might want to hide the magic behind a small module with more documentation.

    Like this, you'll only need something like use ReadARGV; in your scripts

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re^3: Getting data from either ARGV or STDIN
by LanX (Saint) on Mar 11, 2021 at 23:30 UTC
    Maybe shorter

    *iter = (@ARGV || -t STDIN) ? sub { shift @ARGV } : sub { <STDIN> }; while (defined(my $in = iter() )) { }

    > Thanks!

    Your welcome. :)

    update

    renaming iter to input may be more self documenting.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery