Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: {} vs do{}

by MeowChow (Vicar)
on Jul 04, 2001 at 08:31 UTC ( [id://93782]=note: print w/replies, xml ) Need Help??


in reply to {} vs do{}

do BLOCK and naked BLOCK's are completely unrelated. The documentation for do BLOCK is illuminating:
Not really a function. Returns the value of the last command in the se +quence of commands indicated by BLOCK. When modified by a loop modifier, executes the BLO +CK once before testing the loop condition. (On other statements the loop modifiers test the c +onditional first.) do BLOCK does not count as a loop, so the loop control statements next +, last, or redo cannot be used to leave or restart the block. See the perlsyn manpage for alt +ernative strategies.
Naked BLOCK's on the other hand do not return values, cannot be used with loop modifiers, but can use loop control statements. Both naked BLOCK's and do BLOCK establish a new lexical scope within the BLOCK, however.

   MeowChow                                   
               s aamecha.s a..a\u$&owag.print

Replies are listed 'Best First'.
Re: Re: {} vs do{}
by scott (Chaplain) on Jul 05, 2001 at 01:34 UTC

    Hmmmm ... what about this:

    open( FH1 , '>foo' ); open( FH2 , '>bar' ); @fhs = ( FH1 , FH2 ); print { $fhs[ $_ % 2 ] } "$_\n" for ( 1 .. 10 );
    which does actually put odds in one file and evens in another.

    I understood this as the BLOCK returning the last value. Is something else going on?

    Scott 'Confused in Indiana' Crittenden
      the print BLOCK LIST form is, I suspect, analagous to something like map BLOCK list, where the BLOCK is converted into an anonymous sub which returns the value of the last expression in the block. These are syntactically different from naked BLOCKs. You can also get this behaviour with subroutine prototypes:
      sub MySub (&\@) { ... } MySub { # do something, return a value } @list;
         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print
      The "indirect object" uses a block for its syntax, and is a different rule than a general block in the middle of an expression.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-04-16 07:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found