PERL PACKAGE IN MODULE

C

colli

Hi
I need to load one of mine perl template module whose package (which
is evoked at the head of each module) at the head of the code should
change to be the same of the name of the module I decided to call.
Each time I load it I would like to change the name of my module (but
the template still remain the same)
It is a default module template but can assume different MYNAME.pm so
a different package should be evoked for different module
name...Instead of MYNAME I would be able to change into MYNAME1 and to
change dinamically the package.
Is there a perl function to insert in my module instead of the static
expression: package MYNAME; ?

Thank you in advance
 
M

Michele Dondi

I need to load one of mine perl template module whose package (which
is evoked at the head of each module) at the head of the code should
change to be the same of the name of the module I decided to call.
Each time I load it I would like to change the name of my module (but
the template still remain the same)
It is a default module template but can assume different MYNAME.pm so
a different package should be evoked for different module
name...Instead of MYNAME I would be able to change into MYNAME1 and to
change dinamically the package.
Is there a perl function to insert in my module instead of the static
expression: package MYNAME; ?

I'm not sure I understand your question, but chances may be that
you're after __PACKAGE__.


Michele
 
B

Ben Morrow

Quoth colli said:
Hi
I need to load one of mine perl template module whose package (which
is evoked at the head of each module) at the head of the code should
change to be the same of the name of the module I decided to call.
Each time I load it I would like to change the name of my module (but
the template still remain the same)
It is a default module template but can assume different MYNAME.pm so
a different package should be evoked for different module
name...Instead of MYNAME I would be able to change into MYNAME1 and to
change dinamically the package.
Is there a perl function to insert in my module instead of the static
expression: package MYNAME; ?

There are various ways you could do this, none of which are for the
faint of heart :). One simple way would be to put the entire module into
a string eval: then you can perform any replacements you like on the
code before evalling it. Another would be to use a source filter. Both
these approaches would fall foul of the fact that Perl will only load a
given file once, but that can be got around.

However, before you do this you should take a step back. What are you
actually trying to achieve here? Chances are you can do it without
resorting to rewriting your module on the fly, say by creating a
'package factory' module that will, on request, create a new package and
import some functions into it.

Ben
 
M

Michele Dondi

However, before you do this you should take a step back. What are you
actually trying to achieve here? Chances are you can do it without
resorting to rewriting your module on the fly, say by creating a
'package factory' module that will, on request, create a new package and
import some functions into it.

This can be "easily" done with the code-in-@INC facility.


Michele
 
C

colli

There are various ways you could do this, none of which are for the
faint of heart :). One simple way would be to put the entire module into
a string eval: then you can perform any replacements you like on the
code before evalling it. Another would be to use a source filter. Both
these approaches would fall foul of the fact that Perl will only load a
given file once, but that can be got around.

However, before you do this you should take a step back. What are you
actually trying to achieve here? Chances are you can do it without
resorting to rewriting your module on the fly, say by creating a
'package factory' module that will, on request, create a new package and
import some functions into it.

Ben

Thank you very much
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top