3

All the documentation I've been reading about __DATA__ says stuff like, "the special filehandle" but it's not a filehandle is it?

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."

#!/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. It can't be assigned or passed as a function argument.

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

--------------------------
brian d foy
  • 129,424
  • 31
  • 207
  • 592
Walt Howard
  • 7,676
  • 1
  • 16
  • 11

1 Answers1

6

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.

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.

The filehandle is called DATA, not __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.

So try:

$config_copy = DATA;
Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • Great! Thanks. My intuition was telling me that any "internal" data, including a special file handle, would retain the underscores __DATA__ to avoid collision with user symbols. But that only goes so far. (stack overflow swallows underscores) – Walt Howard Jan 28 '22 at 18:16
  • 1
    Re "*but those occasions seem to be rare these days.*", Well, any time you want to pass it to a sub – ikegami Jan 28 '22 at 22:01
  • 1
    DATA sets itself aside from other symbols by being in all caps. Several other special symbols in Perl do that too. – brian d foy Jan 28 '22 at 22:08