I've written some Perl that I'm a little unsure of and wanted to bounce it of the Perl Monks.

The problem: I need to create member variable that is an array of objects that can be accessed by various other classes that will expect a particular type of object, in the example a URL object.

To illustrate I've created a couple of simple classes and a script to build a couple of URL objects, popular an array and then iterate the array printing out the results.

My question: Is this the best way to manage data in Perl OOP?

#!/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; }


In reply to How to store objects as arrays in other objects by halfbaked

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.