Thanks Rob, this has been very helpful. I wanted to know why the second
form is an abuse of perl. Since I'm new to perl, it is helpful to kn ow
what is good programming practise.

Tx.
H
 On Tue, 2003-06-10 at 15:39, Rob Dixon wrote:
> Hamish Whittal wrote:
> > Hi All,
> >
> > I'm still a little confused...
> > What is the difference between
> >  %{$config_file->{$key}}
> 
> Here, $config_file is a hash reference. If you print it it will
> display something like
> 
>   HASH(0x12345678)
> 
> The hash it refers to is accessed by the value in $key, so if
> you had
> 
>   my $config_file = { K => { K2 => 'V2' } };
>   my $key = 'K';
> 
> then
> 
>     $config_file->{$key}
> 
> would be equal to the anonymous hash { K2 => 'V2' }.
> 
> This value is then dereferenced again as a hash, so if
> we have
> 
>   my %hash = %{$config_file->{$key}};
> 
> then
> 
>   $config_file->{$key}
> 
> %hash would be set to
> 
>   %hash = ( K2 => V2 );
> 
> 
> 
> >  %$config_file->{$key}
> 
> Now this is something altogether different. You've
> discovered that you can use hash names as references,
> and this is an abuse of Perl and not to be encouraged!
> 
> If you had
> 
>   my %config = ( K => { K1 => 'V1' } );
>   my $config_file = \%config;
> 
> then %$config_file is the same as %config, so you can access
> the element with
> 
>   $config{K}
> or
>   $config_file->{K}
> 
> What the book doesn't tell you, though, is that Perl will
> let you treat the hash name as a reference like this
> 
>   %config->{K}
> 
> which is the same as
> 
>   %$config_file->{K}
> 
> So you've dereferenced $config_file and then used it as a reference,
> so achieving nothing.
> 
> To summarize:
> 
>   %$config_file->{$key}
> 
> is the same as
> 
>   $config_file->{$key}
> 
> and so is the same as the first case, %{$config_file->{$key}}
> without the additional level of dereferencing.
> 
> But, as I said, don't use the second case!
> 
> Cheers,
> 
> Rob
> 
> 
-- 
You are not defined by what loves you, but what you love - Nicholas Cage
(Adaptation, 2003)
---
Hamish Whittal
QED Technologies cc
+27 82 803 5533


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to