Hi perlbeginner10, apart from davido's good post your question has had the misfortune to only receive replies from our Anonymous Troll (whose posts have been deleted by now), so I thought I'd add some more detail just so you know the content/noise ratio here is usually quite high.

The decision whether to create an object to store a given data structure ultimately boils down to one question IMO: Do you want your data structure to just contain data or should it contain data-specific behaviour as well? As davido rightly pointed out, you can create a data structure of arbitrary complexity using a hash (which in turn contains references to other data structures etc.). For example, the requirement you give in your post (a string and an array in a structure) can be fulfilled like this:

my %ruler = ( title => "King", fiefdoms => ["Narnia","Gondor","Scotland"] );

(The [] brackets around the fiefdoms create an anonymous array, see perldoc perldsc for more on this.)

If this is all you want, fine, no need for an object. But if you also want the data structure to be able to exhibit some behaviour, for example

$ruler->list_fiefdoms();

You'd implement an object, e.g. (this is a bare-bone skeleton object only and you almost certainly want to do this differently):

package Ruler; sub new { bless $_[1],$_[0]; } sub list_fiefdoms { my $self = shift; print join(",",@{$self->{fiefdoms}})."\n"; } 1;

Now you could create the object with

use Ruler; my $ruler = Ruler->new({ title => "Tyrant", fiefdoms => ["Mordor","Giedi Prime","Texas"] + }); $ruler->list_fiefdoms();

And it would print the list. Note this still implements the underlying data structure as an (anonymous) hash, the only difference is that you now also have behaviour coupled with the structure which can manipulate the contained data.

Hope that helps.


There are ten types of people: those that understand binary and those that don't.

In reply to Re: class or structure in Perl? by tirwhan
in thread class or structure in Perl? by perlbeginner10

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.