'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?

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

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


Solution 1:[1]

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;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dave Cross