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
Net::Amazon::EC2::Snapshot=HASH(0x4c1be90)->snapshot_id
Is there a simple way to print the value of the property inside a print?
Not in the way you want to do it. In fact, what you’re doing with
$snap->snapshot_idis calling a method (as insub). Perl cannot do that inside a double-quoted string. It will interpolate your variable$snap. That becomes something likeHASH(0x1234567)because that is what it is: ablessed reference of a hash.The interpolation only works with scalars (and arrays, but I’ll omit that). You can go:
There is one way to do it, though: You can reference and dereference it inside the quoted string, like I do here:
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: