Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: For vs. Foreach

by tilly (Archbishop)
on Feb 13, 2009 at 17:28 UTC ( #743648=note: print w/replies, xml ) Need Help??

in reply to For vs. Foreach

Can you offer a code example where for and foreach behave differently?

(And if you can do so, you might wish to use perlbug to report the bug that causes them to differ...)

Replies are listed 'Best First'.
Re^2: For vs. Foreach
by JavaFan (Canon) on Feb 13, 2009 at 18:04 UTC
    Peeking in toke.c suggests there is one difference between 'for' and 'foreach'. It seems it's valid to use 'for' somewhere in an attribute declaration. The documentation about attribute doesn't make it clear to me how. Best thing I can do is generating different error message when using 'for' or 'foreach':
    $ perl -wE 'sub foo: foreach' Invalid CODE attribute: foreach at -e line 1 BEGIN failed--compilation aborted at -e line 1. $ perl -wE 'sub foo: for' syntax error at -e line 1, next token ??? Execution of -e aborted due to compilation errors.
    Using 'for' is a compile time error, using 'foreach' a run time error (resulting in abortion of the compilation).

    If someone could show how to use 'for' inside an attribute, that would be dandy.

      You're right!
      use Attribute::Handlers; sub foreach :ATTR { print "Just another Perl hacker,\n"; } sub foo :foreach {} foo();
      prints the expected, while
      use Attribute::Handlers; sub for :ATTR { print "Just another Perl hacker,\n"; } sub foo :for {} foo();
      does not. I'll follow my own advice and send a perlbug.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2021-01-23 20:16 GMT
Find Nodes?
    Voting Booth?