Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Iterating over *any* thing in Perl

by BrowserUk (Pope)
on Dec 03, 2002 at 06:48 UTC ( #217142=note: print w/replies, xml ) Need Help??


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
    Data::Dumper does a pretty good job.

    No it doesn't. It'll fail on:

    package Pet; my (%colour, %name, %species); sub new {bless [] => shift} sub colour : lvalue {$colour {+shift}}; sub name : lvalue {$name {+shift}}; sub species : lvalue {$species {+shift}}; sub DESTROY { my $self = shift; delete $colour {$self}; delete $name {$self}; delete $species {$self}; } 1; __END__

    There's no way you can get to the attributes from the outside, other than using the interface. And that's a feature, not a bug.

    Abigail

      Agreed. I guess you'd need to use Padwalker to get at those.

      And that's a feature, not a bug

      Also agreed. That's a much better way to implement private data.


      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://217142]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2021-05-18 08:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (176 votes). Check out past polls.

    Notices?