in reply to Marking an array as already seen

Yes, "$ref" is always the same if $ref= \@array; for the same array. Just go ahead and use it as the key in a hash. This is how Tie::RefHash is able to work.

You can even use $hash{0+$ref} which is probably faster.

                - tye
  • Comment on Re: Marking an array as already seen (yes)

Replies are listed 'Best First'.
Re: Re: Marking an array as already seen (yes)
by John M. Dlugosz (Monsignor) on Jan 18, 2003 at 01:41 UTC
    OK, why is {0+$ref} faster? I can see the different semantics... $hash{$ref} converts the ref to a string, while $hash{0+$ref} converts the ref to a number and then to a string. A quick test shows that converting a ref to a number returns (no surprise) a number, probably the machine address of an underlying unique piece of data in the interpreter, the same as the hex value shown in the stringification form.

    So why would the latter be faster?

    —John

      Just a tiny bit faster, but really "simpler" might have been a better description. It won't get confused if someone (re)blesses the array, for example. It will (probably, as I said) be slightly faster because it doesn't even have to look up blessedness and put that into the string and it will use a shorter string. *shrug*

      It was a quick note I added at the end. I prefer 0+$ref over "$ref" in general. But the speed isn't really the point. Just not doing extra work that might get in the way.

                      - tye

        Scalar::Util's refaddr (assuming the XS version) is slightly faster still, and doesn't suffer from possible problems with overloaded objects :-)