Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > use constant and BEGIN don't mix?

Reply
Thread Tools

use constant and BEGIN don't mix?

 
 
J Krugman
Guest
Posts: n/a
 
      04-30-2004



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

--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.

 
Reply With Quote
 
 
 
 
J Krugman
Guest
Posts: n/a
 
      04-30-2004
In <c6u7e7$es9$(E-Mail Removed)> J Krugman <(E-Mail Removed)> writes:

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




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


>Many thanks!


>jill

--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.

 
Reply With Quote
 
 
 
 
Steven Kuo
Guest
Posts: n/a
 
      04-30-2004
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

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      05-01-2004
J Krugman <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
>
>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"use constant X=>(1,2);" or "use constant X=>[1,2];"? Victor Porton Perl Misc 7 12-11-2007 09:10 PM
pointers to constant characters and constant pointers to characters sam_cit@yahoo.co.in C Programming 4 12-14-2006 11:10 PM
len(var) is [CONSTANT] equal to len(var) == [CONSTANT]? Tor Erik Soenvisen Python 14 11-23-2006 09:57 PM
"Non-constant" constant can't be used as template argument Martin Magnusson C++ 2 10-08-2004 08:41 AM
Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files Christopher M. Lusardi C++ 1 09-02-2004 07:43 AM



Advertisments