http://www.perlmonks.org?node_id=440342


in reply to Array iterator factory

Nice. I like this function more than the array iterator classes I've seen.

The &$iter; syntax in the documentation is evil though. If you use it it will break things sooner or later. It's better to change them all to $iter->(). (See perlfaq7, "What's the difference between calling a function as &foo and foo()?")

ihb

See perltoc if you don't know which perldoc to read!

Replies are listed 'Best First'.
Re^2: Array iterator factory (())
by tye (Sage) on Mar 17, 2005 at 16:46 UTC

    Note that &$iter() is just fine (it has none of the problems of &$iter;) and is even better than $iter->() in that it works on fairly old versions of Perl (though enough time has passed since the $iter->() syntax was introduced that few need to worry much about supporting versions of Perl that predate it).

    It seems that 'everyone' focuses on the & as being the problem and never mention that ampersand with parens is okay, sometimes even an advantage. Note that the both $iter->() and &$iter() ignore prototypes (since such are only enforced at compile time, a time during which perl doesn't know what function $iter might point to) and neither can be used directly with built-in functions so not even the difference between func() and &func() apply between &$iter() and $iter->().

    (tye)Re: A question of style covers much of & and () and calling subroutines.

    - tye        

      It seems that 'everyone' focuses on the & as being the problem and never mention that ampersand with parens is okay, sometimes even an advantage.

      Because I've repeatedly pointed out the issue with &foo; I take the easy way in my replies and don't mention &foo() as the alternative. Instead of elaborating on the same issue once again I'll quote myself. From Re: Hash values and constants:

      I intentionally avoided mentioning & as a way of disambiguate the constant. This is because it will confuse and mislead people to think that you can do &foo; instead of foo(), which isn't the case.
      From Re: Re: Forward-referenceing subs:
      For some reason, it's much more common to drop the parenthesis when dereferencing than when making a regular subroutine call.
      Therefore I consistently use $foo->() as it doesn't leave room for such subtle unexpected behaviour as &$foo; presents.

      ihb

      See perltoc if you don't know which perldoc to read!

Re^2: Array iterator factory
by Roy Johnson (Monsignor) on Mar 17, 2005 at 12:13 UTC
    Good point. I was not aware of that. Code is updated accordingly.

    Caution: Contents may have been coded under pressure.