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---------------------------------------------
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---------------------------------------------