Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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

L*

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.

In reply to Re^3: How does $. work in one liner? -- it does not reset automatically by Discipulus
in thread How does $. work in one liner? by elevenfly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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







    Results (88 votes). Check out past polls.

    Notices?