CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
[Solved] Print references



May 5, 2013, 8:45 PM

Post #1 of 4 (1938 views)
[Solved] Print references Can't Post

Greetings again. Wow, twice in one day! Smile

As a curiosity that has come up in debugging, I would like to sometimes print the value of a reference, not the object it points to. When I'm int he debugger and I have a reference to an array, like

$a = \@some_list

I can type

p $a

and get a hex value of some sort. But when I try to do this for an object reference I get a complaint about no method. Now that message makes some sense because, normally, when when someone wants says

print $some_object

they *do* mean to display the contents of said object. (HMMmm, I haven't tried it for an unblessed hash.)

Similarly, if I want to see if to object references are referencing the same object, I might try:

if ($ref1 == $ref2) {..}

but I will get a very similar complaint, for the same reason as with the print command.

So how *do* I display or compare references as a scalar quantity?

Thanks much.
-- Rasputin Paskudniak (In perpetual pursuit of undomesticated, semi-aquatic avians)

(This post was edited by rpaskudniak on May 7, 2013, 8:20 PM)

Veteran / Moderator

May 5, 2013, 11:11 PM

Post #2 of 4 (1936 views)
Re: [rpaskudniak] Print references [In reply to] Can't Post

Under the debugger, you might try that to view the content of your list:

$a = \@some_list 
x $a


May 6, 2013, 5:39 AM

Post #3 of 4 (1930 views)
Re: [rpaskudniak] Print references [In reply to] Can't Post

Use the function refaddr in the module Scalar::Util (its in the standard library).
Good Luck,


May 7, 2013, 8:17 PM

Post #4 of 4 (1903 views)
Re: [BillKSmith] Print references [In reply to] Can't Post

Thanks to both Laurent and BillK; both of your solutions worked.

First a minor footnote: When I had just execute the <$self = {}; > I was able to immediately print the reference value:

(MyNewClass::new(Math/        bless $self;              # Makes it an object 
DB<2> p $self

However, after I bless $self, when I try the same p[rint] command. I get the error:

  DB<3> p $self 
Operation """": no method found, argument in overloaded package MyNewClass at (eval 14)[/usr/lib/perl5/5.14/] line 2.

(Note: I am substituting MyNewClass for the actual qualified class name; keeping that under wraps for now)

On the other hand, when I use refaddr, I can obtain:

  DB<2>  printf("0X%x\n", refaddr($self)) 

Nah, too much trouble. Try the x command, as suggested by Laurent:

  DB<4> x $self 
0 MyNewClass=HASH(0x80617678)
empty hash

And after I have put some data in there, the x command is perfect for an interactive debugging session; a far easier command to use than Data::Dumper->Dump([$self]); I may never use that again for debugging. Still, if all I needed to do was check the reference itself (like to make sure if I was overwriting a hash referenced by two reference variables) the refaddr() call is obviously the way to go.

Two great answers! Again, my gratitude!
-- Rasputin Paskudniak (In perpetual pursuit of undomesticated, semi-aquatic avians)


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives