where to look for object method?

P

Piet

Hello,
I just started with OO Programming in Perl and stumbled over some
behaviour that I would like to understand.
When running the following code:
use strict;
print Cow->sound."\n";
Cow->speak;
{package Cow;
use vars qw(@ISA);
@ISA = qw(Animal);
sub sound {"moooh"};
}
{package Animal;
sub sound {"some undefined noise"};
sub speak {
my $class = $_[0];
print "A $class makes ",$class->sound," !\n";
};
}
it turns out that the class method is correctly invoked, but the class
method of the superclass is not found. However, when I put
"Cow->speak" at the end (i. e. after the package definitions), I get
the expected output. Normally, I would put any code after the package
imports and definitions, in which case the behaviour described above
would not have shown up. But I am still interested in your
explanations: Why is the class method found, while the inheritance
tree is NOT climbed up?
Thanks for any hints.
Piet
 
S

Sherm Pendley

Piet said:
use strict;
print Cow->sound."\n";
Cow->speak;
{package Cow;
use vars qw(@ISA);
@ISA = qw(Animal);
sub sound {"moooh"};
}
{package Animal;
sub sound {"some undefined noise"};
sub speak {
my $class = $_[0];
print "A $class makes ",$class->sound," !\n";
};
}
it turns out that the class method is correctly invoked, but the class
method of the superclass is not found. However, when I put
"Cow->speak" at the end (i. e. after the package definitions), I get
the expected output.

Perl code is executed in the order in which it appears in the source
file. Package declarations change the default package, but they do *not*
change the order of execution. To see why that's important, lets' remove
the pragmas, package declarations, and method definitions and examine
what's left:

print Cow->sound."\n";
Cow->speak;
@ISA = qw(Animal);

Does that help clear it up? Animal isn't added to @Cow::ISA until
*after* the Cow->speak method is called.
Normally, I would put any code after the package
imports and definitions, in which case the behaviour described above
would not have shown up.

That's one way of doing it. Another way is to place a BEGIN { ... }
block around your class definitions, which will force them to be
executed at compile-time instead of at run-time - that is, before the
rest of your code runs.

Yet another way - which is far more common - is to place each class in
its own .pm file, and "use" them at the top of your code. Since "use"
has an implicit BEGIN block, that has the same effect.

Now, if you all will excuse me for a while, all this talk of barnyard
animals has left me wanting to hear "Pigs on the Wing". :)

sherm--
 
J

Jay Tilton

(e-mail address removed) (Piet) wrote:

: I just started with OO Programming in Perl and stumbled over some
: behaviour that I would like to understand.
: When running the following code:

: use strict;
: print Cow->sound."\n";
: Cow->speak;
: {package Cow;
: use vars qw(@ISA);
: @ISA = qw(Animal);
: sub sound {"moooh"};
: }
: {package Animal;
: sub sound {"some undefined noise"};
: sub speak {
: my $class = $_[0];
: print "A $class makes ",$class->sound," !\n";
: };
: }

: it turns out that the class method is correctly invoked, but the class
: method of the superclass is not found. However, when I put
: "Cow->speak" at the end (i. e. after the package definitions), I get
: the expected output. Normally, I would put any code after the package
: imports and definitions, in which case the behaviour described above
: would not have shown up. But I am still interested in your
: explanations: Why is the class method found, while the inheritance
: tree is NOT climbed up?

@Cow::ISA needs to be defined before invoking Cow->speak. If you put the
method call before the "@ISA = qw(Animal);" line, the method gets called
before Cow's inheritance is defined.

The usual solution is to use a BEGIN{} block to initialize such package
variables before the class is ever used:

BEGIN { @ISA = 'Animal';}
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top