thekestrel has asked for the wisdom of the Perl Monks concerning the following question:
This might be a 101 question, but someone has to ask the silly ones. I have a scipt that operates on a global as such...
use a_module; my $global = a_module->new(); sub func1 { $global->do_something1(); } sub func2 { $global->do_something2(); } $global->do_init; func1(); func2();
The problem with this setup is that it is not re-entrant so if I wanted a $global and $global2 i'd have to duplicate everything which is just not on. So I think ok how about rewriting it as follows...
use a_module; sub func1 { my $var = $_[0]; $var->do_something1(); } sub func2 { my $var = $_[0]; $var->do_something2(); } my $local = a_module->new(); $local->do_init; func1($local); func2($local);
The problem with the above code is that the variables $var in both func1 and func2 don't know that they are supposed to be an instance of type a_module and hence don't have clue what the member functions do_something1 and do_something2 are.
This leads to the obvious question, how do i tell the scalars in the subroutines that they in fact a certain type? Is the only way to do it to pass a reference to the variable such that when you operate on it, it then knows about the member functions. Something like...
use a_module; sub func1 { my $var = $_[0]; $var->do_something1(); } sub func2 { my $var = $_[0]; $var->do_something2(); } my $local = a_module->new(); $local->do_init; func1(\$local); func2(\$local);
Thanks in advance for any thoughts =).
Regards Paul.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Typecasting a scalar to a module
by Mugatu (Monk) on Mar 03, 2005 at 18:09 UTC | |
by Fletch (Bishop) on Mar 03, 2005 at 18:19 UTC | |
by Mugatu (Monk) on Mar 03, 2005 at 18:30 UTC | |
by thekestrel (Friar) on Mar 03, 2005 at 18:34 UTC | |
|
Re: Typecasting a scalar to a module
by Rhandom (Curate) on Mar 03, 2005 at 18:16 UTC | |
|
Re: Typecasting a scalar to a module
by jhourcle (Prior) on Mar 04, 2005 at 01:40 UTC | |
by Mugatu (Monk) on Mar 04, 2005 at 16:53 UTC |