K
kj
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
the fly (i.e. when the module is loaded)?
Thanks!
kj
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.
Uri Guttman said:k> In <[email protected]> Uri Guttman
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?
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.
Sidestepping the rest of the thread (ie the validity of doing this), youWalter said::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.
Mark Clements said: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
Mark Clements said: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
I tend to use them because if used correctly they enable the compiler toAnno said:Why the prototype? They are the exception with sub definitions, not
the rule.
In this case, yes, but I don't see how this enables you to build*newsub = sub { return "from newsub" };
Anno Siegel wrote:
In this case, yes, but I don't see how this enables you to build
subroutines on the fly.
Mark Clements said:I tend to use them because if used correctly they enable the compiler to
catch attempts to call subs with the wrong number of arguments,
though
obviously here they'll have no effect as the compiler can't see the
definition of the new sub. It was therefore misleading of me to use it here.
In this case, yes, but I don't see how this enables you to build
subroutines on the fly. eval STRING is relatively inefficient but
extremely powerful (and therefore dangerous).
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.
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.