Eric Roode on Wed, 15 May 2002 14:39:06 -0400


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

Re: Error message


rGeoffrey writes:
 > At 13:32 05/15/2002, Eric Roode wrote:
 > >Use of & to invoke functions is bad form. Has undesirable side
 > >effects.
 > 
 > When I started working at my current job I followed the style used in the 
 > group, which was to always use the &function () method of calling 
 > subroutines.  The big reason given for using the '&', which is not really 
 > needed, is that it makes it easier to read and much easier to grep through 
 > the code looking for subroutine calls.

I'm not clear on why this is an advantage.  But hey.


 > I have started to leave the '&' off in my newer code, but I am still not 
 > sure why it matters.  What makes it 'bad form'?

It has two side effects.

1. It disables prototypes.  Now, most functions don't use prototypes,
but if a function does have a prototype, it's probably there for a reason.

2. If you use an ampersand and no parentheses, the subroutine inherits 
the calling function's @_ (argument list).  If it makes any changes to 
@_, as many, many functions do, it'll screw up the calling program's
argument list.  Example:

    sub outer {
        &initialize;
        print "'$_'\n" foreach @_;
    }

    sub initialize {
        my $arg = shift;    # optional argument
        print "some stuff\n" if defined $arg;
    }

----------------------------------------------------------------------
Eric J. Roode                                            eric@myxa.com
Senior Software Engineer, Myxa Corporation
$_{"@{[sort/./g]}"}.=$_ for sort<>;$_[s/
(.)/ $1/g].=$_ for sort%_;print@_[1..99]
**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**