Jsp java subroutine_Perl错误:无法修改非左值子例程调用(Perl error: Can't modify non-lvalue subroutine call at)...

这篇博客讨论了一个在Perl编程中遇到的常见错误——'Can't modify non-lvalue subroutine call'。作者分享了在file.do文件第26行触发该错误的代码片段,并提供了BookController.pm模块的相关代码。问题在于尝试修改子例程的返回值而非对象的属性。解决方案是正确调用对象的方法,如`$bookdb->dbh(0);`而不是`$bookdb->dbh = 0;`。
摘要由CSDN通过智能技术生成

Perl错误:无法修改非左值子例程调用(Perl error: Can't modify non-lvalue subroutine call at)

我的班级出现以下错误:“无法在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;

有什么建议么?

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?

原文:https://stackoverflow.com/questions/9551819

更新时间:2020-03-02 12:59

最满意答案

你试图设置sub的返回值,因此是错误。 从实际的方法来看,我认为你的意思是:

$bookdb->dbh(0);

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);

2012-03-04

相关问答

你不能取一个函数返回值的地址 - 它是短暂的。 你需要使用一个真正的变量: Nametype first_name = name.getFirstName();

Database.bindWhereClause( "FIRST_NAME", SQL_VARCHAR, first_name);

// ... and maybe name.setFirstName(first_name); here

使用内联函数可以使其编译,但实际上不太可行 。 据推测,接下来是: Database.execut

...

你需要引用你的字符串。 您不能使用双引号""来执行此操作,因为您已将它们用于-e标志的shell参数。 请使用单引号''或引号运算符q或qq 。 $ perl -e "require 'burt.pm'; file('/u/path', '/u/build');"

在单行中使用q一般是个好主意,这样就不会干扰shell引用。 在任何情况下,在Linux系统上,您可能希望对-e之后的Perl程序使用单引号'' ,因为双引号""启用了shell的引用机制。 在Perl中,双引号""启用插值,因此填充

...

海事组织,唯一的时候有任何理由使用&是如果你正在获取或打电话给代码,如: sub foo() {

print "hi\n";

}

my $x = \&foo;

&$x();

在大多数情况下绝对不应该使用它的主要时间是调用具有指定任何非默认调用行为的原型的子。 我的意思是,一些原型允许重新解释参数列表,例如将@array和%hash规范转换为引用。 所以这个sub会期待这些重新解释的发生,除非你去任何必要的手段来模仿它们,否则这个sub会得到与之前预期的不同的输入。 我认为主要是人们正在

...

间接方法符号再次出现! [1] factorial { ... }

被解析为 (do { ... })->factorial

问题是你在sub声明的开头缺少sub关键字。 更换 factorial { ... }

同 sub factorial { ... }

此外,子程序参数在@_中提供,而不是@ARGV ,因此 my $n = $ARGV[0];

应该 my $n = $_[0];

-or-

my $n = shift;

-or-

my ($n) = @_;

最后,使用递归

...

Moose对象属性隐藏在同名函数后面,所以当你有$self->grow_params这是一个函数时。 试图为它分配一个值是行不通的,你需要像函数一样调用它。 $self->grow_params($params);

但即便如此,由于您当前将grow_params定义为只读,因此一旦创建了对象,即使在其自己的方法中,也无法更改它的值。 Moose object attributes are hidden behind functions of the same name, so when you

...

你不能修改一个const引用,试试这个: //Snippet1

Array& integers4=integers1;

integers4[3] = 2000;

为了澄清OP对修改const引用的怀疑: //Snippet2

//This will not compile as you saw.

const Array& integers4=integers1;

integers4[3] = 2000; //errors

这是Xeo在回复这篇文章时所做的 。 他没有改变参考,而是改变了原来的

...

很难说出现错误,不知道MyModule.pm什么。 @INC看起来没问题( .在列表中,所以在当前目录中找到MyModule.pm应该没有问题)。 这是一个按照您描述的方式工作的最小示例。 希望能帮助到你。 $ cat SomeModule.pm

package SomeModule;

sub testsub

{

return "it works\n";

}

1;

$ VAL=`perl -I. -MSomeModule -e 'print SomeModule::testsub'`

$

...

你的'admin_view'子进行打印。 因此,您不需要像这样调用它: print &admin_view;

因为通过这样做,它打印子的返回码。 这是没有显式返回的最后一个命令的结果,因此向我们显示print成功。 试试吧 admin_view;

(失去了它的坏风格) Your 'admin_view' sub does the print. Thus you don't need to call it like this: print &admin_view;

Because by doi

...

有一个主要的程序 #! /usr/bin/env perl

use strict;

use warnings;

use MyModule;

sub mainsub {

print "Original mainsub\n";

}

mainsub;

并包含MyModule.pm package MyModule;

use strict;

use warnings;

my $referenceToOriginalSub;

sub overrideSub {

print "Over

...

你试图设置sub的返回值,因此是错误。 从实际的方法来看,我认为你的意思是: $bookdb->dbh(0);

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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值