Öznur Tastan wrote:

> > >     &print_rusage($t);
> >
> > Don't do this.  The & operator is needed only in special cicumstances
>
> I was wondering when to use this &?

Not very often, really.  I used it recently, and I think that was the first time
in many thousands of function calls.  On place it is necessary is when
dereferencing a function ref [or it could be that I just haven't found a way to
accomplish that task without it.]  It was in code I posted a few days ago--let
me see if I can find it...Okay, the sitch here was that I was building a hash of
function references, as sample of how a swittchboard function could be set up.

sub get_crisis {
   my $crisis_tag = shift;
   my $crises = {
      'airlock open'  => \&raise_airlock_open_emergency,
      'antimatter volley' => \&raise_positron_emergency
   };
   return $crises->{$crisis_tag};
}

sub raise_airlock_open_emergency {
   my $name = shift;
   print "Airlock is open, $name\n";
}

sub raise_positron_emergency {
   my $name = shift;
   print "They're bombarding us with positrons, $name\n";
}

my $crisis = get_crisis ('airlock open');
&$crisis('Captain');
^Z
Airlock is open, Captain


 In this callback context, the & was necessary in order to tell the compiler to
dereference the reference scalar $crisis as a function.  When a function name is
declared in or impoted to the local namespace, there is no such need to specify
that the name is a reference to a subroutine.  Used in that context, it also
does other tweaky things with the argument list.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to