[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Constants as array refs



On Thu, Mar 23, 2000 at 04:42:20PM -0500, Bernie Cosell wrote:
> On 23 Mar 2000, at 15:39, Ronald J Kimball wrote:
> 
> > > It is (very unfortunately) necessary to remember that constants
> > > defined with "use constant" are actually subroutines.
> > > 
> > > 	for ( @{ &CARRAY } ) { ...
> > 
> > Using the & syntax prevents Perl from optimizing calls to constant
> > subroutines.
> 
> I don't understand...  I didn't think there was a semantic difference 
> between:
>    a()
> and
>    &a
> but apparently there is..  ????

The differences between func() and &func are subtle but significant, I
think.

The most important difference is that C<&func> is basically equivalent to
C<&func(@_)>; C<&func> with no parens passes the current @_ to the called
subroutine.  C<func()> and C<func>, on the other hand, call the subroutine
with an empty argument list, as does C<&func()>.

Another difference is that without the &, parentheses are optional, but
with the &, the parentheses are not optional.  C<func 7> calls the
subroutine with an argument of 7, while C<&func 7> is a syntax error;
C<&func(7)> must be used instead.

A third difference is the one mentioned previously, that Perl can only
optimize calls to constant subroutines if the call does not have the &.
A constant subroutine has an empty prototype and a body consisting of a
literal scalar value, such as C<sub const () { 7 }>.  Thus, C<const> would
be optimized to C<7>, but C<&const> would be compiled as C<&const>.


Ronald


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe