gpt4 book ai didi

perl - 在 perl 中写在 unblessed 引用上

转载 作者:行者123 更新时间:2023-12-04 19:20:10 24 4
gpt4 key购买 nike

为什么我收到一条错误消息“无法在/usr/... 的 unblessed 引用上调用方法 'write'”
当我在我的 ubuntu 上运行时,根本没有错误,但是当我在我的 gentoo 上运行这段代码时。弹出此错误。我认为操作系统不是这里的问题。但它是什么?

这是我的代码:

#!/usr/bin/perl
#index.cgi

require 'foobar-lib.pl';


ui_print_header(undef, $module_info{'desc'}, "", undef, 1, 1);


ui_print_footer("/", $text{'index'});

use CGI;
use Config::Tiny;
use Data::Dumper;
use CGI::Carp qw(fatalsToBrowser);

#location/directory of configuration file
my $file = "/home/admin_config.conf";
my $Config = Config::Tiny->read($file);

#reads the section, key and the value of the configuration file.
my $status_in_file = $Config->{"offline_online_status"}->{"offline_online_status.offline_online_state"};

print "Content-type:text/html\n\n";

print qq~<html>
<link rel="stylesheet" type="text/css" href="style4.css">
<body>

<div id="content">
<div id="bar">
<span><p>Controller Settings</p></span>
</div>

<div id="tab-container">
<ul>
<li><span><a href="index.cgi">Offline / Online State</a></span></li>
</ul>

</div>

<div id="main-container">

<table border="0" width="100%" height="80%">
<tr>
<td align="left" width="20%">
<div id="title"><span>Offline/Online Status :</span></div>
</td>
<td width="25%">
<table border="0" style=\"text-align:right;font-family:Arial, Helvetica, sans-serif;\" cellpadding="5">
<tr>
<td width="30%"><div id="data">Offline Online State:</div></td>
</tr>
<tr>
<td width="30%"><div id="data">Data Mode:</div></td>
</tr>
</table>
</td>
<td align="left" width="20%">
<table border="1" style=\"text-align:center;font-family:Arial, Helvetica, sans-serif;\" cellpadding="5">
<tr>
<td width="20%"><div id="data">$status_in_file</div></td>
</tr>
</table>
</td>
<td width="50%"></td>
</tr>
<tr>
<td colspan="4">
<div id="description"><p><b>Description :</b></p>
<p>This <i>indication</i> is sent ..</p>
</div>
</td>
</tr>
</table>


</div>
</div>


</body>
</html>

~;

有人可以帮我吗?
这是我的 foobar-lib.pl
=head1 foobar-lib.pl
foreign_require("foobar", "foobar-lib.pl");
@sites = foobar::list_foobar_websites()
=cut
BEGIN { push(@INC, ".."); };
use WebminCore;
init_config();
=head2 get_foobar_config()
=cut
sub get_foobar_config
{
my $lref = &read_file_lines($config{'foobar_conf'});
my @rv;
my $lnum = 0;
foreach my $line (@$lref) {
my ($n, $v) = split(/\s+/, $line, 2);
if ($n) {
push(@rv, { 'name' => $n, 'value' => $v, 'line' => $lnum });
}
$lnum++;
}
return @rv;
}

我也不太了解这个 foobar-lib.pl。当我运行代码时,也许这导致了我的问题?

最佳答案

您显示的代码不会尝试调用名为 write 的方法。在任何事情上,更不用说在未受祝福的引用上。所以我假设方法调用发生在你没有显示的一些代码中。也许在 foobar-lib.pl 中?

因为看不到导致错误的代码,所以只能根据方法调用write的线索进行猜测。 .

在 Perl 中,文件句柄是被归类为“对象”(因此可以调用它们的方法)还是未受祝福的引用(因此不能),这有点模棱两可。这种情况在 Perl 5.12 和 Perl 5.14 中发生了变化。因此,如果您在每台机器上安装了不同版本的 Perl,那么您在尝试执行以下操作时可能会观察到不同的行为:

$fh->write($data, $length)

Perl 5.14+ 的行为可能是您想要的(因为它是最棒的),幸运的是,您可以通过预加载几个模块在早期版本的 Perl 上实现相同的行为。将以下两行添加到脚本的顶部:
use IO::Handle ();
use IO::File ();

问题解决了……也许???

关于perl - 在 perl 中写在 unblessed 引用上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22473863/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com