On Thursday, Jun 5, 2003, at 13:07 US/Pacific, Andrew Brosnan wrote: [..]
my $hash = {};

while (my $rows = $response->fetchrow_hashref){
  $hash->{$rows->{task_ID}} = $rows;
}

Why is hash declared as my $hash and not my %hash? and how does the

This creates an annonymous hash '{}' and stores a reference to that hash
in $hash.


As for solving -
         when to use a reference to a hash,
        vice declaring a hash itself.

Some of it is merely 'art work' - but there is a useful
practical distinction, is one planning to
pass by 'value' or pass by 'reference' at
any time in the life of the variable. { and/or
all of the future refactorings of the code.... 8-) }

One can have a large hash, and if you pass it by value
then you have to put all of the key/value pairs onto the stack.
While if you passed merely a reference to the hash, you
will pass merely the singular reference itself.


I just hacked something where I was not sure what the structure would really need to look like, so opted to merely declare

        my $struct;
        ....
                $struct->{$sname} = "src: $src_file open error:\n\t $!";
        ....
        $struct->{$sname} = "copy of $src_file ok";
        ...

so technically the code does not WARN the coder that
I plan to use it as a reference to a hash.... but looking
at the various places where I used it, most perl coders
would understand that it was merely a hash.

I also do this sort of trick when I am planning to do
strange return solutions:

        sub some_funktion
        {
                ....
                return undef unless($condition_one);
                ....

return "that's not right" if $error_case ;

                $hash_ref;
        }

hence I can 'know' a bit about what went on with

my $got_back = some_funktion(@arglist);

        unless(defined($got_back)) {
                # condition_one not met
                ....
        }

        unless(ref($got_back)) {
                #
                # that's not right handle the error_case
                #
        }

        while(my ($k,$v) = each %$got_back))
        {
                #
                # here we deal with it just like it were a regular hash
                #
        }


HTH....


ciao
drieux

---


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



Reply via email to