Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to define a sub on the fly?

Reply
Thread Tools

How to define a sub on the fly?

 
 
kj
Guest
Posts: n/a
 
      04-30-2004



Can someone please show me how to define a sub Foo::Bar::baz on
the fly (i.e. when the module is loaded)?

Thanks!

kj

--
NOTE: In my address everything before the period is backwards.
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      04-30-2004
In article <c6uipi$inb$(E-Mail Removed)>, kj <(E-Mail Removed)> wrote:
:Can someone please show me how to define a sub Foo::Bar::baz on
:the fly (i.e. when the module is loaded)?

Try constructing it inside a BEGIN block. BEGIN blocks are executed
as the module is loaded.
--
Disobey all self-referential sentences!
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      05-01-2004
>>>>> "WR" == Walter Roberson <(E-Mail Removed)-cnrc.gc.ca> writes:

WR> In article <c6uipi$inb$(E-Mail Removed)>, kj <(E-Mail Removed)> wrote:
WR> :Can someone please show me how to define a sub Foo::Bar::baz on
WR> :the fly (i.e. when the module is loaded)?

WR> Try constructing it inside a BEGIN block. BEGIN blocks are executed
WR> as the module is loaded.

that won't help much. the module itself is executed at compile time when
it is loaded by the use statement so the BEGIN block gains little there.

but i would ask the OP what they mean by creating a sub on the fly and
why does he think it is needed? it isn't hard to do but i won't help if
it is not even needed.

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Malcolm Dew-Jones
Guest
Posts: n/a
 
      05-01-2004
kj ((E-Mail Removed)) wrote:
: Can someone please show me how to define a sub Foo::Bar::baz on
: the fly (i.e. when the module is loaded)?

Since that's the normal case, I'll assume you mean you want to define it
in an unusual manner. The following probably defines two subs called baz
and boz, though I can't test it while using this computer.


foreach $baz (qw(baz boz))
{ eval qq{
sub Foo::Bar::$baz
{ print "This is sub Foo Bar $baz\n";
}
};
}


(oh dear, ATARI sm125 appears to be toasting, bye bye to an old faithful
companion. I wonder if this will make sense. Better stop typing

 
Reply With Quote
 
kj
Guest
Posts: n/a
 
      05-01-2004
In <(E-Mail Removed)> Uri Guttman <(E-Mail Removed)> writes:

>>>>>> "WR" == Walter Roberson <(E-Mail Removed)-cnrc.gc.ca> writes:


> WR> In article <c6uipi$inb$(E-Mail Removed)>, kj <(E-Mail Removed)> wrote:
> WR> :Can someone please show me how to define a sub Foo::Bar::baz on
> WR> :the fly (i.e. when the module is loaded)?


>but i would ask the OP what they mean by creating a sub on the fly and
>why does he think it is needed? it isn't hard to do but i won't help if
>it is not even needed.


OK, here's the situation. The module in question has the simple
task of serving information contained in a *variable* collection
of data flat-files (data/A, data/B, ..., data/X, etc.). For me,
the most natural way to code this would be to have a sub/method
that took a filename as argument (e.g. get_data("$path/data/B")),
but for reasons that have to do with the software framework in
which this module will operate, it is necessary, instead, to have
several separate subroutines named after the data files (e.g. sub
A { ... }, etc., which could simply call get_data("$path/data/A").
And since the names of these files cannot be known until runtime,
these subroutines can only be defined then.

Thanks,

kj
--
NOTE: In my address everything before the period is backwards.
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      05-01-2004
>>>>> "k" == kj <(E-Mail Removed)> writes:

k> In <(E-Mail Removed)> Uri Guttman
k> <(E-Mail Removed)> writes:

>> but i would ask the OP what they mean by creating a sub on the fly and
>> why does he think it is needed? it isn't hard to do but i won't help if
>> it is not even needed.


k> OK, here's the situation. The module in question has the simple
k> task of serving information contained in a *variable* collection of
k> data flat-files (data/A, data/B, ..., data/X, etc.). For me, the
k> most natural way to code this would be to have a sub/method that
k> took a filename as argument (e.g. get_data("$path/data/B")), but
k> for reasons that have to do with the software framework in which
k> this module will operate, it is necessary, instead, to have several
k> separate subroutines named after the data files (e.g. sub A { ...
k> }, etc., which could simply call get_data("$path/data/A"). And
k> since the names of these files cannot be known until runtime, these
k> subroutines can only be defined then.

then how do you determine which method to call? i mean how does the
driving code select the method? is based on the filename itself? what is
the class this object is in? could it have an attribute that is the
filename? one way to solve this is via polymophism. you create a set of
classes named for the file (e.g. MyData::A) which all implement the
get_data method. the framework then builds a class name based on the
file name and uses that to create a new object. then it calls get_data
(remember, the same method name in all the classes) and voila, you have
your data. but it all sounds like too much work in any case. what kind
of framework can't handle simple args or a dispatch table.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      05-01-2004
Uri Guttman <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> >>>>> "k" == kj <(E-Mail Removed)> writes:

>
> k> In <(E-Mail Removed)> Uri Guttman
> k> <(E-Mail Removed)> writes:
>
> >> but i would ask the OP what they mean by creating a sub on the fly and
> >> why does he think it is needed? it isn't hard to do but i won't help if
> >> it is not even needed.

>
> k> OK, here's the situation. The module in question has the simple
> k> task of serving information contained in a *variable* collection of
> k> data flat-files (data/A, data/B, ..., data/X, etc.). For me, the
> k> most natural way to code this would be to have a sub/method that
> k> took a filename as argument (e.g. get_data("$path/data/B")), but
> k> for reasons that have to do with the software framework in which
> k> this module will operate, it is necessary, instead, to have several
> k> separate subroutines named after the data files (e.g. sub A { ...
> k> }, etc., which could simply call get_data("$path/data/A"). And
> k> since the names of these files cannot be known until runtime, these
> k> subroutines can only be defined then.
>
> then how do you determine which method to call? i mean how does the


I'm not sure the OP's framework is OO at all.

> driving code select the method? is based on the filename itself?


That's the question, whether OO or not. Making the names of subroutines
dependent on exterior filenames breaks the program/data separation.
Making the sub calls later may break it again.

> the class this object is in? could it have an attribute that is the
> filename? one way to solve this is via polymophism. you create a set of
> classes named for the file (e.g. MyData::A) which all implement the
> get_data method. the framework then builds a class name based on the
> file name and uses that to create a new object.


What is the object for? The get_data method doesn't need one, it might
as well be a class method and be called as such. You would need to call
a class method to create the object in any case, it might as well do
the actual job. IOW, instead of doing

my $class = 'MyData::' . $filename;
my $obj = $class->new;
my $data = $obj->get_data;

say

my $class = 'MyData::' . $filename;
my $data = $class->get_data;

> then it calls get_data
> (remember, the same method name in all the classes) and voila, you have
> your data. but it all sounds like too much work in any case. what kind
> of framework can't handle simple args or a dispatch table.


I'm wondering that too...

Anno
 
Reply With Quote
 
Mark Clements
Guest
Posts: n/a
 
      05-01-2004
Walter Roberson wrote:
> In article <c6uipi$inb$(E-Mail Removed)>, kj <(E-Mail Removed)> wrote:
> :Can someone please show me how to define a sub Foo::Bar::baz on
> :the fly (i.e. when the module is loaded)?
>
> Try constructing it inside a BEGIN block. BEGIN blocks are executed
> as the module is loaded.

Sidestepping the rest of the thread (ie the validity of doing this), you
can use the autoloader and/or eval. Creation of subs (or any code on the
fly) can be done with eval $definition.

eg
eval<<EOF;
sub newsub(){
return "from newsub";
};
EOF

You may well want to take this step within a BEGIN block. Bear in mind
prototypes are meaningless when a sub is called as a class or object method.

There are many examples of using the autoloader available so I won't
give one here, but you probably want to use it to intercept calls to eg
get_filename1(), get_filename2() and take it from there.

Mark
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      05-01-2004
Mark Clements <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Walter Roberson wrote:
> > In article <c6uipi$inb$(E-Mail Removed)>, kj <(E-Mail Removed)> wrote:
> > :Can someone please show me how to define a sub Foo::Bar::baz on
> > :the fly (i.e. when the module is loaded)?
> >
> > Try constructing it inside a BEGIN block. BEGIN blocks are executed
> > as the module is loaded.

> Sidestepping the rest of the thread (ie the validity of doing this), you
> can use the autoloader and/or eval. Creation of subs (or any code on the
> fly) can be done with eval $definition.


....it can, granted, but there's no real need for "eval".

> eg
> eval<<EOF;
> sub newsub(){


Why the prototype? They are the exception with sub definitions, not
the rule.

> return "from newsub";
> };
> EOF


*newsub = sub { return "from newsub" };

has the same effect, but is clearer and simpler.

Anno
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      05-01-2004
Mark Clements <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Walter Roberson wrote:
> > In article <c6uipi$inb$(E-Mail Removed)>, kj <(E-Mail Removed)> wrote:
> > :Can someone please show me how to define a sub Foo::Bar::baz on
> > :the fly (i.e. when the module is loaded)?
> >
> > Try constructing it inside a BEGIN block. BEGIN blocks are executed
> > as the module is loaded.

> Sidestepping the rest of the thread (ie the validity of doing this), you
> can use the autoloader and/or eval. Creation of subs (or any code on the
> fly) can be done with eval $definition.


....it can, granted, but there's no real need for "eval".

> eg
> eval<<EOF;
> sub newsub(){


Why the prototype? They are the exception with sub definitions, not
the rule.

> return "from newsub";
> };
> EOF


*newsub = sub { return "from newsub" };

has the same effect, but is clearer and simpler.

Anno

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Death To Sub-Sub-Sub-Directories! Lawrence D'Oliveiro Java 92 05-20-2011 06:50 AM
Recognising Sub-Items and sub-sub items using xslt Ben XML 2 09-19-2007 09:35 AM
About typedef -- define the function pointer or define function model? robin liu C Programming 3 04-21-2006 03:26 PM
How to define a define that defines some defines ? theotyflos C Programming 3 02-19-2004 05:07 PM



Advertisments