Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Subroutines and '&'

Reply
Thread Tools

Subroutines and '&'

 
 
Bill H
Guest
Posts: n/a
 
      08-07-2007
I have always called my subroutines with an '&', Example:

&something;

sub something
{
return;
}

But I see in the perlfaq that the '&' isnt used. Is there any reason I
should / should't be using the '&' or does it just not make a
difference?

Bill H

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-07-2007
Bill H wrote:
> I have always called my subroutines with an '&', Example:
>
> &something;
>
> sub something
> {
> return;
> }
>
> But I see in the perlfaq that the '&' isnt used. Is there any reason I
> should / should't be using the '&' or does it just not make a
> difference?


perldoc -q "&foo"

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Randal L. Schwartz
Guest
Posts: n/a
 
      08-07-2007
>>>>> "Sherm" == Sherm Pendley <(E-Mail Removed)> writes:

Sherm> It *does* make a difference - if you don't know or understand what that
Sherm> difference is, or you don't specifically want the behavior specified by
Sherm> the &, you shouldn't use it.

Or, for the opposite point of view, if you have:

sub log { print STDERR localtime() . ": @_\n" }

then you better darn well invoke it as:

&log("my message");

and *not*:

log("my message"); # invokes built-in logarithm function

And until you know all the perl built-ins, you should use &. And this
is what we teach in Learning Perl.

Please don't be so quick to dismiss the value of the leading &.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      08-07-2007
>>>>> "~greg" == ~greg <(E-Mail Removed)> writes:

~greg> It is still required,
~greg> but only when dealing with references (I think)

Or when dealing with a subroutine that is the same name as a built-in.
See my other post.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-07-2007
On Aug 7, 2:32 pm, (E-Mail Removed) (Randal L. Schwartz) wrote:

> Or, for the opposite point of view, if you have:
>
> sub log { print STDERR localtime() . ": @_\n" }
>
> then you better darn well invoke it as:
>
> &log("my message");
>
> and *not*:
>
> log("my message"); # invokes built-in logarithm function
>
> And until you know all the perl built-ins, you should use &.


The difference, of course, is that failing to use & when required will
generate a pretty explicit warning, while using it when you shouldn't
will not. That's why I generally tell my students this is the one
tiny piece of Learning Perl that I disagree with. :-/

Paul Lalli

 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      08-07-2007
~greg wrote:
> It is still required,
> but only when dealing with references (I think)


And nonlocal context 'jumps':

...
sub indirect { print "otherthing\n@_\n" };

sub onething { goto &indirect; print "this wont show up\n" }

onething( qw'A B C' );
...


Regards

M.
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      08-07-2007
Randal L. Schwartz wrote:
>>>>>> "Sherm" == Sherm Pendley <(E-Mail Removed)> writes:

>
> Sherm> It *does* make a difference - if you don't know or understand what that
> Sherm> difference is, or you don't specifically want the behavior specified by
> Sherm> the &, you shouldn't use it.
>
> Or, for the opposite point of view, if you have:
>
> sub log { print STDERR localtime() . ": @_\n" }
>
> then you better darn well invoke it as:
>
> &log("my message");
>
> and *not*:
>
> log("my message"); # invokes built-in logarithm function
>
> And until you know all the perl built-ins, you should use &. And this
> is what we teach in Learning Perl.
>
> Please don't be so quick to dismiss the value of the leading &.


Fortunately the syntax highlighting in my editor of choice allows me to
distinguish between built-in functions and user defined subroutines.




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
trudge@gmail.com
Guest
Posts: n/a
 
      08-08-2007
On Aug 7, 2:54 pm, (E-Mail Removed) (Randal L. Schwartz) wrote:
> >>>>> "~greg" == ~greg <(E-Mail Removed)> writes:

>
> ~greg> It is still required,
> ~greg> but only when dealing with references (I think)
>
> Or when dealing with a subroutine that is the same name as a built-in.
> See my other post.
>


In another situation, I find it most useful as well. For example, I
typically determine what to do in a script by checking an incoming
parameter of a form:

my $MODE=param('Mode');
{
no strict;
$MODE="GetLogin" unless $MODE; # default sub to execute
&$MODE; # else execute this sub
}

This saves a lot of 'if-else' chains. Just my 2 Canadian cents.
--
Amer Neely
Web Mechanic - www.webmechanic.softouch.on.ca

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-08-2007
On Aug 8, 5:05 am, (E-Mail Removed) wrote:
> I
> typically determine what to do in a script by checking an incoming
> parameter of a form:
>
> my $MODE=param('Mode');
> {
> no strict;
> $MODE="GetLogin" unless $MODE; # default sub to execute
> &$MODE; # else execute this sub
>
> }
>
> This saves a lot of 'if-else' chains. Just my 2 Canadian cents.


It's also a rather absurdly large security whole. You have no way of
knowing what that parameter is. You are making the assumption that
the only way to contact your CGI script is via your form. This is
incorrect. A user can contact your CGI script without ever going near
your form.

You are allowing your users to call any subroutine in your program.

Paul Lalli

 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      08-08-2007
John W. Krahn <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Randal L. Schwartz wrote:
> >>>>>> "Sherm" == Sherm Pendley <(E-Mail Removed)> writes:

> >
> > Sherm> It *does* make a difference - if you don't know or understand what that
> > Sherm> difference is, or you don't specifically want the behavior specified by
> > Sherm> the &, you shouldn't use it.
> >
> > Or, for the opposite point of view, if you have:
> >
> > sub log { print STDERR localtime() . ": @_\n" }
> >
> > then you better darn well invoke it as:
> >
> > &log("my message");
> >
> > and *not*:
> >
> > log("my message"); # invokes built-in logarithm function
> >
> > And until you know all the perl built-ins, you should use &. And this
> > is what we teach in Learning Perl.
> >
> > Please don't be so quick to dismiss the value of the leading &.

>
> Fortunately the syntax highlighting in my editor of choice allows me to
> distinguish between built-in functions and user defined subroutines.
>
>


Ignoring the smiley for the moment, the problem with syntax highlighters
for Perl is that they aren't reliable. They tend to let you down
exactly in the marginal cases when you could use the help.

What's worse, sometimes a quirk in the syntax highlighter makes people
use non-obvious code because the highlighter can't cope with the obvious
solution.

Currently I'm still using vim's syntax highlighting only out of inertia.
One of these days I'm going to switch it off for good.

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
memory and subroutines Frank Silvermann C Programming 4 06-14-2006 09:47 PM
Multiple Page_Load subroutines and User Controls =?Utf-8?B?UGF1bA==?= ASP .Net 1 10-26-2005 07:06 PM
References Subroutines and Arrays Ketema Perl Misc 2 03-06-2004 01:49 PM
References and subroutines ReaprZero Perl 1 12-04-2003 02:53 PM
Style question regarding subroutines and lexical variables Joseph Ellis Perl Misc 6 07-24-2003 11:37 PM



Advertisments