http://www.perlmonks.org?node_id=410848

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

Is there any way to bypass the overloading of an object's stringification? In other words, I want some way to see "SomeModule=HASH(0x1234567)" instead of whatever the module author decided I would want.

Thanks.

Replies are listed 'Best First'.
Re: Subverting overload
by BrowserUk (Patriarch) on Nov 29, 2004 at 03:05 UTC

    Yes. Call overload::StrVal(). From the POD:

    Package overload.pm provides the following public functions:

    overload::StrVal(arg)

    Gives string value of arg as in absence of stringify overloading

    ...


    Examine what is said, not who speaks.
    "But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
    "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      Thanks! I did perldoc overload, but I must have missed that.
Re: Subverting overload
by Zaxo (Archbishop) on Nov 29, 2004 at 03:05 UTC

    The ref function will tell you what class you're blessed into, if that's what you want.

    package Foo; use overload '""', sub {"Hello, World!"}; package main; my $foo = bless {}, 'Foo'; { local $, = ' '; print $foo, ref($foo), $/;</p> }
    Otherwise, you'll probably want to rebless into another class without the overloaded stringification. I can't think of a way to completely unbless an object.

    UpdateL BrowserUK++, I completely forgot about that.

    After Compline,
    Zaxo

Re: Subverting overload
by crouchingpenguin (Priest) on Nov 30, 2004 at 01:17 UTC
    In addition to BrowserUk's suggestion, you can use use overload "\"\"" => "my_print_method"; just as easily.

    cp
    ----
    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."