in reply to Typecasting a scalar to a module
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
For one thing, yes, they do. References in Perl know what type of thing they reference to, and if that reference is blessed, they know what class they're blessed into. Furthermore, you don't have to do typecasting in Perl, nor does the compiler have to know what methods an object has at compile time. It is handled at runtime. Your 2nd snippet of code will work as is.
Update: since there seems to be some confusion about what I meant when I said "2nd snippet," this is the one I was talking about:
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);
That is, his post contains 3 snippets. The first stores the object in a package global, the 2nd stores the object in a lexical and passes it in, and the 3rd stores the object in a lexical but passes in a reference to it.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Typecasting a scalar to a module
by Fletch (Bishop) on Mar 03, 2005 at 18:19 UTC | |
by Mugatu (Monk) on Mar 03, 2005 at 18:30 UTC | |
|
Re^2: Typecasting a scalar to a module
by thekestrel (Friar) on Mar 03, 2005 at 18:34 UTC |