The trick is to use {$ref} where you would normally put the variable name:
So $array[0] becomes ${$ref}[0] (not @$ref[0] which is a "slice").
And $hash{key} becomes ${ref}{key}.
Or you can use the little arrow syntax...
${$ref}[0] is the same as $ref->[0].
And ${$ref}{key} is the same as $ref->{key}.
In your script you are doing a variation of the first, where you are just
dropping the curly braces. In which case it would look like this:
${$ref}[0] is the same as $$ref[0] (NOT @$ref[0]).
${ref}{key} is the same as $$ref{key}.
Hope that helps. Check out the perlreftut manpage, it explains all of this
pretty well while avoiding all of the nasty details of refs.
Rob
-----Original Message-----
From: Tor Hildrum [mailto:[EMAIL PROTECTED]]
Sent: Thursday, February 28, 2002 7:46 AM
To: Perl
Subject: Dereferencing a referenced hash.
Is this a bug or a feature?
Or is it just me? :)
!/usr/bin/perl -w
use strict;
use warnings;
my $variable = "variable";
my @array = qw(this is an array);
my %hash = qw(1 this 2 is 3 a 4 hash); ## correct syntax
my $rvariable = \$variable;
my $rhash = \%hash; ## correct syntax
my $rarray = \@array;
print "\$variable from reference: $$rvariable\n";
print "\@array from reference: @$rarray[0] @$rarray[1] @$rarray[2]
@$rarray[3]\n";
print "\%hash from reference: %$rhash{1} %$rhash{2} %$rhash{3}
%$rhash{4}\n"; ## correct syntax
[localhost:~/Perl/Advanced Perl] tor% ./1.pl
Global symbol "%rhash" requires explicit package name at ./1.pl line 16.
Global symbol "%rhash" requires explicit package name at ./1.pl line 16.
Global symbol "%rhash" requires explicit package name at ./1.pl line 16.
Global symbol "%rhash" requires explicit package name at ./1.pl line 16.
Execution of ./1.pl aborted due to compilation errors.
I simply can't see that anything is wrong with my syntax.
If I remove -w, use warnings; and use strict;
[localhost:~/Perl/Advanced Perl] tor% ./1.pl
$variable from reference: variable
@array from reference: this is an array
%hash from reference: % % % %
Anyone know why this is happening?
--
T.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]