Function calls using ampersand & in Perl, what is it?

S

sln

According to the doc's it enables the callee to see @_ of
the caller. Is there anything more insidius about this or is that
the extent of & ?

Example:

sub BeginSegStr {return ${&BeginSeg};}

sub BeginSeg
{
# return lvref of begining segment
my $str = "";
return \$str unless (defined $_[1] && ref($_[1]) eq 'SCALAR');
return \substr ${$_[1]}, 0, $_[0]->{'offset'};
}


sln
 
R

rthangam

According to the doc's it enables the callee to see @_ of
the caller. Is there anything more insidius about this or is that
the extent of & ?

Example:

sub BeginSegStr {return ${&BeginSeg};}

sub BeginSeg
{
        # return lvref of begining segment
        my $str = "";
        return \$str unless (defined $_[1] &&  ref($_[1]) eq 'SCALAR');
        return \substr ${$_[1]}, 0, $_[0]->{'offset'};

}

sln

Yeah only that is the difference. &foo() passes @_ to foo() subroutine
from the caller, but foo() doesn't pass it.
 
T

Tad J McClellan

rthangam said:
According to the doc's it enables the callee to see @_ of
the caller. Is there anything more insidius about this or is that
the extent of & ?

Example:

sub BeginSegStr {return ${&BeginSeg};}

sub BeginSeg
{
        # return lvref of begining segment
        my $str = "";
        return \$str unless (defined $_[1] &&  ref($_[1]) eq 'SCALAR');
        return \substr ${$_[1]}, 0, $_[0]->{'offset'};

}

sln

Yeah only that is the difference. &foo() passes @_ to foo() subroutine ^^
^^
from the caller, but foo() doesn't pass it.


No, &foo() circumvents prototypes.

&foo (no parenthesis) passes @_ to foo() subroutine...

.... which can lead to "action at a distance" which is very difficult to
debug (which is why using an ampersand when you don't need to is a Bad Idea).
 
J

Jürgen Exner

rthangam said:
Yeah only that is the difference. &foo() passes @_ to foo() subroutine
from the caller, but foo() doesn't pass it.

No, it is not. The ampersand overrides prototypes.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top