Calling subroutines with the & sigil
Hello,
I read somewhere that it's bad practice anymore to call a =
subroutine like this:
&subroutine();
I've also read where it's faster to call a sub like this:
&subroutine;
than it is to call like this:
subroutine();
Is this true? I searched but I can't find a good recent =
discussion of this. What is the best (preferred) way to call =
subroutines today?
Thanks,
Marc
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Calling subroutines with the & sigil
On Wed, 13 Apr 2011 16:07:47 -0700, sono-io wrote:
> Hello,
>
> I read somewhere that it's bad practice anymore to call a
subroutine
> like this:
>
> &subroutine();
>
> I've also read where it's faster to call a sub like this:
> &subroutine;
>
> than it is to call like this:
> subroutine();
>
> Is this true? I searched but I can't find a good recent
discussion of
> this. What is the best (preferred) way to call subroutines today?
It is better to call them as subroutine() provided that you don't define =
a
subroutine with the same name as a Perl builtin. It's better to learn th=
e
names of the Perl builtins than to give up and always use &subroutine.
The builtins are all listed in perlfunc. It's actually not that hard to
get them. The only one that people habitually collide with is log().
If you're worried about the speed overhead of one vs the other then eithe=
r
you are worried about the wrong thing or you have bigger problems. Worry=
about speed only when (a) you've determined that your code is too slow,
and then (b) you've profiled it to find out where the bottleneck is (hint=
:
it's not likely to be in subroutine call overhead).
--
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=3D0137001274
http://www.oreillyschool.com/courses/perl3/
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Calling subroutines with the & sigil
Thanks to both Peter Scott, and Brian Fraser (who wrote me =
off-list). Very informative. I'm cleaning up a very old script that =
uses the &subroutine construct and learning more Perl in the process.
> If you're worried about the speed overhead of one vs the other then =
either
> you are worried about the wrong thing or you have bigger problems
I'm not concerned about speed - I was just curious from what =
I've read.
> The builtins are all listed in perlfunc. It's actually not that hard =
to
> get them. The only one that people habitually collide with is log().
The only one I was concerned with was a sub called "if", but it =
doesn't look like it's a built-in function. However, just so it makes =
more sense to me and to be on the safe side, I've renamed it =
"if_routine".
Thanks again,
Marc=
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Calling subroutines with the & sigil
On Apr 13, 4:07=A0pm, sono... [at] fannullone.us wrote:
> Hello,
>
> =A0 =A0 =A0 =A0 I read somewhere that it's bad practice anymore to call a=
subroutine like this:
>
> &subroutine();
>
Normally yes but there are a few circumstances
where you need the sigil. See: perldoc perlsub
> =A0 =A0 =A0 =A0 I've also read where it's faster to call a sub like this:
> &subroutine;
>
> than it is to call like this:
> subroutine();
You may be thinking of recursive calls. From
perlsub :
Subroutines may be called recursively. If a
subroutine is called using the "&" form, the
argument list is optional, and if omitted, no [at] _
array is set up for the subroutine: the [at] _ array
at the time of the call is visible to subroutine
instead. This is an efficiency mechanism that
new users may wish to avoid.
Also, for quick reference, see following section
in perlfaq7:
"What's the difference between calling a
function as &foo and foo()?"
--
Charles DeRykus
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Calling subroutines with the & sigil
On Thu, 14 Apr 2011 08:49:45 -0700, sono-io wrote:
> The only one I was concerned with was a sub called "if",
Sounds like a great device for an obfuscated Perl contest entry. That's
the only place I want to see it, though.
>but it
doesn't
> look like it's a built-in function. However, just so it makes more
> sense to me and to be on the safe side, I've renamed it
"if_routine".
--
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=3D0137001274
http://www.oreillyschool.com/courses/perl3/
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/