Home > Archive > PERL Beginners > August 2007 > Problem with function overloading
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Problem with function overloading
|
|
| I don't like SPAM 2007-07-29, 3:59 am |
| Hello,
I have an quite outdated perl-app, that does not work any more.
It consists of various objects and a simple plugin-interface. All "static"
objects work well, even with function overloading.
The plugin-objects do not work as expected any more.
I hope, someone can shine me a light ...
The plugins have a base-class, where the new() subroutine and common
initialization stuff resides.
All plugins belong to a subpackage of the base-class.
The app determines the name of the plugin from the filesystem.
The loading of the plugin happens like this:
if (opendir($dp, $base)) {
while (my $de =3D readdir($dp)) {
if ($de =3D~ /\.pm$/) {
my $moduleName =3D 'subpkg::'.$de;
eval "require $moduleName";
if ($moduleName->isa('subpkg::Plugin')) {
$inst =3D $moduleName->new($someArg);
}
}
}
closedir($dp);
}
I also tried to create the instance with:
$inst =3D eval('new '.$moduleName);
but it makes no difference.
The new() - subroutine from the baseclass contains this print-statement:
print($class.'::new() ... overloading does not work?!?'."\n");
and calls $self->init() afterwards.
Each init() - subroutine has a print-statement telling the classname and the
base-class as well as every plugin-class has the init() - subroutine.
=46rom the output I can see, that the right plugin get's loaded, but the wr=
ong
initialization happens (only the init() from the base class is executed).
The same code worked with perl 5.8.8 some months ago and I have no idea, wh=
at
part of perl changed and what I have to change in my scripts to get them=20
working again.
Any help is very appreciated.
regards Reinhard
| |
| Tom Phoenix 2007-08-02, 7:00 pm |
| On 7/29/07, I don't like SPAM <unknown@public-files.de> wrote:
> eval "require $moduleName";
Hm. You say this isn't working for you, but you're not checking the
value of $@ after the evil eval? (By the way, unless I missed
something, that "require" could load your module, if it loads it at
all, from a different directory than the one you're looking through
via readdir. That may matter to you. Perhaps you want to use local()
on @INC?)
Before I eval a string, I make three promises:
1. I promise that I know exactly what the string contains.
2. I promise to check $@ after the eval.
3. I promise that I really understand the code contained in the string.
If you can find another way to do what you want without eval of a
string, the other way is almost certainly better. (eval of a block is
another matter. Using eval on a block is acceptable, standard
practice; using eval on a string is the evil eval, and should be
avoided.)
Hope this helps!
--Tom Phoenix
Stonehenge Perl Training
| |
| I don't like SPAM 2007-08-05, 4:01 am |
| Hello Tom,
thank you very much for your attention!
>
> Hm. You say this isn't working for you, but you're not checking the
> value of $@ after the evil eval?
Ok, you got me :)
I did not know $@ - I'll go for it. Promised!
Anyway - I'm not sure, whether the eval fails.
I placed dump-statements to the new() function as well to the overloaded
functions.
The class ($moduleName) is right, but the overloaded function does not get
called.
> By the way, unless I missed something, that "require" could load your
> module, if it loads it at all, from a different directory than the one
> you're looking through via readdir. That may matter to you.
That's right.
The matter is, I stripped lot of code just to focus on "my" problem.
Of cause I had a print of $moduleName before the eval-line.
May be I did not mention it - the script is not new. It worked for quite a
long time.
I did some further investigations and found out, that it may be a problem of
the operating system. My scripts work fine on a real debian an they fail on
kubuntu.
So - may be, I did not post at the apropiate list.
As I don't have that deep understanding of perl or the ongoing behind the
sceenes, I went back to real debian.
> Perhaps you want to use local() on @INC
Sorry, but I don't understand the benefit of that proposal.
I dumped the content of @INC and I guess, that perl works like any other stuff
acting on a bunch of paths, and uses the first matching one.
> Before I eval a string, I make three promises:
>
> 1. I promise that I know exactly what the string contains.
> 2. I promise to check $@ after the eval.
> 3. I promise that I really understand the code contained in the string.
well, I failed at least at point 2. Gonna change that.
Thanks a lot for your advices!
regards
Reinhard
|
|
|
|
|