QM has asked for the wisdom of the Perl Monks concerning the following question:

perlmod says:
When you use the -n and -p switches to Perl, BEGIN and END work just as they do in awk, as a degenerate case.
Well, that doesn't document anything. And someone is bound to have stopped reading just before this sentence and said, "Try it and see!". To which I reply, "If I have to try it to figure out what it *should* be doing, it's not documented properly!"

Or am I just being AR again?

(BTW, what would perlmod say if it had been written for ARses like me?)

-QM
--
Quantum Mechanics: The dreams stuff is made of

Replies are listed 'Best First'.
Re: BEGIN and END with -p or -n
by Fletch (Bishop) on Dec 01, 2005 at 05:02 UTC

    What -n and -p do is wrap something like the following around your code (no, it's not the exact code; just a rough facsimile)

    while( <> ) { # -e goes here } continue { print; }

    If you have a BEGIN {} or END {} in awk the code in those is run before or after the first line of input is read. So it's in effect saying you get:

    ## code that was in BEGIN block while( <> ) { ## -e code } continue { print; } ## code that was in END block
Re: BEGIN and END with -p or -n
by ambrus (Abbot) on Dec 01, 2005 at 08:48 UTC

    What BEGIN and END really does is documented in perlsub. It wouldn't make much sense to repeat it in perlrun.

      But it would make great sense that if you're going to single them out in perlrun, that you point to perlsub "for more information on how BEGIN and END work."


      "What BEGIN and END really does is documented in perlsub".

      Actually perlsub points back to perlmod:

      The BEGIN , CHECK , INIT and END subroutines are not so much subroutines as named special code blocks, of which you can have more than one in a package, and which you can not call explicitly. See "BEGIN, CHECK, INIT and END" in perlmod

      --
      John.

Re: BEGIN and END with -p or -n
by sauoq (Abbot) on Dec 02, 2005 at 09:22 UTC

    Did anyone else read the subject of this post the way I did?

    -sauoq
    "My two cents aren't worth a dime.";
    
      heh... yes, I did a quick double-take ;)
      Sorry, I missed that. I must be in Perl6 mode, as whitespace has meaning.

      So who's going to define that -r command so you guys can spell something more interesting? Or do we just need to use a pipe?

      perl -p|-n

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

Re: BEGIN and END with -p or -n
by jmcnamara (Monsignor) on Dec 01, 2005 at 15:43 UTC

    "When you use the -n or -p switches to Perl, BEGIN and END work just as they do in awk"

    What confuses me about this is the corollary that without -n and -p BEGIN and END don't work as they do in awk.

    That is, it seems to imply that BEGIN and END work differently with -n and -p than in other cases (apart from the obvious exception of -c).

    Is that the case?

    --
    John.

      No, they work the same. It's the rest of the code that works differently, insofar as it's not inside an implicit loop any more. BEGIN and END don't care whether they're inside a loop or not, which is why they "degenerate" to the case of awk and sed when they are in an implicit loop.
        So it seems that the statement in perlmod is extraneous, and should be cut, as BEGIN and END do what they do, regardless.

        There's no reason to mention awk, or imply a different behavior under -p or -n.

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of