0

I'm new to Perl, and this thing has got me stuck for far too long...

I want to dump a readable representation of the object itself from inside a function (I'm trying to debug something, and I'm doing this by returning an array reference which the caller expects, but containing an object dump rather than human readable text as per normal) so in my package I have:

use Data::Dumper;

sub somefunctionName{

my $self = shift;
my $d = Dumper($self);
my @retval = ();
push(@retval, $d);
return \@retval;
}

This is giving me the error "Can't use string ("the literal object dump goes here") as a HASH ref while "strict refs" in use"

I can't for the life of me figure out a way to make the error go away, no matter how I mess with the backslashes, and what I've done above looks to me like exactly what every online tutorial does... But I'm obviously missing the point somewhere.

What am I doing wrong?

9
  • Does: sub somefunctionName{ my $self = shift; return Dumper($self); } and my @list = somefunctionName(\&somefunctionName); not do the job? The array-ness is more determined by the recipient (@list in my example) than in the sub itself. Commented May 27, 2014 at 2:07
  • The code you are showing does not look like it would give the error you say it does. Perhaps you need to create some code that can reproduce the problem? Commented May 27, 2014 at 2:16
  • I'm unfortunately not in control of the code that calls this function, I'm adding some functionality to Request Tracker for my company's use. I've looked through its source code and I can't find an explicit call to it anywhere, but it clearly gets called :/ Commented May 27, 2014 at 2:51
  • 1
    @SamSvenbjorgchristiensensen It is unfortunate that you cannot provide more clarity. The code you have provided looks like it should work, in the sense that it should return a string version of $self (that obviously cannot be used as a reference). Without more to go on, there really isn't anything left to do buy guess. Commented May 27, 2014 at 13:22
  • 1
    Yes, it wraps an empty array around your single string, which looks something like $VAR1 = { stuff ... }. Your problem is like Chris said that you then try to use this value as a hash, when it is not a hash, with something like $foo->[0]{key}. Which gives the warning you mentioned, Can't use string .. as HASH ref ... Commented May 27, 2014 at 23:50

1 Answer 1

1

According to the documentation

  • Dumper(LIST)

    Returns the stringified form of the values in the list, subject to the configuration options below. The values will be named $VAR n in the output, where n is a numeric suffix. Will return a list of strings in a list context.

You should be able to do

@retval = Dumper($self);
return \@retval
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.