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

Creating a Moose object at run-time

by talexb (Chancellor)
on Oct 22, 2015 at 18:45 UTC ( [id://1145674]=perlquestion: print w/replies, xml ) Need Help??

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

I'm interested in learning how to use Moose to create an object at run-time. I have a CSV file with field names and lengths, and I have a flat file that's in that format. I'd love to be able to go from those two data sources and come up with a collection of Moose objects that I can then Do Stuff With.

My research so far has turned up this contentious solution and the suggestion to look at Moose::Meta::Class. However, that last module seems to build on another class, when I don't want to do that -- I want to create a class so that (among other reasons) I can take advantage of Moose's code to check that a field's data is of the right type.

Update: Upon re-reading this, after posting it, I realized I'm not as clear here as I was when the question was in my head. I want to add attributes at run-time, rather than specify them (has, etc.) beforehand.

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Replies are listed 'Best First'.
Re: Creating a Moose object at run-time
by jeffa (Bishop) on Oct 22, 2015 at 19:49 UTC

    An object in Perl is most often implemented as a hash, because an object is key/value store of attributes and a namespace by which the compiler/interpreter can locate its methods. Even in Moose, they are still most often implemented as hashes, so you can always add "attributes" at run time, you just don't get the type checking, coercions and automatic attribute accessors.

    package Class; use Moose; package main; use strict; use warnings; use Data::Dumper; my $obj = Class->new; print Dumper $obj; $obj->{foo} = 'bar'; print Dumper $obj;

    You have to ask yourself, why do i need to add attributes at runtime and if still valid, why do i need Moose sugar to make my code only slightly easier for me to churn out at the expense of reinventing Moose, as the "contentious solution" you linked to points out.

    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)
    

      Thanks for the sample code -- I imagined that it might be a lot more complicated. I'm fairly new to Moose.

        You have to ask yourself, why do i need to add attributes at runtime and if still valid, why do i need Moose sugar to make my code only slightly easier for me to churn out at the expense of reinventing Moose, as the "contentious solution" you linked to points out.

      A fair point. It may be necessary to total some of the fields in a collection of Moose objects -- that's the only reason I can think of right now.

      My preference is just to stick with a simple array of hashrefs until it's necessary to get more complicated.

      Alex / talexb / Toronto

      Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: Creating a Moose object at run-time
by nevdka (Pilgrim) on Oct 22, 2015 at 23:53 UTC

    You can do complicated things with Moose::Meta::Attribute and Moose::Meta::Class (and the other Moose::Meta modules). I haven't delved deep into the Moose::Meta stuff, so I can't be more helpful. But from what I can tell, there's usually a simpler way, like what jeffa has suggested.

      there's usually a simpler way, like what jeffa has suggested.

      How about not using Moose at all? That seems the simplest.

      Edit: I realize this sounds kind of snarky, but if you need ad-hoc behavior, Moose may not be the best fit.

          I realize this sounds kind of snarky ..

        Nope, that's fine -- I knew that the question I was asking was pretty open-ended. For now, I using just a hashref, and it's working just fine. My little script is processing over 12,000 records in just over two seconds, so I'm happy with the performance.

        Alex / talexb / Toronto

        Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-25 11:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found