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

Don't know how to make a module for this repeating code

by exilepanda (Monk)
on Nov 24, 2012 at 16:00 UTC ( #1005388=perlquestion: print w/ replies, xml ) Need Help??
exilepanda has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I have a module that acting as an interface which looks quite similar to this :
package Some::Interface; use strict; my $formType = "HR-Recruitment"; # hard coded my $lastVerion = "hr_re_v12"; # hard coded eval "use parent 'somepack::$formType::$lastVersion';"; sub new { my $c = shift; # some statements to check if any error in @_ # some statements to check access right my $wrap = $c->SUPER::new( @_ ); # some statements to add more attributes for $wrap return bless $wrap; } 1;
I've repeating (copy and paste) this code(file) in every module that has similar purpose. But since this is repeating code, so I am wondering is there anyway I can snap out this code into some central place... The only different things among these interface modules is only the had coded part. But then...I have no idea how to offshore a constructor... can anyone tell me an idea for how to get a start ?

*UPDATE: my ultimate wish is I can make it as few lines as possible when I am creating another interface module.

Comment on Don't know how to make a module for this repeating code
Download Code
Re: Don't know how to make a module for this repeating code
by zwon (Monsignor) on Nov 24, 2012 at 16:24 UTC
    my $formType = "HR-Recruitment"; # hard coded my $lastVerion = "hr_re_v12"; # hard coded eval "use parent 'somepack::$formType::$lastVersion';";
    If values are hardcoded, then why not just:
    use parent 'somepack::HR-Recruitment::hr_re_v12';
    Answering your question, I would probably wrote a function to generate a package from the name and data unique to this package, and then just generate all the packages. Details depend on your particular case. Have a look onto Class::MOP::Class, and Moose::Meta::Class, though you can do it without Moose too.
      Thanks for answering, and helped me to think deeper for what is my question...

      So, I think my problem is not worrying how to create this package. Perhaps I can make a module to auto update the interface version triggered by the implementation classes as well.

      But then, seems what really interested me is how to eliminate this kind of "code", when this code is a constructor. like.. can I eval() some text which return from a package that carrying the script body etc..

        can I eval() some text which return from a package that carrying the script body etc..

        What does that mean?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (16)
As of 2014-08-22 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (159 votes), past polls