Stop perl overload or memory address "address" link

I have a class that I created that overloads a statement ""

to print out a nice gated shape for an object that is readable by the user.

But now I would like to get the memory address, for example:

Some_class=HASH(0xb0aff98)

      

which I would normally do with print "$some_object"

if I hadn't already redefined the operator ""

.

Is there a way to get around the overridden method or, otherwise, just get the memory address of that object?

+3


source to share


2 answers


Use overload::StrVal($o)

.



use overload '""' => sub { "Hello, World!" };
my $o = bless({});
print($o, "\n");                     # Hello, World!
print(overload::StrVal($o), "\n");   # main=HASH(0x62d038)

      

+5


source


Two options:



  • Use overload::StrVal

    Common functions

    The package overload.pm

    provides the following publicly available features:

    • overload::StrVal(arg)

      Gives a string value arg

      as if there were no overloading. If you are using this to get the url of a link (useful for checking if two links are linking to the same thing), you might be better off using Scalar::Util::refaddr()

      which is faster.

  • Use Scalar::Util::refaddr()

    $addr = refaddr( $ref )

    If $ref

    is a reference, the internal memory address of the reference value is returned as a simple integer. Otherwise undef is returned.

       1.    $addr = refaddr "string";           # undef
       2.    $addr = refaddr \$var;              # eg 12345678
       3.    $addr = refaddr [];                 # eg 23456784
       4. 
       5.    $obj  = bless {}, "Foo";
       6.    $addr = refaddr $obj;               # eg 88123488
    
          

+3


source







All Articles