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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



rpaskudniak
User


May 5, 2013, 8:45 PM

Post #1 of 4 (632 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

Code
$a = \@some_list

I can type

Code
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

Code
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:

Code
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)


Laurent_R
Veteran / Moderator

May 5, 2013, 11:11 PM

Post #2 of 4 (630 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:


Code
$a = \@some_list 
x $a



BillKSmith
Veteran

May 6, 2013, 5:39 AM

Post #3 of 4 (624 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,
Bill


rpaskudniak
User


May 7, 2013, 8:17 PM

Post #4 of 4 (597 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:


Code
(MyNewClass::new(Math/Yapp.pm:174):        bless $self;              # Makes it an object 
DB<2> p $self
HASH(0x80617678)

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

Code
  DB<3> p $self 
Operation """": no method found, argument in overloaded package MyNewClass at (eval 14)[/usr/lib/perl5/5.14/perl5db.pl:640] 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:

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

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

Code
  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