PERL PACKAGE IN MODULE

PERL PACKAGE IN MODULE

am 19.09.2007 15:44:35 von eser

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

Re: PERL PACKAGE IN MODULE

am 19.09.2007 16:50:08 von Michele Dondi

On Wed, 19 Sep 2007 06:44:35 -0700, colli wrote:

>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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: PERL PACKAGE IN MODULE

am 19.09.2007 18:24:48 von Ben Morrow

Quoth 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; ?

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

Re: PERL PACKAGE IN MODULE

am 19.09.2007 22:55:11 von Michele Dondi

On Wed, 19 Sep 2007 17:24:48 +0100, Ben Morrow
wrote:

>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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: PERL PACKAGE IN MODULE

am 20.09.2007 22:05:06 von eser

On 19 Set, 18:24, Ben Morrow wrote:
> Quoth 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; ?
>
> 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