Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Inserting Code Before an -n/-p Loop

by LanX (Sage)
on Dec 17, 2018 at 21:07 UTC ( #1227367=note: print w/replies, xml ) Need Help??


in reply to Inserting Code Before an -n/-p Loop

I somehow remember seeing it in the perldocs, but not sure where.

What's happening is that the snippet is literally injected into a loop (see perlrun)

}{ is just closing the loops body and opening a block to pair with the old end.

Nota bene that using BEGIN and END blocks have similar effects but are probably easier to read.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

update

from perlrun#*-n*

  • -n

    causes Perl to assume the following loop around your program, which makes it iterate over filename arguments somewhat like sed -n or awk:

    LINE: while (<>) { ... # your program goes here }

    ...

    BEGIN and END blocks may be used to capture control before or after the implicit program loop, just as in awk.

  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (7)
    As of 2023-03-27 16:27 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Which type of climate do you prefer to live in?






      Results (65 votes). Check out past polls.

      Notices?