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

Hi Wise monks.

When we print out an object, our beloved perl outputs something like that:

Class::HASH{0x73E448} .

Which is very usefull for machines, but not really for humans. Is there a way of overriding some kind of as_string method ? I looked in the UNIVERSAL module, but didn't find any :(

Thx for help !!

-- Nice photos of naked perl sources here !

Replies are listed 'Best First'.
Re: Overriding as_string ?
by cephas (Pilgrim) on Oct 02, 2006 at 14:26 UTC
    Look at the overload documentation (perldoc overload)

    Something like this
    use overload q("") => \&as_string fallback => 1;
Re: Overriding as_string ?
by wfsp (Abbot) on Oct 02, 2006 at 14:30 UTC
    Not sure if I understand the question correctly but I use this:
    use Data::Dumper; print Dumper $obj;
    Hope that helps.
Re: Overriding as_string ?
by Limbic~Region (Chancellor) on Oct 02, 2006 at 14:26 UTC
    jeteve,
    See ref and some of the functions provided by Scalar::Util. To toString() method from Java land doesn't do much more than that unless you overide it. You could always build your own here too but without overloading stringification you will have to specifically call the method.

    Cheers - L~R

Re: Overriding as_string ?
by johnnywang (Priest) on Oct 02, 2006 at 20:16 UTC
    and if you're inspecting the variable in the debugger (perl -d), use "x $foo", not "p $foo", it will print as nicely as Data::Dumper.