|
|
On Tuesday 22 Dec 2009 04:20:30 Parag Kalra wrote:
> Hi Shlomi,
>
> Thanks for your valuable comments. Will definitely keep it in mind in
> future scripts.
You're welcome.
>
>
> > I've ran it and I still cannot figure out the bug.
>
>
>
> Here is the bug -
>
> parag@station3:/tmp$ perl /tmp/oops.pl
> Chetak goes Neigh
> Chetak eats Long grass
> Chetak has Brown color
> An unamed Horse goes Neigh
> An unamed Horse eats Long grass
> *An unamed Horse has color*
Well, the code for that is:
<<<<<<<<
Horse->new();
Horse->speak;
Horse->eat('Long grass');
Horse->color;
>>>>>>>>
Where sub color is:
<<<<<<<<
sub color {
$class = shift;
print $class->name, " has ", ${$class}{Color}, " color\n";
}
>>>>>>>>
Now ${$class}{Color} gets evaluated to ${"Horse"}{Color} which is actually
$Horse{Color} (only a package variable) - the value of %Horse hash indexed by
the key "Color". This is a symbolic reference which "use strict;" would have
caught, but in your case it evaluates to undef.
You can try defining a sub _color { ... } method accessor similar to the "sub
name", but you should really rethink your entire strategy here. I can't think
of any reason why the current duality of class methods and instance methods in
your code should be preserved.
Regards,
Shlomi Fish
> Chimpu goes Baaah
> Chimpu eats Small grass
> Chimpu has White color
> An unamed Sheep goes Baaah
> An unamed Sheep eats Small grass
> *An unamed Sheep has color*
> parag@station3:/tmp$
>
> As shown in the above output color for unnamed Horse is not getting
> printed.
>
> Cheers,
> Parag
>
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Interview with Ben Collins-Sussman - http://shlom.in/sussman
Bzr is slower than Subversion in combination with Sourceforge.
( By: http://dazjorz.com/ )
|
|