Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

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

Time for you to learn OOP!

{ package Person; use Moo; has name => (is => 'ro', required => 1); has age => (is => 'rw', required => 1); sub introduce_yourself { my $self = shift; printf("My name is %s, and I am %d years old.\n", $self->name, + $self->age); } } my $alice = Person->new(name => 'Alice', age => 32); $alice->introduce_yourself;

Note that we don't need to pass any $name and $age variables to introduce_yourself. It picks them up automatically from the $self object.

And because we've not used any globals, we can easily have two people living side by side. They each get their own copy of name and age and don't get them mixed up:

my $alice = Person->new(name => 'Alice', age => 32); my $bob = Person->new(name => 'Bob', age => 31); $_->introduce_yourself for $alice, $bob;

When two people are living side by side, they don't always remain just two people. Let's extend Person with the ability to breed more people...

{ package Person; use Moo; has name => (is => 'ro', required => 1); has age => (is => 'rw', required => 1); sub introduce_yourself { my $self = shift; printf("My name is %s, and I am %d years old.\n", $self->name, + $self->age); } } { package Breeding; use Moo::Role; has children => (is => 'ro', default => sub { return []; }); sub is_parent { my $self = shift; @{ $self->children }; } sub bear_children { my $self = shift; my ($partner, @kids_names) = @_; for my $name (@kids_names) { my $kid = ref($self)->new(name => $name, age => 0); push @{ $self->children }, $kid; push @{ $partner->children }, $kid; } return; } } { package Person::Breeding; use Moo; extends 'Person'; with 'Breeding'; } my $alice = Person::Breeding->new(name => 'Alice', age => 32); my $bob = Person::Breeding->new(name => 'Bob', age => 31); $_->introduce_yourself for $alice, $bob; $alice->bear_children($bob, 'Carol', 'Dave', 'Eve'); print "Bob is a father!\n" if $bob->is_parent;

PS: in the interest of pimping my new project Moops, here's that last example again, using Moops instead of plain Moo:

use Moops; class Person { has name => (is => 'ro', required => 1); has age => (is => 'rw', required => 1); method introduce_yourself () { printf("My name is %s, and I am %d years old.\n", $self->name, + $self->age); } } role Breeding { has children => (is => 'ro', default => sub { return []; }); method is_parent () { @{ $self->children }; } method bear_children ($partner, @kids_names) { for my $name (@kids_names) { my $kid = ref($self)->new(name => $name, age => 0); push @{ $self->children }, $kid; push @{ $partner->children }, $kid; } return; } } class Person::Breeding extends Person with Breeding; my $alice = Person::Breeding->new(name => 'Alice', age => 32); my $bob = Person::Breeding->new(name => 'Bob', age => 31); $_->introduce_yourself for $alice, $bob; $alice->bear_children($bob, 'Carol', 'Dave', 'Eve'); say "Bob is a father!" if $bob->is_parent;
use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

In reply to Re: data structure question by tobyink
in thread data structure question by Anonymous Monk

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 drinking their drinks and smoking their pipes about the Monastery: (13)
    As of 2014-07-31 14:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (249 votes), past polls