Question

I can use <> to loop there the pipeline input to a perl program. However how can I decide whether there are pipelined input, if there is no pipelined input I will use environment variable to load a file. I am trying to use:

my @lines = (<>);
if ($#lines == -1) {
    use setenv;
    open FILE, "$ENV{'ART_FILE_LIST'}" or die $!;
    @lines = <FILE>;
}

Obviously it doesn't work, because the program will waiting at the first line

Was it helpful?

Solution

use 5.010_000;

use utf8;
use strict;
use autodie;
use warnings qw<  FATAL all  >;
use open     qw< :std  :utf8 >;

END {
   close(STDOUT) 
       || die "can't close stdout: $!";
}


if (@ARGV == 0 && -t STDIN) {
    # NB: This is magic open, so the envariable
    #     could hold a pipe, like 'cat -n /some/file |'
    @ARGV = $ENV{ART_FILE_LIST} 
          || die q(need $ART_FILE_LIST envariable set);
}

while (<>) {
    # blah blah blah
}

OTHER TIPS

You can use the -t operator to see if you are a terminal, i.e., not a pipeline:

if (-t STDIN) { print "Terminal\n" } 
        else { print "Not a terminal\n" }

Use Getopt::Long

perl -Mylib -e 'Mylib::do_stuff' --i_am_pipe_lined

One of the things about UNIX pipelines is that they achieve their usefulness by not caring what's before them or after them. They just have a job to do and they do it. They do one thing, simply, but they all have switches to do their simple job with a little more customization.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top