Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Perl Moose, basics

by Nar (Novice)
on Dec 10, 2015 at 22:57 UTC ( [id://1149965]=perlquestion: print w/replies, xml ) Need Help??

Nar has asked for the wisdom of the Perl Monks concerning the following question:

Moving into OOP Perl for the first time. I've always stayed away from this, then I ran into Moose and thought to give it a try. So in my first step moving this direction I thought I would ask the Monks to help me find out how to:
- Add the small blue table to the party once the party has started.
- Once all the tables are at the party, how do I see if I have a red table?

#Party.pm { package Party; use Moose; has 'table' => ( is => 'rw', isa => 'ArrayRef[Table]' ); } { package Table; use Moose; has 'size' => ( is => 'rw', isa => 'Str' ); has 'color' => ( is => 'rw', isa => 'Str' ); } -------- #goToParty.pl use Party.pm #BIG RED TABLE my $table1 = Table->new(size => 'big', color => 'red'); my $party = Party->new( tables => [ $table1] ); #SMALL BLUE TABLE my $table2 = Table->new(size => 'small', color => 'blue');

Replies are listed 'Best First'.
Re: Perl Moose, basics
by jeffa (Bishop) on Dec 10, 2015 at 23:10 UTC

    You give the Party class a method that looks for a red table:

    sub has_red_table { my $self = shift; for (@{ $self->tables }) { return 1 if $_->color eq 'red'; } return 0; }
    Homework: implement this method using grep

    Also, note that you have a serious typo: "table" is not "tables" ... but Moose should catch that for you. Here is the client that i used to help you as well:

    use strict; use warnings; my $table1 = Table->new(size => 'big', color => 'red'); my $table2 = Table->new(size => 'small', color => 'blue'); my $party1 = Party->new( tables => [$table1,$table2] ); my $party2 = Party->new( tables => [$table2,$table2] ); print $party1->has_red_table, $/; print $party2->has_red_table, $/;

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      Thank you for your contributions to this thread.

      However, I referred back to the original post and reread the specifications:

      1.) Add the small blue table to the party once the party has started.

      2.) Once all the tables are at the party, how do I see if I have a red table?

      Now, if I were to substitute the word “pizza” for the phrase “small blue table” in the first specification, that sentence would indicate to me that the party had started, but there was no pizza, but the pizza arrived soon after the start of the party.

      So, I assume  my $party1 = Party->new( tables => [$table1,$table2] ); models the start of a party.

      Hence, the design of the Party object-oriented programming (OOP) application program interface (API) seems to lack a way to add a table or tables to a party after the party has started.

      So, our model of a party on a computer is incomplete. I think this falls under the category of abstraction, for those readers that are students of OOP.

Re: Perl Moose, basics
by Riales (Hermit) on Dec 10, 2015 at 23:02 UTC

    This sounds like a homework question...but what have you tried? Which concepts are you struggling with?

Re: Perl Moose, basics
by Nar (Novice) on Dec 10, 2015 at 23:57 UTC
    Was struggling with concept, Jeffa got me going in the right direction. Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1149965]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-04-24 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found