Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: How does $. work in one liner?

by AnomalousMonk (Bishop)
on May 28, 2016 at 22:41 UTC ( #1164419=note: print w/replies, xml ) Need Help??

in reply to How does $. work in one liner?

Please see the discussion of the  .. operator in scalar (not list!) context in Range Operators in perlop. Also see discussion of  -n switch in perlrun. Also see  $. in perlvar. (Update: BTW: As far as I'm aware, there's no difference between the way  $. behaves in a one-liner versus the way it behaves anywhere else.)

Update: I always forget these in our own Tutorials: Flipin good, or a total flop? and The Scalar Range Operator.

Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: How does $. work in one liner?
by Eily (Monsignor) on May 28, 2016 at 23:42 UTC

    There's nothing special about one-liners, not even the options which can be present in the shebang. The -n option on the other hand can make using $. a little more tricky, as it will not be reset from one file to the other if you are processing several at once. You can force it to be reset by closing the file handle as shown in eof, but personally at this point I would consider turning the one-liner into a .pl file. The $. issue is still true in a file script when using <>, but I haven't often seen it used to read from files rather than STDIN outside of a one-liner.

      In fact Eily is precise as always elevenfly, and he pointed exactly where $. is special in oneliners.

      He gave you the definition, i'll add an example. Given a.txt and b.txt as following:

      #cat a.txt a file line 1 a file line 2 a file line 3 #cat b.txt b file line 1 b file line 2 b file line 3
      if you use $. to check line number (and $. is implicit for a bare .. flip-flop operator), you have:
      #perl -ne "print if 1..2" a.txt b.txt a file line 1 a file line 2
      and so is because $. does not reset automatically for an implicit close of the filehandle (and -n iterates across file given as arguments and reopen each time ARGV so without explicitly closing it).

      But if you use the right idiom close ARGV if eof everything runs as expected: $. is reset by the explicit close that happens only if eof is encounterd:

      #perl -ne "print if 1..2; close ARGV if eof" a.txt b.txt a file line 1 a file line 2 b file line 1 b file line 2


      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re^2: How does $. work in one liner?
by Anonymous Monk on May 29, 2016 at 01:05 UTC

    Thank you! perlop really talks about that!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2021-10-20 13:11 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (81 votes). Check out past polls.