azaidi has asked for the wisdom of the Perl Monks concerning the following question:

This is from slashcode... Th last line of getSlashConf() is ... return *I{HASH}; ...I is a %. Later on, he does this... *I = getSlashConf(); getSlash(); my $op = $I{F}{op}; What the heck does $I{F}{op} mean? What is he doing? I just don't get it...

Replies are listed 'Best First'.
Re: I just can't figure this out...
by mikfire (Deacon) on Apr 06, 2000 at 17:43 UTC
    Are you familiar with globs? Skip the next paragraph if you are and I will answer the question.

    You are familiar with the concept that we can have $I, @I, %I, sub I {}, and a file handle called I? A glob ( indicated by the *I ) is a convenient way of referring to all of them at once.

    Basically, the *I{HASH} construct means we are only referring to the HASH part if the glob. When the assignment is done ( *I = getSlashConf() ), we are certain that only the HASH portion of the glob will be reassigned. After the assignment, %I now refers to whatever getSlashConf returns and we can treat it like a normal hash, or in this case, a hash of hashes. It is a clever way of returning a reference without returning a reference.

    If you are not familiar with globs, get yourself a copy of Conway's Object Oriented Perl, because he explains what globs are, how they work and what this kind of assignment is doing in a very clear and concise way.

    Mik
    Mik Firestone ( perlus bigotus maximus )

Re: I just can't figure this out...
by chromatic (Archbishop) on Apr 06, 2000 at 18:55 UTC
    It's a Perl 4 relic in a shiny new Perl 5.6 world. (As Perl 5 came out in 1994, well, there's not much excuse to do things this way.)

    What would be better is returning a reference to a hash from getSlashConf(). That code might look something like this:

    my $l = getSlashConf(); getSlash(); my $op = $l->{F}{op};
    In getSlashConf(), that last line should probably change to: return $l{HASH}; In my opinion, this makes it more clear that we are dealing with a hash of hashes data structure. (All that means is that the value of a hash is actually a reference to another hash, instead of a scalar.)

    For more information on references, see perlman:perlref. For more information on complex data structures, see perlman:perldsc.

Re: I just can't figure this out...
by btrott (Parson) on Apr 06, 2000 at 20:07 UTC
    W/r/t the question about what's $I{F}{op}--are you familiar with multidimensional data structures? This looks to be a hash of hashes, in this case. Perl implements multidimensional data structures using references--so you have a regular hash, %I, with a key called "F"; and the value of that key is a reference to another hash (a reference is a scalar, and a hash value can only be a scalar, so that works out).

    So you could say:

    my $hash_ref = $I{F};
    Now you've got a hash ref in $hash_ref, and you can dereference it to see what it holds. You could now say:
    my $op = $hash_ref->{'op'};
    So, if we put those together and eliminate the temporary variable, we get
    my $op = $I{F}->{op};
    which is the same as
    my $op = $I{F}{op};
    because Perl lets you eliminate the dereferencing arrows between brackets subscripts.

    Take a look at perlref, perldsc, and perllol.