简体   繁体   English

解决 __DATA__ 不是文件句柄这一事实的任何方法,即使它在所有文档中都以这种方式引用

[英]Any way to work around the fact that __DATA__ is not a filehandle, even though it's referred that way in all the documentation

All the documentation I've been reading about __DATA__ says stuff like, "the special filehandle" but it's not a filehandle is it?我一直在阅读的有关__DATA__的所有文档都说诸如“特殊文件句柄”之类的东西,但它不是文件句柄吗?

https://perldoc.perl.org/perldata#Special-Literals "The DATA file handle by default has whatever PerlIO layers were in place when Perl read the file to parse the source." https://perldoc.perl.org/perldata#Special-Literals “默认情况下,当 Z0114AD06D728F1834E36FE1A3957.4 解析源文件时,DATA 文件句柄具有任何 PerlIO 层。”

#!/bin/env perl                                                                                                                                                                                   

open($config, "<test.pl");

$config_copy = $config; # let's me copy a filehandle

while (<$config_copy>) {
  print $_;  # works just fine
}

$config_copy = __DATA__;  # FAIL syntax error does not let me copy a filehandle

while (<$config_copy>) {
  print $_;
}

__DATA__                                                                                                                                                                                          
1                                                                                                                                                                                                 
2                                                                                                                                                                                                 
3                                                                                                                                                                                                 
4                                                                                                                                                                                                 
5          

I basically want to hand a filehandle to a config file reader function OR pass in __DATA__ if it exists, but the reader function is in a different package than the __DATA__ segment, so I need to pass __DATA__ as a parameter because __DATA__ is only accessible from the same package or file it's declared in but perl is not treating __DATA__ like other file handles. I basically want to hand a filehandle to a config file reader function OR pass in __DATA__ if it exists, but the reader function is in a different package than the __DATA__ segment, so I need to pass __DATA__ as a parameter because __DATA__ is only accessible from相同的 package 或在其中声明的文件,但 perl 不像其他文件句柄那样处理__DATA__ It can't be assigned or passed as a function argument.它不能作为 function 参数分配或传递。

------ package ConfigReader ------------------
package ConfigReader;

sub ReadConfig {
   my ($handle) = @);
   while($handle) {
       # blah blash
   }
}

----------- application ------------------

use ConfigReader;
    
# it won't let me set a scalar to __DATA__.
# but it lets scalars be set to other file handles.

my $config_handle = __DATA__ # set up default, FAIL syntax error
open($config_handle, "<$config_file_name")

my $configs = ConfigReader::ReadConfig($config_handle);

--------------------------

As you quote from the documentation:正如您从文档中引用的那样:

The DATA file handle by default has whatever PerlIO layers were in place when Perl read the file to parse the source.当 Perl 读取文件以解析源代码时,默认情况下, DATA文件句柄具有任何 PerlIO 层。

It also says this:它还说:

Text after __DATA__ may be read via the filehandle PACKNAME::DATA , where PACKNAME is the package that was current when the __DATA__ token was encountered.可以通过文件句柄PACKNAME::DATA读取 __DATA__ 之后的文本,其中PACKNAME是遇到 __DATA__ 令牌时的当前 package。

The filehandle is called DATA , not __DATA__ .文件句柄称为DATA ,而不是__DATA__ In some cases, you might need to refer to it by taking a reference to the glob that contains it - \*DATA - but those occasions seem to be rare these days.在某些情况下,您可能需要通过引用包含它的 glob 来引用它 - \*DATA - 但这些日子似乎很少见。

So try:所以试试:

$config_copy = DATA;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM