JohnnyAce
JohnnyAce

Reputation: 3749

Perl error: Can't modify non-lvalue subroutine call at

I get the following error with my class: "Can't modify non-lvalue subroutine call at file.do line 26." My file.do looks something like this:

line 2:    use BookController;
line 3:    my $bookdb = BookController->new();
...
line 26:   $bookdb->dbh = 0;

And my BookController.pm looks like this:

#!/usr/bin/perl

package BookController;
use strict;

sub new
{
    my $this = shift;
    my $class = ref($this) || $this;

    my $self = {};
    $self->{DBH} = undef;

    bless $self, $class;

    return ($self);
}

sub dbh
{
    my $self = shift;
    $self->{DBH} = shift if (@_);
    return $self->{DBH};
}

1;

Any suggestions?

Upvotes: 4

Views: 5320

Answers (1)

Ry-
Ry-

Reputation: 225164

You're attempting to set the return value of the sub, hence the error. Judging by the actual method, I think you meant:

$bookdb->dbh(0);

Upvotes: 11

Related Questions