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

Discussion in 'Perl Misc' started by sln@netherlands.com, Nov 23, 2008.

  1. Guest

    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
    , Nov 23, 2008
    #1
    1. Advertising

  2. rthangam Guest

    On Nov 24, 12:35 am, wrote:
    > 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.
    rthangam, Nov 25, 2008
    #2
    1. Advertising

  3. rthangam <> wrote:
    > On Nov 24, 12:35 am, wrote:
    >> 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).


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Nov 25, 2008
    #3
  4. rthangam <> wrote:
    >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
    Jürgen Exner, Nov 25, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tfortney

    Ampersand in attribute

    tfortney, Apr 27, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    1,186
    tfortney
    Apr 28, 2004
  2. Prasanna Padmanabhan
    Replies:
    1
    Views:
    642
    Kevin Spencer
    Jul 8, 2004
  3. Alexander Grigoriev
    Replies:
    0
    Views:
    386
    Alexander Grigoriev
    Sep 12, 2003
  4. Replies:
    2
    Views:
    921
    Bengt Richter
    Aug 1, 2005
  5. Bob
    Replies:
    5
    Views:
    260
Loading...

Share This Page