Home > Archive > PERL Miscellaneous > July 2004 > Class quandry
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Andrew Burton 2004-07-23, 3:56 am |
| (Bah, I accidentally posted this to c.l.p.moderated. My apologies.)
I'm dickering around with Perl tonight, trying to grasp OOP concepts in Perl --
I like them enough in C#, and thought understanding them in my favorite
language would be worthwhile. To do this, I'm reading through and using
examples from "Tom's object-oriented tutorial for perl." For some reason his
example is giving me bugs, and Google is no help. This is the error I get with
the following code:
Can't modify non-lvalue subroutine call at ./testclass line 27.
Here's the code...
code:
#!/usr/bin/perl -w
use strict;
{ package Roxy;
sub new {
my $self = {};
$self->{'word'} = undef;
bless $self;
return $self;
}
sub speak {
my $self = shift;
print "$self says 'hello'.\n";
}
sub name {
my $self = shift;
if (@_) { $self->{'word'} = shift; }
return $self->{'word'}
}
}
my $muse = Roxy->new();
$muse->speak;
$muse->name = "Andy";
print $muse->name;
exit;
For whatever it's worth, I'm using Perl 5.8.0 Any help or suggestions would be
appreciated. Thanks!
Andrew Burton - tuglyraisin at aol dot com
Felecia Station on Harvestgain - Jarod Godel in Second Life
| |
| Bob Walton 2004-07-23, 3:56 am |
| Andrew Burton wrote:
....
> example is giving me bugs, and Google is no help. This is the error I get with
> the following code:
> Can't modify non-lvalue subroutine call at ./testclass line 27.
>
> Here's the code...
>
> code:
>
> #!/usr/bin/perl -w
>
> use strict;
>
> { package Roxy;
>
> sub new {
> my $self = {};
> $self->{'word'} = undef;
> bless $self;
> return $self;
> }
> sub speak {
> my $self = shift;
> print "$self says 'hello'.\n";
Probably should be:
print ref($self)." says 'hello'.\n";
> }
> sub name {
> my $self = shift;
> if (@_) { $self->{'word'} = shift; }
> return $self->{'word'}
> }
> }
>
> my $muse = Roxy->new();
>
> $muse->speak;
> $muse->name = "Andy";
You probably meant:
$muse->name("Andy");
>
> print $muse->name;
>
> exit;
>
>
....
> Andrew Burton - tuglyraisin at aol dot com
....
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
| |
| Jeff 'japhy' Pinyan 2004-07-23, 3:56 am |
| On 23 Jul 2004, Andrew Burton wrote:
>I'm dickering around with Perl tonight, trying to grasp OOP concepts in
>Perl -- I like them enough in C#, and thought understanding them in my
>favorite language would be worthwhile. To do this, I'm reading through
>and using examples from "Tom's object-oriented tutorial for perl." For
>some reason his example is giving me bugs, and Google is no help. This
>is the error I get with the following code: Can't modify non-lvalue
>subroutine call at ./testclass line 27.
[snip]
> sub name {
> my $self = shift;
> if (@_) { $self->{'word'} = shift; }
> return $self->{'word'}
> }
[snip]
>$muse->name = "Andy";
You're trying to assign to a method call. You can't do that unless the
method is an lvalue method:
sub name :lvalue {
my $self = shift;
$self->{word};
}
Otherwise, you need to *pass* the name to the method:
$obj->name("New Name Here");
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
RPI Corporation Secretary % have long ago been overpaid?
http://japhy.perlmonk.org/ %
http://www.perlmonks.org/ % -- Meister Eckhart
| |
| Purl Gurl 2004-07-23, 3:56 am |
| Andrew Burton wrote:
(snipped)
> Can't modify non-lvalue subroutine call at ./testclass line 27.
> print $muse->name;
print $muse->name('Andy');
Take a look at your "speak" subroutine
which will return a hash reference.
Is a reference what you intend?
Purl Gurl
| |
| thundergnat 2004-07-23, 3:56 pm |
| Andrew Burton wrote:
> (Bah, I accidentally posted this to c.l.p.moderated. My apologies.)
>
> I'm dickering around with Perl tonight, trying to grasp OOP concepts in Perl --
> I like them enough in C#, and thought understanding them in my favorite
> language would be worthwhile. To do this, I'm reading through and using
> examples from "Tom's object-oriented tutorial for perl." For some reason his
> example is giving me bugs, and Google is no help. This is the error I get with
> the following code:
> Can't modify non-lvalue subroutine call at ./testclass line 27.
>
> Here's the code...
>
> code:
>
> #!/usr/bin/perl -w
>
> use strict;
>
> { package Roxy;
>
> sub new {
> my $self = {};
> $self->{'word'} = undef;
> bless $self;
> return $self;
> }
> sub speak {
> my $self = shift;
> print "$self says 'hello'.\n";
> }
> sub name {
> my $self = shift;
> if (@_) { $self->{'word'} = shift; }
> return $self->{'word'}
> }
> }
>
> my $muse = Roxy->new();
>
> $muse->speak;
> $muse->name = "Andy";
$muse->name('Andy');
>
> print $muse->name;
>
> exit;
>
>
>
> For whatever it's worth, I'm using Perl 5.8.0 Any help or suggestions would be
> appreciated. Thanks!
>
> Andrew Burton - tuglyraisin at aol dot com
> Felecia Station on Harvestgain - Jarod Godel in Second Life
| |
|
|
"Andrew Burton" <tuglyraisin@aol.commcast> wrote in message
news:20040722224528.04675.00000243@mb-m21.aol.com...
> (Bah, I accidentally posted this to c.l.p.moderated. My apologies.)
>
> I'm dickering around with Perl tonight, trying to grasp OOP concepts in
Perl --
> I like them enough in C#, and thought understanding them in my favorite
> language would be worthwhile. To do this, I'm reading through and using
> examples from "Tom's object-oriented tutorial for perl." For some reason
his
> example is giving me bugs, and Google is no help. This is the error I get
with
> the following code:
> Can't modify non-lvalue subroutine call at ./testclass line 27.
>
> Here's the code...
>
> code:
>
> #!/usr/bin/perl -w
>
> use strict;
>
> { package Roxy;
>
> sub new {
> my $self = {};
> $self->{'word'} = undef;
> bless $self;
> return $self;
> }
> sub speak {
> my $self = shift;
> print "$self says 'hello'.\n";
> }
> sub name {
> my $self = shift;
> if (@_) { $self->{'word'} = shift; }
> return $self->{'word'}
> }
> }
>
> my $muse = Roxy->new();
>
> $muse->speak;
> $muse->name = "Andy";
>
> print $muse->name;
>
> exit;
>
>
>
> For whatever it's worth, I'm using Perl 5.8.0 Any help or suggestions
would be
> appreciated. Thanks!
>
> Andrew Burton - tuglyraisin at aol dot com
> Felecia Station on Harvestgain - Jarod Godel in Second Life
was this the actual code from the example? I'm curious...
-Robin
| |
| Sherm Pendley 2004-07-28, 9:00 pm |
| Robin wrote:
> was this the actual code from the example? I'm curious...
If you're that curious, why not type 'perldoc perltoot' and read the
example for yourself?
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
| |
| Joe Smith 2004-07-28, 9:00 pm |
| Sherm Pendley wrote:
> Robin wrote:
>
> If you're that curious, why not type 'perldoc perltoot' and read the
> example for yourself?
You mean 'perldoc perlboot'.
-Joe
| |
| Sherm Pendley 2004-07-28, 9:00 pm |
| Joe Smith wrote:
> Sherm Pendley wrote:
>
>
>
> You mean 'perldoc perlboot'.
No I don't.
The OP wrote:
> To do this, I'm reading through and using
> examples from "Tom's object-oriented tutorial for perl."
That's perltoot, not perlboot.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
| |
| Joe Smith 2004-07-28, 9:00 pm |
| Sherm Pendley wrote:
>
>
> No I don't.
>
> The OP wrote:
>
>
> That's perltoot, not perlboot.
OK, "Tom's object-oriented tutorial for perl" is perldoc perltoot.
Robin wrote: "was this the actual code from the example? I'm curious..."
The OP's sample code does not appear in perltoot but code that is
similar, with "sub speak{}", appears in perldoc perlboot.
The answer to Robin's question is: No, that is not taken verbatim
from the examples in the perl-5.8.3 docs.
-Joe
|
|
|
|
|