【问题标题】:Perl error: Can't modify non-lvalue subroutine call atPerl 错误:无法修改非左值子例程调用
【发布时间】:2012-03-04 02:31:46
【问题描述】:

我的班级出现以下错误:“无法在 file.do 第 26 行修改非左值子例程调用。”我的 file.do 看起来像这样:

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

我的 BookController.pm 看起来像这样:

#!/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;

有什么建议吗?

【问题讨论】:

    标签: perl class function


    【解决方案1】:

    您正在尝试设置sub 的返回值,因此出现错误。从实际的方法来看,我想你的意思是:

    $bookdb->dbh(0);
    

    【讨论】:

    • 我在使用 moose 时经常看到这一点。接受一些观点。
    • ...虽然undef0 更有意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多