halfbaked has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w package URL; use strict; use Data::Dumper; sub new { my $class = shift; my $self = {}; $self = bless($self, $class); return $self; } sub load { my $self = shift; my %data = %{$_[0]}; $self->url($data{url}); $self->urlid($data{urlid}); } sub url { my $self = shift; if (@_) { $self->{url} = shift; } return $self->{url}; } sub urlid { my $self = shift; if (@_) { $self->{urlid} = shift; } return $self->{urlid}; } 1
#!/usr/bin/perl -w package Tester; use strict; use URL; use Data::Dumper; sub new { my $class = shift; my $self = {}; $self = bless($self, $class); return $self; } sub add { my $self = shift; my @urls; if ($self->{urls}) { @urls = @{$self->{urls}}; } else { @urls = (); } my $url = ${$_[0]}; push(@urls, $url); $self->{urls} = \@urls; } sub next { my $self = shift; my @urls = @{$self->{urls}}; if (!@urls) { return undef; } my $url = pop(@urls); $self->{urls} = \@urls; return $url; } 1
#!/usr/bin/perl -w use strict; use Tester; use URL; use Data::Dumper; my $test = Tester->new(); my $url = URL->new(); my %hash = ('urlid'=>2,'url'=>'http://www.perlmonks.org'); $url->load(\%hash); $test->add(\$url); my %hash2 = ('urlid'=>3,'url'=>'http://www.ire.org'); my $url2 = URL->new(); $url2->load(\%hash2); $test->add(\$url2); while (my $u = $test->next()) { printf "URL ID: %d URL: %s\n", $u->urlid, $u->url; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to store objects as arrays in other objects
by almut (Canon) on Dec 07, 2008 at 21:28 UTC | |
by halfbaked (Sexton) on Dec 07, 2008 at 22:26 UTC | |
|
Re: How to store objects as arrays in other objects
by kennethk (Abbot) on Dec 07, 2008 at 21:27 UTC | |
|
Re: How to store objects as arrays in other objects
by spmlingam (Scribe) on Dec 08, 2008 at 11:42 UTC | |
|
Re: How to store objects as arrays in other objects
by otto (Beadle) on Dec 08, 2008 at 05:31 UTC |