in reply to dereferencing hash & array refs

Besides the optimization that pg presents (that of passing a ref to the array into the subs and not returning lists), there is danger that you want to consider. That being that ref $foo eq "TYPE" is a dangerous test. It will work with plain vanilla data structures, but will choke if there are blessed refs involved. UNIVERSAL::isa($foo,'TYPE') is safer as it doesnt suffer from this problem.

Also a minor technicality, there are more forms of SCALAR ref than just SCALAR. REF is for instance a scalar (it is a reference to a scalar that holds a reference itself), as is GLOB.. By this i mean that they are dereferenced in the same way that a scalar is. The special SCALAR ref type 'Regexp' will incidentally pass the UNIVERSAL::isa(qr//,"SCALAR") test but is handled very differently (but thats a can of worms you probably want to avoid :-)

my $scalar=\"Foo"; my $REF=\$scalar; my $GLOB=\*A; print "$$scalar $scalar $$REF $$GLOB\n"; __END__ Foo SCALAR(0x1ab3108) SCALAR(0x1ab3108) *main::A

--- demerphq
my friends call me, usually because I'm late....

Replies are listed 'Best First'.
Re: Re: dereferencing hash & array refs
by parv (Parson) on Feb 09, 2003 at 21:36 UTC

    Thanks for bringing up a very good point about various SCALAR types of references which i hadn't given much thought before.

    And i am aware of the cost of passing complete lists (arrays and hashes) and long strings to/from a sub. I am/was not concerned much about that for the particular program.