in reply to Oddity...losing filename from stdin - very wierd

Try it on a directory with several files. You should see that you only get half the files in the directory.

Why is that? A little deparse gives us the answer:

perl -MO=Deparse -n -e 'print "file is: ".<>;'
Should give us:
LINE: while (defined($_ = <ARGV>)) { print 'file is: ' . <ARGV>; }
So you see, every line of this loop, one filename is unshifted from ARGV going into $_, and one is unshifted and printed, making you miss the first, the third, etc.

What you need to do is something like

ls dir | perl -n -e 'print "File is $_"'


You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue.

Replies are listed 'Best First'.
Re: Re: Oddity...losing filename from stdin - very wierd
by snafu (Chaplain) on Apr 09, 2002 at 17:01 UTC
    Fellow monk, if I could vote you up anymore I would. You are correct! It did not occur to me that perl is invoked for every instance of output from ls. Thus, it was not necessary to push the output of ls into a while loop (unles I wanted to do something more granular to the output of each instance from ls). Thanks a bunch! See? It was something I was doing wrong! :)

    Another lesson learned! I'll log that away! :) Thank you much!

    _ _ _ _ _ _ _ _ _ _
    - Jim
    Insert clever comment here...

      You missread what Dog & Pony is saying. Perl isn't "Invoked for every instance of output from ls" ... the problem you had was using the -n option of perl in addition to your own while loop. You need one, or the other, but not both.