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

Dear colleagues,

I upgraded XML::LibXML from 1.x to 2.x and now all its objects return their textContent() when no methods are given.

I've got used to log object's class names with their unique identifiers, so now I can't just say something like: $log->trace("Information has been loaded as $dom");

ow to get object's name and ID?

Thank you!

V.Melnik

P.S. Updated:

sub mm_sprintify { my($message, @values) = @_; return($message) unless(@values); for(my $i = 0; $i < @_; $i++) { given($values[$i]) { when(undef) { $values[$i] = "< undef >"; } when(blessed($_)) { $values[$i] = sprintf("[%s\@0x%x +]", blessed($_), refaddr($_)); } when(ref($_) eq 'HASH') { $values[$i] = Dumper($_); } } } return(sprintf($message, @values)); }

So, now I say $log->debug("The %s %s is loaded as %s", $element_name, $element_type, $element_dom); and feeling quite happy.

  • Comment on How to use object's reference in "object" context if being used in scalar context it does something wrong?
  • Download Code

Replies are listed 'Best First'.
Re: How to use object's reference in "object" context if being used in scalar context it does something wrong?
by perlfan (Parson) on Jul 08, 2014 at 14:24 UTC
    To get a blessed reference's package name, you can use ref.

    To get an answer for your other questions, I think you need to post some more information.

Re: How to use object's reference in "object" context if being used in scalar context it does something wrong?
by Anonymous Monk on Jul 08, 2014 at 14:26 UTC

    ref should still get you the object class. I don't know how to get a unique identifier, but perhaps anothermonk does.

      As I've got to know, Scalar::Util can be a bit helpful with refaddr(). Not the best solution for me, though. :(
      V.Melnik

        Try the overloading pragma...

        use strict; use warnings; use XML::LibXML 2; my $doc = XML::LibXML->load_xml(IO => \*DATA); my $bar = $doc->getElementsByTagName('bar')->get_node(1); print $bar, "\n"; { no overloading; print $bar, "\n"; } print $bar, "\n"; __DATA__ <foo><bar /></foo>