如何重新定义“开放”? 正确地使用 Perl 吗?

发布于 2024-07-17 21:02:50 字数 780 浏览 11 评论 0原文

前段时间,我问了一个问题: How do I redefinebuilt in Perl功能?

答案对我很有帮助。 我有一个包可以覆盖 Perl 的“打开”功能,使我能够记录文件访问。

现在我遇到了一个破坏原始代码功能的案例。

use strict;
use warnings;
use Data::Dumper;

sub myopen (*;@) {
  my $p;
  my $retval = CORE::open($p, $_[1]);
  {
    no strict;
    *{"main::$_[0]"} = $p;
  }
  return $retval;
}

BEGIN {
  *CORE::GLOBAL::open = *myopen;
};

my @a = (1, 2, 3);

open(CHECK, ">dump") or print "UNABLE TO OPEN DUMPER FILE: $!\n";
print CHECK "test\n";
print CHECK Data::Dumper->Dump(\@a);
close CHECK

现在我收到此消息:

Can't locate object method "CHECK" via package "Data::Dumper"

我该如何解决它?

Some time ago, I ask a question: How do I redefine built in Perl functions?

And the answers have served me well. I have a package that overrides Perl's 'open' function enabling me to log file access.

Now I've come to a case that breaks the functionality of the original code.

use strict;
use warnings;
use Data::Dumper;

sub myopen (*;@) {
  my $p;
  my $retval = CORE::open($p, $_[1]);
  {
    no strict;
    *{"main::$_[0]"} = $p;
  }
  return $retval;
}

BEGIN {
  *CORE::GLOBAL::open = *myopen;
};

my @a = (1, 2, 3);

open(CHECK, ">dump") or print "UNABLE TO OPEN DUMPER FILE: $!\n";
print CHECK "test\n";
print CHECK Data::Dumper->Dump(\@a);
close CHECK

Now I get this message:

Can't locate object method "CHECK" via package "Data::Dumper"

How do I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

总以为 2024-07-24 21:02:50

尝试使用“CHECK”以外的名称

“CHECK”是一个在编译时调用的特殊函数,您确实不应该使用它。

$ open CHECK , '<', 'foo.txt'; 
Took 0.00224494934082031 seconds.

Runtime error: Undefined subroutine &Devel::REPL::Plugin::Packages::DefaultScratchpad::CHECK called at (eval 329) line 5.

$ open CHECKS , '<', 'foo.txt'; 
Took 0.00155806541442871 seconds.

$

为什么会出现这个特定错误?

perl -MO=Deparse -e 'print CHECK Data::Dumper 1';
print 'Data::Dumper'->CHECK(1);

另外,您使用的是全局文件句柄,这是有问题的。

使用这个符号:

open my $fh, '<' , $foo ; 
print <$fh>;
close $fh;

这些额外的好处是当它们超出范围时它们会自动关闭。

Try using a name other than "CHECK".

"CHECK" is a special function which is called during compile time, and you really shouldn't use it.

$ open CHECK , '<', 'foo.txt'; 
Took 0.00224494934082031 seconds.

Runtime error: Undefined subroutine &Devel::REPL::Plugin::Packages::DefaultScratchpad::CHECK called at (eval 329) line 5.

$ open CHECKS , '<', 'foo.txt'; 
Took 0.00155806541442871 seconds.

$

Why that specific error?

perl -MO=Deparse -e 'print CHECK Data::Dumper 1';
print 'Data::Dumper'->CHECK(1);

Also, you're using global file handles, which are problematic.

use this notation:

open my $fh, '<' , $foo ; 
print <$fh>;
close $fh;

These are extra beneficial is they self-close when they go out of scope.

脸赞 2024-07-24 21:02:50

比较:

> perl -MData::Dumper -e'local*_=*STDOUT;print _ Data::Dumper->Dump([2]);'
Can't locate object method "_" via package "Data::Dumper" at -e line 1.

> perl -MData::Dumper -e'local*_=*STDOUT;print _ ( Data::Dumper->Dump([2]) );'
$VAR1 = 2;

使用了与“STDOUT”不同的名称,因为当它不是内置句柄时,它似乎只会使间接对象出错。

Compare:

> perl -MData::Dumper -e'local*_=*STDOUT;print _ Data::Dumper->Dump([2]);'
Can't locate object method "_" via package "Data::Dumper" at -e line 1.

to

> perl -MData::Dumper -e'local*_=*STDOUT;print _ ( Data::Dumper->Dump([2]) );'
$VAR1 = 2;

I used a different name from "STDOUT" because it seems to only gets the indirect object wrong when it's not a built-in handle.

蘸点软妹酱 2024-07-24 21:02:50

这将起作用并且不会产生错误...

 print {*CHECK} Data::Dumper->Dump(\@a);

这会阻止它被混淆有一个 “间接对象语法”

但是,我建议避免使用 CHECK 和其他 Perl 中的特殊命名代码块 和使用文件句柄的词法变量是首选方法。 PBP

This will work and without producing the error...

 print {*CHECK} Data::Dumper->Dump(\@a);

This stops it being confused has an "Indirect Object Syntax"

However I do recommend steering clear of using CHECK and other special named code blocks in Perl and using lexical variables for filehandles is the preferred method. PBP

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文