On Mon, Nov 26, 2001 at 05:54:24PM -0500, [EMAIL PROTECTED] wrote:

I've modified the original email, and moved things around, for clarity.

> #========================================================
> #!/usr/bin/perl
> use strict;
> #     IF I do not assign {}, nothing is printed
> my($hash) = {};       
> &getArray($hash);
[snip]
> a) When a hash reference is essentially a SCALAR, why should I explicitly
> denote the hash variable as
>       my($hash)       = {};
> and not simply
>       my($hash);

When you say getArray($hash), where $hash eq undef, you are effectively
passing $hash by value.  $hash is not a reference, and therefore your
assignment and manipulation of it inside getArray() isn't going to be
visible outside of the subroutine.

When you say getArray($hash), where $hash is a hash reference, you are now
passing by reference, and modifications to $hash inside the subroutine
affect the original hash you passed in.


[snip]
> #     IF I do not engulf array-ref within {} as @{$hash->{array}}
> #     an error message NOT AN ARRAY reference is generated
> map {print "$_\n"} @{$hash->{array}}; 
> 
> sub getArray{
>       my($hash)       = shift;
>       $hash->{array}  = [1,2,3,4,5];
> }
> #========================================================
[snip]
> b) I thought that an array can be dereferenced simply by @$arrayRef
> notation. However, in the code sample I have sent, I am forced to use
> @{$arrayRef} notation. Why do I need the Chain-braces?

It has to do with precedence.  When you say:

    print @$hash->{array}

you're basically saying:

    my @tmp = @$hash;
    print $tmp{array};

Which is, of course, incorrect, because you're accessing a hash, not an
array.  You have to add the braces to clear up the ambiguity; that you want
to dereference $hash->{array} as an array.

    
Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

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

Reply via email to