clueless on "use base" pragma

Discussion in 'Perl Misc' started by babydoe@mailinator.com, Mar 18, 2005.

  1. Guest

    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---------------------------------------------
     
    , Mar 18, 2005
    #1
    1. Advertising

  2. Also sprach :

    > 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
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
     
    Tassilo v. Parseval, Mar 18, 2005
    #2
    1. Advertising

  3. Guest

    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.
     
    , Mar 18, 2005
    #3
  4. Sherm Pendley, Mar 18, 2005
    #4
  5. Guest

    Sherm Pendley wrote:
    > wrote:
    >
    > > OO is new stuff to me.

    >
    > 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.
     
    , Mar 19, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sal achhala

    clueless student trying to parse XML

    sal achhala, Oct 17, 2003, in forum: Java
    Replies:
    7
    Views:
    464
    sal achhala
    Oct 20, 2003
  2. Inertia_sublimation
    Replies:
    5
    Views:
    2,126
    Inertia_sublimation
    Jan 3, 2004
  3. Mickey Segal
    Replies:
    0
    Views:
    398
    Mickey Segal
    Aug 21, 2004
  4. dave
    Replies:
    1
    Views:
    416
    Andy Dingley
    Oct 17, 2005
  5. Michael Lehmeier
    Replies:
    11
    Views:
    635
    Donn Cave
    Oct 27, 2003
Loading...

Share This Page