clueless on "use base" pragma

B

babydoe

I know this is bread and butter stuff to all of you,
and I blush to post. I am not familiar with OO and
have never rolled my own modules. This fails to compile;
perl outputs the error :

' Can't locate object method "get_name" via package "9"
' at c:/myPerl/conway/myMusic.plx line 13.

I have two files (worked examples from Conway's OO book) :

Music.pm located at c:/myPerl/conway/CD/Music.pm
myMusic.plx located at c:/myPerl/conway/myMusic.plx

%<--myMusic.plx------------------------------------

package main ;
use base qw(CD::Music);

# Create and object storing a CD's details
my $cd = CD::Music->new("Canon in D", "Pachelbel",
"Boering Mußak GmbH", "1729-67836847-1",
1,
8,8,
5.0) ;

# What's the CD called ?
print $cd->get_name, "\n" ;

# Where would we find it ?
printf "Room %s, shelf %s\n", $cd->set_location ;

# Move it to room 5, shelf 3
printf "Room %s, shelf %s\n", $cd->set_location(5,3) ;

# How many CDs in the entire collection ?
print CD::Music->get_count, "\n" ;

%<--end---------------------------------------------

%<--Music.pm----------------------------------------

package CD::Music;
use base qw(Exporter) ;
use strict;

{
my $_count = 0;
sub get_count { $_count }
my $_incr_count = sub { ++$_count };

sub new {
my $class = @_;
$_incr_count->();
bless {
_name => $_[1],
_artist => $_[2],
_publisher => $_[3],
_ISBN => $_[4],
_tracks => $_[5],
_room => $_[6],
_shelf => $_[7],
_rating => $_[8],
}, $class;
}
}

sub get_name { $_[0]->{_name} }
sub get_artist { $_[0]->{_artist} }
sub get_publisher { $_[0]->{_publisher} }
sub get_ISBN { $_[0]->{_ISBN} }
sub get_tracks { $_[0]->{_tracks} }

sub set_location {
my ( $self, $shelf, $room ) = @_;
$self->{_room} = $room if $room;
$self->{_shelf} = $shelf if $shelf;
return ( $self->{_room}, $self->{_shelf} );
}

sub set_rating {
my ( $self, $rating ) = @_;
$self->{_rating} = $rating if defined $rating;
return $self->{_rating};
}

1;

%<--end---------------------------------------------
 
T

Tassilo v. Parseval

Also sprach (e-mail address removed):
I know this is bread and butter stuff to all of you,
and I blush to post. I am not familiar with OO and
have never rolled my own modules. This fails to compile;

Well, it does compile. It fails to run. :)
perl outputs the error :

' Can't locate object method "get_name" via package "9"
^^^^^^^^^^^^^^^

That's fairly conclusive. It means that you tried to call the method
"get_name" on an object that was blessed into the class "9". Naturally,
you don't have such a class. The class is supposed to be CD::Music.
' at c:/myPerl/conway/myMusic.plx line 13.

I have two files (worked examples from Conway's OO book) :

Music.pm located at c:/myPerl/conway/CD/Music.pm
myMusic.plx located at c:/myPerl/conway/myMusic.plx

%<--myMusic.plx------------------------------------

package main ;
use base qw(CD::Music);

This statement makes 'main' a subclass of CD::Music. Not that it would harm,
but you really want:

use CD::Music;
# Create and object storing a CD's details
my $cd = CD::Music->new("Canon in D", "Pachelbel",
"Boering Mußak GmbH", "1729-67836847-1",
1,
8,8,
5.0) ;

# What's the CD called ?
print $cd->get_name, "\n" ;

# Where would we find it ?
printf "Room %s, shelf %s\n", $cd->set_location ;

# Move it to room 5, shelf 3
printf "Room %s, shelf %s\n", $cd->set_location(5,3) ;

# How many CDs in the entire collection ?
print CD::Music->get_count, "\n" ;

%<--end---------------------------------------------

%<--Music.pm----------------------------------------

package CD::Music;
use base qw(Exporter) ;
use strict;

{
my $_count = 0;
sub get_count { $_count }
my $_incr_count = sub { ++$_count };

sub new {
my $class = @_;

After this statement, $class will not contain "CD::Music" but instead
the number 9 because the array @_ contained 9 elements. You have to make
the assignment in list-context:

my ($class) = @_;
$_incr_count->();
bless {
_name => $_[1],
_artist => $_[2],
_publisher => $_[3],
_ISBN => $_[4],
_tracks => $_[5],
_room => $_[6],
_shelf => $_[7],
_rating => $_[8],
}, $class;
}
}

[...]

I only had a cursory glance over the rest of your code and didn't spot
any obvious (other) problems in it.

Tassilo
 
B

babydoe

Hello Tassilo,

Thankyou for being patient :)

I am sorry for wasting your time (and everyone else's) on such
a basic error - my only excuse is that OO is new stuff to me.
You are, of course, correct : the fault lay in making an assignment
in scalar context, when it should have been in list context.
 
S

Sherm Pendley

OO is new stuff to me.

Do you know that several OO tutorials come with Perl?

perldoc perlboot
perldoc perltoot
perldoc perltooc
perldoc perlbot

sherm--
 
B

babydoe

Sherm said:
Do you know that several OO tutorials come with Perl?

perldoc perlboot
perldoc perltoot
perldoc perltooc
perldoc perlbot

Hello Sherm,

The high-quality HTML in an ActiveState distro
is a pleasure to look at and to read; but even so,
I can't but feel sometimes, that perldoc is a maze
of twisty little passages, all alike. Thankyou for
pointing out the treasures of perlboot, perltoot,
perltooc, and perlbot - I must have tripped over
them with them without seeing.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top