in reply to Iterating over *any* thing in Perl
Data::Dumper does a pretty good job.
#! perl -slw use strict; package scalarObj; my $scalar; sub new{ return bless \$scalar, shift; } package arrayObj; my @private = qw(1 a the 3.14159); sub new { return bless \@private, shift; } package hashObj; sub new { return bless {some=>1, things=>2, in=>3, a=>4, hash=>5 }, shift; } package main; use Data::Dumper; my @objects = ( new scalarObj, new arrayObj, new hashObj ); foreach my $obj (@objects) { print Dumper $obj; } __END__ C:\test>217134 $VAR1 = bless( do{\(my $o = undef)}, 'scalarObj' ); $VAR1 = bless( [ '1', 'a', 'the', '3.14159' ], 'arrayObj' ); $VAR1 = bless( { 'things' => 2, 'a' => 4, 'some' => 1, 'hash' => 5, 'in' => 3 }, 'hashObj' ); C:\test>
Writing your own code to do something similar isn't much harder.
#! perl -slw use strict; package scalarObj; my $scalar= 'test'; sub new{ return bless \$scalar, shift; } package arrayObj; my @private = qw(1 a the 3.14159); sub new { return bless \@private, shift; } package hashObj; sub new { return bless {some=>1, things=>2, in=>3, a=>4, hash=>5 }, shift; } package main; use Data::Dumper; my @objects = ( new scalarObj, new arrayObj, new hashObj ); foreach my $obj (@objects) { if ($obj =~ /scalar/) { print $$obj; } elsif($obj =~ /array/) { print "@{$obj}" } elsif($obj =~ /hash/) { print "$_ => $obj->{$_}" for keys %$obj; + } } __END__ C:\test>217134 test 1 a the 3.14159 things => 2 a => 4 some => 1 hash => 5 in => 3 C:\test>
Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Iterating over *any* thing in Perl
by Abigail-II (Bishop) on Dec 03, 2002 at 11:47 UTC | |
by BrowserUk (Patriarch) on Dec 03, 2002 at 13:37 UTC |