Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Just another Perl shrine
 
PerlMonks  

Re: <> oddity ?

by LanX (Abbot)
on Mar 27, 2013 at 10:26 UTC ( #1025668=note: print w/ replies, xml ) Need Help??


in reply to <> oddity ?

this

$ ./weird.pl sample.txt

is fundamentally different to

$ ./weird.pl <sample.txt or $ cat sample.txt | ./weird.pl

the first passes a filename as argument while the latter two pipe the content of this file into STDIN.

See ARGV for a way to solve this.

Cheers Rolf

( addicted to the Perl Programming Language)


Comment on Re: <> oddity ?
Select or Download Code
Re^2: <> oddity ?
by hdb (Parson) on Mar 27, 2013 at 10:31 UTC

    If you call the code below stdin.pl,

    while(<>) { print; }

    then all three commands below result in the same output:

    $ perl stdin.pl stdin.pl $ perl stdin.pl < stdin.pl $ cat stdin.pl | perl stdin.pl
      you're right

      from perlop

             The null filehandle <> is special: it can be used to emulate the
             behavior of sed and awk.  Input from <> comes either from standard
             input, or from each file listed on the command line.  Here’s how it
             works: the first time <> is evaluated, the @ARGV array is checked, and
             if it is empty, $ARGV[0] is set to "-", which when opened gives you
             standard input.  The @ARGV array is then processed as a list of
             filenames.  The loop

      while (<>) { ... # code for each line }

      is equivalent to the following Perl-like pseudo code:

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

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Re^2: <> oddity ?
by Krambambuli (Deacon) on Mar 27, 2013 at 10:31 UTC
    I'm aware of that, thanks. But *should* my program work nevertheless or not ?

    So far I don't realize why it shouldn't. I just can see that it doesn't, and I'm trying to understand *why*.

    Thank you.

      I still do not know why your code does not work. I wanted to reply to LanX only. Apologies for cluttering your thread. I'll be silent now.

      OK ... sorry ... I never needed that magic ...

      Your code is too long for me to spot the reason, you should try shortening it till you isolated the problem.

      Maybe one of your modules (like IO::Select ) clutters something?

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1025668]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-04-23 23:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (557 votes), past polls