\@ and \&

R

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?


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];
}
 
J

Joost Diepenmaat

Rose said:
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
 
R

Rose

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

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];
}
 
B

Ben Morrow

Quoth "Rose said:
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
 
J

Joost Diepenmaat

Rose said:
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].
 
J

Joost Diepenmaat

Rose said:
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.
 
B

Ben Morrow

Quoth "Rose said:
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
 
R

Rose

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.


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];
}
 
R

Rose

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

Dear Frank,

You are correct. I have to get back to study add_track().
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top