nysus has asked for the wisdom of the Perl Monks concerning the following question:
I'm going through Conway's OO book. I put together his example code from Chapt. 3 and up to section 3.3.3 Automating data member access on page 91:
#!c:/perl/bin/perl -w package CD::Music; use strict; use vars '$AUTOLOAD'; sub AUTOLOAD { my ($self) = @_; $AUTOLOAD =~ /.*::get(_\w+)/ or die "No such method: $AUTOLOAD"; exists $self->{$1} or die "No such attribute: $1"; return $self->{$1} } { 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_location { ($_[0]->{_room}, $_[0]->{_shelf}) } 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}; } } package main; my $cd = CD::Music->new( "Canon in D", "Pachelbel", "Boering Muß GmbH" +, "1729-67836847-1", 1, 8, 8, 5.0); print $cd->get_name, ", ", $cd->get_publisher, "\n"; printf "Room %s, shelf %s\n", $cd->get_location; $cd->set_location(5,3); print CD::Music->get_count, "\n";
I'm getting some strange behavior that I can't explain. I stepped through the program with the debugger and discovered that after the program executes line 19:
sub get_count { $_count },
the program jumps up and to the 'AUTOLOAD' subroutine which generates the following error:
(in cleanup) No such method: CD::Music::DESTROY at d_autoload line 10.
So where is this DESTROY method coming from and why is the programming executing the AUTOLOAD subroutine after line 19 anyway???
Thanks!
$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot";
$nysus = $PM . $MCF;
Click here if you love Perl Monks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Strange code execution with 'AUTOLOAD'
by Masem (Monsignor) on Jul 01, 2001 at 22:24 UTC | |
by nysus (Parson) on Jul 01, 2001 at 22:29 UTC | |
|
Re: Strange code execution with 'AUTOLOAD'
by btrott (Parson) on Jul 01, 2001 at 22:32 UTC | |
by DrZaius (Monk) on Jul 01, 2001 at 23:40 UTC | |
by btrott (Parson) on Jul 02, 2001 at 04:38 UTC |