Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsDiscussion Forum → Quick Perl question
Quick Perl question
2009-04-18, 10:20 PM #1
I am not a particularly strong Perl programmer, so forgive me if this is a stupid question.

What does @$ notation mean, exactly? For instance, if I wrote

Code:
for (@$foo) {
# do stuff
}
How is this any different than a regular array (@ by itself)? E.g.:

Code:
for (@foo) {
# do stuff
}


I'm seeing @$ used in several places in code that I have to maintain and I can't seem to figure out how it's different from a regular list. I thought it might mean "use the array if it is an array and the scalar if not," but that wouldn't make any sense because of Perl's ability to have an array and a scalar be named the same but be two different things. It's also at odds w/ another example of its use like this:
Code:
$count = @$foo


Which is normally supposed to get the index count of the array, which seems to imply that this has to be an array of some sort.

Any help is appreciated. This is hard to google since google omits special symbols like @ and $ -_-
一个大西瓜
2009-04-18, 10:33 PM #2
Never mind, figured it out.

For those who are curious, it's what you do when you create a list/array reference and you want to access its contents (value(s)) later.
一个大西瓜
2009-04-19, 11:27 AM #3
Its an arrayref dereference. You can do the same thing with hashrefs;

Code:
my $hashref = { foo => 'bar', bar => 'foo' };
foreach my $key (keys %$hashref)
{
  printf "%s => %s\n", $key, $hashref->{$key};
}


See perlref for more info
And when the moment is right, I'm gonna fly a kite.
2009-04-19, 3:10 PM #4
OCaml > Perl :P

References, parameter passing, and classes in Perl always seemed like an afterthought to me. The syntax is kind of weird and it seems to confuse the hell out of people (though I never had a problems with it).
[This message has been edited. Deal with it.]
2009-04-19, 7:14 PM #5
I'll buy parameter passing and classes in perl seem whack, although they're extremely powerful for dynamic programming. But I always through perl's references totally rocked (being their own data type). Perhaps all the different ways of dereferencing things can get odd.

Speaking of perl, my favorite is the hash slice.

my @wanted = @hash{qw/foo bar baz/};
2009-04-19, 7:27 PM #6
Originally posted by Brian:
But I always through perl's references totally rocked (being their own data type). Perhaps all the different ways of dereferencing things can get odd.


That's what I was getting at. It is a great idea with a somewhat hokey implementation. Perl definitely suffers from having too many ways to do the same thing. Having choices is nice, but too many choices seems like a recipe for disaster.
[This message has been edited. Deal with it.]
2009-04-21, 11:31 AM #7
Code:
my $hashref = {};

%$hashref
%{$hashref} # Really more useful for more complex dereferencing
$hashref->{'some_key'}

my $arrayref = [];

@$arrayref
@{$arrayref}
$arrayref->[2]


In perl6, I think you will always use the same sigil, which won't affect references so much as real arrays/hashes:
Code:
my @array = qw(zero one two three four five);

print $array[2]; # prints "two" in perl5
say @array[2]; # prints "two" in perl6


(still not convinced perl6 will be much better...)

↑ Up to the top!