\@ and \&

Discussion in 'Perl Misc' started by Rose, Mar 7, 2008.

  1. Rose

    Rose Guest

    Yesterday I was taught that I can use

    \@array to refer an array and it does not make a copy of itself.

    Indeed i don't quite understand what it means.

    Today, I encounter another mysterious symbol \&, could anybody tell me what
    \ and & are for?


    my %sorted_features;
    for my $f (@features) {
    my $tag = $f->primary_tag;
    push @{$sorted_features{$tag}},$f;
    }

    if ($sorted_features{obj}) {
    $panel->add_track($sorted_features{obj},
    $panel->add_track(-label => \&Label,);
    }

    sub Label
    {
    my $feature = shift;

    my @notes;
    foreach (qw(product result))
    {
    next unless $feature->has_tag($_);
    @notes = $feature->each_tag_value($_);
    last;
    }
    $notes[0];
    }
    Rose, Mar 7, 2008
    #1
    1. Advertising

  2. "Rose" <> writes:

    > Yesterday I was taught that I can use
    >
    > \@array to refer an array and it does not make a copy of itself.
    >
    > Indeed i don't quite understand what it means.
    >
    > Today, I encounter another mysterious symbol \&, could anybody tell me what
    > \ and & are for?


    \&something, \@something, \%something and \$something create references
    to things.

    References are "pointers" to data (or code). One of the ways that's
    useful is that references can be much smaller than the data they point
    at, so they are more efficient if you want to use the same data in
    different parts of your program: instead of passing 10 Mb of data, you
    just pass a reference that says: "look at that data over there".

    Another way that they're useful is that all references that refer to the
    same data really refer to the same data, while if you pass the data
    around you generally create copies. Yet another useful feature of
    references is that they allow you construct nested data structures.

    You'll want to take a look at the perlreftut and perlref manpages.

    To answer your particular question:

    \ is the operator that takes a reference to whatever follows, and & is
    the subroutine sygil. & isn't used much nowadays, but you still need it
    in a few cases, and this is one of them:

    sub some_subroutine {
    print "My arguments are: @_\n";
    }

    my $ref = \&some_subroutine; # take a referent to some_subroutine

    $ref->(1,2,3,4); # call the subroutine referred to by $ref.

    HTH
    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Mar 7, 2008
    #2
    1. Advertising

  3. Rose

    Rose Guest

    > To answer your particular question:
    >
    > \ is the operator that takes a reference to whatever follows, and & is
    > the subroutine sygil. & isn't used much nowadays, but you still need it
    > in a few cases, and this is one of them:
    >
    > sub some_subroutine {
    > print "My arguments are: @_\n";
    > }
    >
    > my $ref = \&some_subroutine; # take a referent to some_subroutine
    >
    > $ref->(1,2,3,4); # call the subroutine referred to by $ref.
    >
    > HTH
    > --
    > Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/


    Oh, thanks! It's "pass by reference"... But in my specific case that the
    subroutine "Label" is called in this way, is $sorted_features{obj} passed
    and $notes[0] returned? The problem is that I'd like to call Label (the
    codes of Label can't be modified) but don't know how to receive its outputs.

    if ($sorted_features{obj}) {
    $panel->add_track($sorted_features{obj},
    $panel->add_track(-label => \&Label,);
    }

    sub Label
    {
    my $feature = shift;

    my @notes;
    foreach (qw(product result))
    {
    next unless $feature->has_tag($_);
    @notes = $feature->each_tag_value($_);
    last;
    }
    $notes[0];
    }
    Rose, Mar 7, 2008
    #3
  4. Rose

    Ben Morrow Guest

    Quoth "Rose" <>:
    > Yesterday I was taught that I can use
    >
    > \@array to refer an array and it does not make a copy of itself.
    >
    > Indeed i don't quite understand what it means.
    >
    > Today, I encounter another mysterious symbol \&, could anybody tell me what
    > \ and & are for?


    \ takes a reference. &Label refers to the sub Label in the current
    package. I believe I've already pointed you at perldoc perlreftut: it
    explains things better than I could hope to here. Post again if you
    still have trouble after reading that.

    Ben
    Ben Morrow, Mar 7, 2008
    #4
  5. "Rose" <> writes:

    > Oh, thanks! It's "pass by reference"... But in my specific case that the
    > subroutine "Label" is called in this way, is $sorted_features{obj} passed
    > and $notes[0] returned?


    Yes. But you've got some syntax problems.

    > The problem is that I'd like to call Label (the
    > codes of Label can't be modified) but don't know how to receive its
    > outputs.


    Well, what do you want to do with it?

    >
    > if ($sorted_features{obj}) {
    > $panel->add_track($sorted_features{obj},


    This passes $sorted_features{obj} to the add_track method. You're also
    missing a closing parenthesis here (or later): you probably want

    $panel->add_track($sorted_features{obj}),
    ^-- close parenthesis

    > $panel->add_track(-label => \&Label,);


    this passes a reference to the Label subroutine to the add_track
    method. I'm not sure if that's what you want. In any case, this
    construct does *not* call the Label subroutine (though add_track may
    call it later).


    > sub Label
    > {
    > my $feature = shift;
    >
    > my @notes;
    > foreach (qw(product result))
    > {
    > next unless $feature->has_tag($_);
    > @notes = $feature->each_tag_value($_);
    > last;
    > }
    > $notes[0];
    > }


    Whenever (if ever) Label is called, it will return $notes[0].


    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Mar 7, 2008
    #5
  6. "Rose" <> writes:

    > Oh, thanks! It's "pass by reference"


    I forgot to add this, but while this is more or less correct, perl
    doesn't have the simple-minded view of references that most languages
    that use the phrase "pass by reference" have. /Especially/ not when
    dealing with references to subroutines.

    Really, read perlreftut and perlref.

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Mar 7, 2008
    #6
  7. Rose

    Ben Morrow Guest

    Quoth "Rose" <>:
    >
    > Oh, thanks! It's "pass by reference"...


    Well, yes, literally speaking. However, 'pass by reference' is more
    usually used to indicate that the called sub modifies its parameters,
    and is able to do so because they were passed by reference; that is not
    the case here. Passing a subref is simply the only way to pass a sub
    value into another sub.

    [code moved up here, for clarity]
    > if ($sorted_features{obj}) {
    > $panel->add_track($sorted_features{obj},

    ^^
    I presume this is a typing error, and that line was supposed to end with
    ');'?

    > $panel->add_track(-label => \&Label,);
    > }
    >
    > sub Label
    > {
    > my $feature = shift;
    >
    > my @notes;
    > foreach (qw(product result))
    > {
    > next unless $feature->has_tag($_);
    > @notes = $feature->each_tag_value($_);
    > last;
    > }
    > $notes[0];
    > }


    > But in my specific case that the subroutine "Label" is called in this
    > way, is $sorted_features{obj} passed and $notes[0] returned?


    Label returns $notes[0], yes. Whether, when, and with what arguments
    Label is called, and what happens to that return value, are completely
    under the control of the $panel object: that's the point of passing it
    in, so the object can call it if it wants to. In this case I'd think
    it's unlikely it would be called with $sorted_features{obj}, but I
    couldn't tell without knowing what sort of object $panel is.

    > The problem is that I'd like to call Label (the codes of Label can't
    > be modified) but don't know how to receive its outputs.


    I'm not sure exactly what you are trying to achieve, here. You can call
    Label, yourself, with any arguments, and do what you like with the
    return value; this is completely independant of the fact you have also
    passed it into $panel->add_track. Can you post a *short* complete script
    we can all run, and explain how it isn't doing what you want?

    Ben
    Ben Morrow, Mar 7, 2008
    #7
  8. Rose

    Rose Guest

    > Label returns $notes[0], yes. Whether, when, and with what arguments
    > Label is called, and what happens to that return value, are completely
    > under the control of the $panel object: that's the point of passing it
    > in, so the object can call it if it wants to. In this case I'd think
    > it's unlikely it would be called with $sorted_features{obj}, but I
    > couldn't tell without knowing what sort of object $panel is.
    >
    >> The problem is that I'd like to call Label (the codes of Label can't
    >> be modified) but don't know how to receive its outputs.

    >
    > I'm not sure exactly what you are trying to achieve, here. You can call
    > Label, yourself, with any arguments, and do what you like with the
    > return value; this is completely independant of the fact you have also
    > passed it into $panel->add_track. Can you post a *short* complete script
    > we can all run, and explain how it isn't doing what you want?
    >
    > Ben
    >


    Although Label returns $notes[0], but it in turn depends on
    $feature->each_tag_value($_); from where $feature is "shifted" in. I'm
    unable to post a short complete script here because reproducing the
    hierarchy of the data is impossible. Therefore my focus is try to understand
    how to use the codes invented instead of reinventing the wheel.

    That's the reason why I care about what is really passed into Label because
    I have to ensure my object is in the correct structure to pass. In this
    case, is $sorted_features{obj} shifted into Label? Or it depends on
    add_track behaviour?

    if ($sorted_features{obj}) {
    $panel->add_track($sorted_features{obj}, -label => \&Label,);
    }

    sub Label
    {
    my $feature = shift;

    my @notes;
    foreach (qw(product result))
    {
    next unless $feature->has_tag($_);
    @notes = $feature->each_tag_value($_);
    last;
    }
    $notes[0];
    }
    Rose, Mar 7, 2008
    #8
  9. Rose

    Rose Guest

    > It's impossible for us to know what your classes do.
    > Why don't you look at the source code, preferably of add_track()?
    >
    > Frank
    > --
    > Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
    > Anwendungen für Ihr Internet und Intranet
    > Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel


    Dear Frank,

    You are correct. I have to get back to study add_track().
    Rose, Mar 7, 2008
    #9
    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. Nut Cracker
    Replies:
    1
    Views:
    338
  2. jakk
    Replies:
    4
    Views:
    12,143
  3. Tim Meagher
    Replies:
    8
    Views:
    1,289
    Karl Seguin
    Jun 18, 2005
  4. Jerry Manner
    Replies:
    2
    Views:
    6,544
    SpaceGirl
    May 30, 2005
  5. titi

    if and and vs if and,and

    titi, Mar 9, 2007, in forum: VHDL
    Replies:
    4
    Views:
    582
    Mike Treseler
    Mar 11, 2007
Loading...

Share This Page