Click here to get back home

\@ and \&

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
\@ and \& Rose 03-06-2008
---> Re: \@ and \& Joost Diepenmaa...03-06-2008
Posted by Rose on March 6, 2008, 9:01 pm
Please log in for more thread options
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 @},$f;
}

if ($sorted_features) {
$panel->add_track($sorted_features,
$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];
}




Posted by Joost Diepenmaat on March 6, 2008, 9:12 pm
Please log in for more thread options

> 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/

Posted by Rose on March 6, 2008, 9:24 pm
Please log in for more thread options
> 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 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) {
$panel->add_track($sorted_features,
$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];
}




Posted by Joost Diepenmaat on March 6, 2008, 9:36 pm
Please log in for more thread options

> Oh, thanks! It's "pass by reference"... But in my specific case that the
> subroutine "Label" is called in this way, is $sorted_features 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) {
> $panel->add_track($sorted_features,

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

$panel->add_track($sorted_features),
^-- 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/

Posted by Joost Diepenmaat on March 6, 2008, 9:42 pm
Please log in for more thread options

> 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/


Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap