为什么我必须在Perl裸字文件句柄前使用*?

前端之家收集整理的这篇文章主要介绍了为什么我必须在Perl裸字文件句柄前使用*?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在尝试这样做时:
my $obj = new JavaScript::Minifier;
 $obj->minify(*STDIN,*STDOUT);
// modified above line to
 $obj->minify(*IP_HANDLE,*OP_HANDLE)

上面的工作如果IP_HANDLE和OP_HANDLE是文件句柄但仍然无法弄清楚当应用于文件句柄或任何其他数据类型时*实际上做了什么.

谢谢,

解决方法

在perl v5.6之前的糟糕旧时代,introduced lexical filehandles – 超过 a decade ago now – 传递文件和目录句柄很尴尬.您问题的代码是使用这种老式风格编写的.

例如,* STDIN的技术名称是一个typeglob,在“Typeglobs and Filehandles” section of perldata中进行了解释.在遗留代码中,您可能会因为各种目的而遇到typeglobs的操作.请注意,您可以仅获取全局变量的类型,而不是词法.

传递句柄是直接处理typeglobs的常见目的,但也有其他用途.请参阅下文了解详情.

>将文件句柄传递给subs
>语法歧义:字符串或文件句柄
>通过typeglob赋值的别名
>通过本地化typeglobs来本地化句柄
>偷看引擎盖:* foo {THING}语法
>将它们捆绑在一起:DWIM!

文件句柄传递给subs

@L_502_3@:

Typeglobs and Filehandles

Perl uses an internal type called a typeglob to hold an entire symbol table entry. The type prefix of a typeglob is a * because it represents all types. This used to be the preferred way to pass arrays and hashes by reference into a function,but now that we have real references,this is seldom needed.

[…]

Another use for typeglobs is to pass filehandles into a function or to create new filehandles. If you need to use a typeglob to save away a filehandle,do it this way:

06000

or perhaps as a real reference,like this:

06001

See 07004 for examples of using these as indirect filehandles in functions.

referenced section of perlsub在下面.

Passing Symbol Table Entries (typeglobs)

WARNING: The mechanism described in this section was originally the only way to simulate pass-by-reference in older versions of Perl. While it still works fine in modern versions,the new reference mechanism is generally easier to work with. See below.

Sometimes you don’t want to pass the value of an array to a subroutine but rather the name of it,so that the subroutine can modify the global copy of it rather than working with a local copy. In Perl you can refer to all objects of a particular name by prefixing the name with a star: *foo. This is often known as a “typeglob,” because the star on the front can be thought of as a wildcard match for all the funny prefix characters on variables and subroutines and such.

When evaluated,the typeglob produces a scalar value that represents all the objects of that name,including any filehandle,format,or subroutine. When assigned to,it causes the name mentioned to refer to whatever * value was assigned to it. […]

请注意,typeglob只能用于全局变量,而不能用于词法.注意上面的警告.更喜欢避免这种模糊的技术.

句法歧义:字符串还是文件句柄?

没有* sigil,裸字只是一个字符串.

简单的字符串有时就足够了.例如,print操作符允许

$perl -le 'print { "STDOUT" } "Hiya!"'
Hiya!

$perl -le '$h="STDOUT"; print $h "Hiya!"'
Hiya!

$perl -le 'print "STDOUT" +123'
123

这些失败并启用严格的’refs’.手册解释说:

FILEHANDLE may be a scalar variable name,in which case the variable contains the name of or a reference to the filehandle,thus introducing one level of indirection.

在您的示例中,请考虑语法歧义.没有* sigil,你可能意味着字符串

$perl -MO=Deparse,-p prog.pl
use JavaScript::Minifier;
(my $obj = 'JavaScript::Minifier'->new);
$obj->minify('IP_HANDLE','OP_HANDLE');

或者是一个子呼叫

$perl -MO=Deparse,-p prog.pl
use JavaScript::Minifier;
sub OP_HANDLE {
    1;
}
(my $obj = 'JavaScript::Minifier'->new);
$obj->minify('IP_HANDLE',OP_HANDLE());

或者当然是文件句柄.请注意上面的示例中,bareword JavaScript :: Minifier如何编译为一个简单的字符串.

启用严格的编译指示,无论如何它都会出现在窗口中:

$perl -Mstrict prog.pl
Bareword "IP_HANDLE" not allowed while "strict subs" in use at prog.pl line 6.
Bareword "OP_HANDLE" not allowed while "strict subs" in use at prog.pl line 6.

别名通过typeglob赋值

Stack Overflow帖子的一个技巧就是使用typeglobs

*ARGV = *DATA;

(我可以更精确地使用* ARGV = * DATA {IO},但这有点挑剔.)

这允许钻石操作者<>从DATA文件句柄中读取,如

#! /usr/bin/perl

*ARGV = *DATA;   # for demo only; remove in production

while (<>) { print }

__DATA__
Hello
there

这样,程序及其输入可以在一个文件中,并且代码与它在生产中的外观更接近:只需删除typeglob赋值.

通过本地化typeglobs来本地化句柄

noted in perlsub

Temporary Values via local()

WARNING: In general,you should be using my instead of local,because it’s faster and safer. Exceptions to this include the global punctuation variables,global filehandles and formats,and direct manipulation of the Perl symbol table itself. local is mostly used when the current value of a variable must be visible to called subroutines. […]

你可以使用typeglobs本地化文件句柄:

$cat prog.pl
#! /usr/bin/perl

sub foo {
  local(*STDOUT);
  open STDOUT,">","/dev/null" or die "$0: open: $!";
  print "You can't see me!\n";
}

print "Hello\n";
foo;
print "Good bye.\n";

$./prog.pl
Hello
Good bye.

“When to Still Use local()” in perlsub有另一个例子.

2. You need to create a local file or directory handle or a local function.

A function that needs a filehandle of its own must use local() on a complete typeglob. This can be used to create new symbol table entries:

06009

要强调,这种风格是老式的.更喜欢在新代码中避免使用全局文件句柄,但能够理解现有代码中的技术是有用的.

在引擎盖下偷看:* foo {THING}语法

你可以得到一个typeglob的不同部分,正如perlref所解释的那样:

A reference can be created by using a special Syntax,lovingly known as the *foo{THING} Syntax. *foo{THING} returns a reference to the THING slot in *foo (which is the symbol table entry which holds everything known as foo).

060010

All of these are self-explanatory except for *foo{IO}. It returns the IO handle,used for file handles (open),sockets (socket and socketpair),and directory handles (opendir). For compatibility with prevIoUs versions of Perl,*foo{FILEHANDLE} is a synonym for *foo{IO},though it is deprecated as of 5.8.0. If deprecation warnings are in effect,it will warn of its use.

*foo{THING} returns undef if that particular THING hasn’t been used yet,except in the case of scalars. *foo{SCALAR} returns a reference to an anonymous scalar if $foo hasn’t been used yet. This might change in a future release.

*foo{IO} is an alternative to the *HANDLE mechanism given in [“Typeglobs and Filehandles” in perldata] for passing filehandles into or out of subroutines,or storing into larger data structures. Its disadvantage is that it won’t create a new filehandle for you. Its advantage is that you have less risk of clobbering more than you want to with a typeglob assignment. (It still conflates file and directory handles,though.) However,if you assign the incoming value to a scalar instead of a typeglob as we do in the examples below,there’s no risk of that happening.

060011

捆绑在一起:DWIM!

上下文是Perl的关键.在您的示例中,虽然语法可能不明确,但意图不是:即使参数是字符串,这些字符串显然也是为了命名文件句柄.

因此,请考虑minify可能需要处理的所有案例:

>赤字
>裸色球
>参考typeglob
>标量中的文件句柄

例如:

#! /usr/bin/perl

use warnings;
use strict;

*IP_HANDLE = *DATA;
open OP_HANDLE,">&STDOUT";
open my $fh,">&STDOUT";
my $offset = tell DATA;

use JavaScript::Minifier;
my $obj = JavaScript::Minifier->new;
$obj->minify(*IP_HANDLE,"OP_HANDLE");

seek DATA,$offset,0 or die "$0: seek: $!";
$obj->minify(\*IP_HANDLE,$fh);

__DATA__
Ahoy there
matey!

作为图书馆作者,住宿可能很有用.为了说明,JavaScript :: Minifier的以下存根理解传递文件句柄的传统方式和现代方式.

package JavaScript::Minifier;

use warnings;
use strict;

sub new { bless {} => shift }

sub minify {
  my($self,$in,$out) = @_;

  for ($in,$out) {
    no strict 'refs';
    next if ref($_) || ref(\$_) eq "GLOB";

    my $pkg = caller;
    $_ = *{ $pkg . "::" . $_ }{IO};
  }

  while (<$in>) { print $out $_ }
}

1;

输出

$./prog.pl
Name "main::OP_HANDLE" used only once: possible typo at ./prog.pl line 7.
Ahoy there
matey!
Ahoy there
matey!
原文链接:https://www.f2er.com/Perl/172874.html

猜你在找的Perl相关文章