On Fri, 30 Apr 2004, J Krugman wrote:
>
> If I try
>
> my %x;
> BEGIN {
> %x = %{ do '/path/to/some/file' };
> }
>
> ...where the file '/path/to/some/file' consists of the string
> "+{ A => 1}\n", then everything is fine, but if instead I do
>
> my %x;
> BEGIN {
> use constant DO_FILE => '/path/to/some/file';
> %x = %{ do DO_FILE };
> }
>
> ...I get an error ("Can't use an undefined value as a HASH reference").
> (I get this error whether I place the "use constant" statement
> inside or before the BEGIN block.)
>
> Is there no way to perform a do operation in a BEGIN block using
> a constant? Can someone explain to me, please, what's going on?
> (Yes, I did read perlmod; it didn't help me with this one.)
The problem is unrelated to the BEGIN block.
You can force the context to a subroutine call (you'll have to study
constant.pm to see why):
use strict;
use warnings;
my %x;
BEGIN {
use constant DOFILE => '/path/to/file';
%x = %{ do +DOFILE() };
}
$\ = "\n";
$, = " ";
print keys %x, values %x;
print "My do file is", DOFILE;
Though I have to ask: why use such an awkward method to define a
hash?
--
Hope this helps,
Steven
|