Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I know object-oriented programming in that I can use others' modules and I've written some of my own. I've read perlobj and perlootut. However, most of my objects have been simply nested Perl data structures (e.g., HoH, HoAoH, etc.). I have no problem creating or accessing those types of objects.

I'm keen to learn more about how to do this more "?correctly?". So I started looking into Class::Struct since it's simple, for building classes and in Perl core.

Take the following code borrowed heavily from the examples in the Class::Struct perldoc:

use strict; use warnings; package Cat; use Class::Struct; struct (name => '$'); 1; package Litter; use Class::Struct; struct (cats => '@'); 1; package main; my $cat1 = Cat->new(name=>'Garfield'); my $cat2 = Cat->new(name=>'Felix'); my $litter = Litter->new(cats => [$cat1, $cat2]); for (@{$litter->cats}) { print $_->name . "\n" }

This works swimmingly until $litter is created like so:

my $litter = Litter->new(cats => [$cat1, 1]);

Which of course produces an error ...

Can't call method "name" without a package or object reference at ...

... because "1" doesn't have a method - in fact, it's not even an "object". What I really 'think' I need to do is define the Litter object like so:

package Litter; use Class::Struct; struct (cats => '@Cat'); 1;

Meaning the 'cats' method should contain an array of 'Cat' objects, not just an array of anything. The documentation for Class::Struct leads me to believe I can't do it.

Questions:

  1. Is the 'workaround' to override the accessor for 'cats' as shown in Example 2 of the Class::Struct perldoc to check that the value passed is an object of type 'Cat'?

  2. Sticking with Class::Struct, is there a 'better' way to define the data structure / classes / objects I'm looking for (Litter 'cats' = array of 'Cat') such that I avoid the '@Cat' definition which isn't legal?

  3. Have I already 'outgrown' the usefulness of Class::Struct and simply that it's in Perl core is not the best reason to use it?


In reply to Perl OO with Class::Struct by VinsWorldcom

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (8)
    As of 2014-09-19 21:17 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (147 votes), past polls