Take the 2-minute tour ×

I'm playing a bit with the Net::Amazon::EC2 libraries, and can't find out a simple way to print object properties:

This works:

my $snaps = $ec2->describe_snapshots();
foreach my $snap ( @$snaps ) {
  print $snap->snapshot_id . " " .  $snap->volume_id . "\n";

But if I try:

 print "$snap->snapshot_id $snap->volume_id \n";

I get


Is there a simple way to print the value of the property inside a print?

share|improve this question
add comment

5 Answers

up vote 4 down vote accepted

Not in the way you want to do it. In fact, what you're doing with $snap->snapshot_id is calling a method (as in sub). Perl cannot do that inside a double-quoted string. It will interpolate your variable $snap. That becomes something like HASH(0x1234567) because that is what it is: a blessed reference of a hash.

The interpolation only works with scalars (and arrays, but I'll omit that). You can go:

print "$foo $bar"; # scalar
print "$hash->{key}"; # scalar inside a hashref
print "$hash->{key}->{moreKeys}->[0]"; # scalar in an array ref in a hashref...

There is one way to do it, though: You can reference and dereference it inside the quoted string, like I do here:

use DateTime;
my $dt = DateTime->now();
print "${ $dt->epoch }"; # both ways
print "@{[$dt->epoch]}"; # work

But that looks rather ugly, so I would not recommend it. Use your first approach instead!

If you're still interested in how it works, you might also want to look at these Perl FAQs:

From perlref:

Here's a trick for interpolating a subroutine call into a string:





share|improve this answer
And if I'd remembered that it was a method call rather than a property, I might have figured it out - thanks for the reminder. –  chris Nov 5 '12 at 22:02
You're welcome @chris. =) –  simbabque Nov 6 '12 at 8:34
add comment

$snap->volume_id is not a property, it is a method call. While you could interpolate a method call inside a string, it is exceedingly ugly.

To get all the properties of an object you can use the module Data::Dumper, included with core perl:

use Data::Dumper;
print Dumper($object);
share|improve this answer
add comment

Stick with the first sample you showed. It looks cleaner and is easier to read.

share|improve this answer
add comment

The problem is that $snap is being interpolated inside the string, but $snap is a reference. As perldoc perlref tells us: "Using a reference as a string produces both its referent's type, including any package blessing as described in perlobj, as well as the numeric address expressed in hex."

In other words, within a string, you can't dereference $snap. Your first try was the correct way to do it.

share|improve this answer
add comment

I'm answering this because it took me a long time to find this and I feel like other people may benefit as well.

For nicer printing of objects use Data::Printer and p():

use DateTime;
use Data::Printer;
my $dt = DateTime->from_epoch( epoch => time );
share|improve this answer
add comment

Your Answer


