use constant and BEGIN don't mix?

J

J Krugman

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

Many thanks!

jill
 
J

J Krugman

In said:
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 };
}



Sorry, I omitted an important line ("use strict;") in the snippets
above. I should have written:

If I try

use strict;
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

use strict;
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.)
 
S

Steven Kuo

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

Anno Siegel

J Krugman said:
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 has to do with Perl's parsing of "do" statements. With the
BLOCK and EXPR forms, plus the deprecated SUBROUTINE form, there is
a bit of ambiguity to resolve, and Perl gets it wrong here. I'm not
sure what exactly is going on, but writing "do" with parentheses will
keep Perl on track:

use constant DO_FILE => '/tmp/x';
%x = %{ do( DO_FILE) };

will work, with or without BEGIN.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top