in reply to Re: input from STDIN or from a file
in thread input from STDIN or from a file

also, there is a subtle difference between the <> operator and the implementation showed in the original post (although not very useful I think).

consider this:

$ cat test1.pl #!/usr/bin/perl use strict; use warnings; while (<>){ print; } $ cat > testfile two three $ echo "one" | perl test1.pl testfile one two three $ cat test2.pl #!/usr/bin/perl use strict; use warnings; my $file = shift @ARGV; my $ifh; my $is_stdin = 0; if (defined $file){ open $ifh, "<", $file or die $!; } else { $ifh = *STDIN; $is_stdin++; } while (<$ifh>){ print } close $ifh unless $is_stdin; $ echo "one" | perl test2.pl testfile two three

That is, the <> operator acts like:

unshift(@ARGV, '-') unless @ARGV; while ($ARGV = shift) { open(ARGV, $ARGV); while (<ARGV>) { ... # code for each line } }

i.e. processes STDIN AND any file given in the command line, while my implementation processes the file given in the command line OR STDIN.

citromatik

Replies are listed 'Best First'.
Re^3: input from STDIN or from a file
by Fletch (Bishop) on Jan 28, 2008 at 20:22 UTC

    Erm, no it does either arguments or STDIN if none were given just like the documentation in perlop and the code you copied there from says. Read it again:

    unshift( @ARGV, '-' ) unless @ARGV;

    Put a string '-' (which is shorthand to open to read from STDIN) onto the front of @ARGV unless there are already arguments in @ARGV.

    If you're getting the behavior you're seeing out of your test1.pl then something very strange is going on.

    $ cat my_test1.pl #!/usr/bin/perl use strict; use warnings; while (<>){ print; } $ cat infile 2 from infile 3 from infile $ echo "testing" | perl my_test1.pl infile 2 from infile 3 from infile

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.