Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Moose handling of unwanted parameters

by glasswalk3r (Friar)
on Dec 19, 2016 at 23:06 UTC ( [id://1178136]=perlquestion: print w/replies, xml ) Need Help??

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

Hello fellow monks,

This post could be named "OOP designing with Moose tip", but that depends the way you see it.

I have a project of mine that I use Moose and I decided to separate connection parameters from the classes that need them, and create a separated class for it. Whenever those classes need to actually connect to something, the corresponding method request a connection instance passed as parameter. This seems to be working well.

Things get fishy when I noticed a unit test that I was expecting to fail indeed did not. I'm still creating instance of those classes and passing connection parameters that they do not require anymore. Moose silents discard those parameters and move on. Since this particular unit test does not invoke a method that requires a connection, everything "works", until the object are actually used, that's it. It became a time bomb that I need to deactivate...

So, the question is: am I using Moose properly? My design has a flaw? My unit test is not complete (I think it is not, although the connection might be considered a integration test)?

Thanks!

Alceu Rodrigues de Freitas Junior
---------------------------------
"You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill

Replies are listed 'Best First'.
Re: Moose handling of unwanted parameters
by kcott (Archbishop) on Dec 20, 2016 at 05:23 UTC

    G'day glasswalk3r,

    With only a prosaic description, I'm left trying to envisage what your code looks like. That visualisation could easily be wrong. With that caveat in mind, here's some suggestions.

    Perhaps use the lazy option in your Moose constructor. Something like:

    has connection_parameters => (lazy => 1, builder => ...);

    Maybe you want a MakesConnection Moose::Role; potentially applied conditionally using one of the Moose::Util functions, e.g. apply_all_roles().

    — Ken

      Fear not, since the code is a free software. See the classes Siebel::Srvrmgr::Daemon and subclasses of it.

      I specially liked the Moose::Role suggestion, using only a method to indicate connection is required (because Siebel::Srvrmgr::Daemon::Offline - which is not on CPAN yet - doesn't require one). Do you mean to create an required attribute, didn't you?

      Alceu Rodrigues de Freitas Junior
      ---------------------------------
      "You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill
Re: Moose handling of unwanted parameters
by choroba (Cardinal) on Dec 20, 2016 at 09:59 UTC
    If you want Moose to fail on unsupported arguments to a constructor, use MooseX::StrictConstructor .

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-24 12:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found