For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > February 2005 > Can't use an undefined value as a HASH reference









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Can't use an undefined value as a HASH reference
Josh McAdams

2005-02-04, 8:55 pm

In the example below, I am using Data::Dumper to print the data
structure for the hash, %h, and then using eval to process the data
structure back into a hash-reference that I dereference into a hash and
print the keys.

This first example works fine:

<code>
perl -MData::Dumper -e 'print join "\n", keys %{eval(`perl
-MData::Dumper -e "print Dumper(\\\%h)"`)}'
</code>

This second example, with the strict pragma turned on causes and error
message "Can't use an undefined value as a HASH reference"

<code type="broken">
perl -MData::Dumper -e 'print join "\n", keys %{eval(`perl
-MData::Dumper -Mstrict -e "print Dumper(\\\%h)"`)}'
</code>

In reality, this section of code is part of a larger script and to
suppress the error, I use "no strict 'vars'" in the block of code that
creates the hash. However, I don't like doing that. Is there a way to
have this code work with strict?

Thanks,
Josh

Josh McAdams

2005-02-04, 8:55 pm

Josh McAdams wrote:

> In the example below, I am using Data::Dumper to print the data
> structure for the hash, %h, and then using eval to process the data
> structure back into a hash-reference that I dereference into a hash and
> print the keys.
>
> This first example works fine:
>
> <code>
> perl -MData::Dumper -e 'print join "\n", keys %{eval(`perl
> -MData::Dumper -e "print Dumper(\\\%h)"`)}'
> </code>
>
> This second example, with the strict pragma turned on causes and error
> message "Can't use an undefined value as a HASH reference"
>
> <code type="broken">
> perl -MData::Dumper -e 'print join "\n", keys %{eval(`perl
> -MData::Dumper -Mstrict -e "print Dumper(\\\%h)"`)}'
> </code>
>
> In reality, this section of code is part of a larger script and to
> suppress the error, I use "no strict 'vars'" in the block of code that
> creates the hash. However, I don't like doing that. Is there a way to
> have this code work with strict?
>
> Thanks,
> Josh
>


Oops, the second code block really was broken, try this instead:

<code type="broken">
perl -MData::Dumper -Mstrict -e 'print join "\n", keys %{eval(`perl
-MData::Dumper -e "print Dumper(\\\%h)"`)}'
</code>
Big and Blue

2005-02-05, 3:55 am

Josh McAdams wrote:
> Oops, the second code block really was broken, try this instead:
>
> <code type="broken">
> perl -MData::Dumper -Mstrict -e 'print join "\n", keys %{eval(`perl
> -MData::Dumper -e "print Dumper(\\\%h)"`)}'
> </code>


Well, %h isn't defined. Not sure why you expect Perl to somehow ignore
this fact when you tell it to trap such things. You're trying to have your
cake and eat it.


--
Just because I've written it doesn't mean that
either you or I have to believe it.
Josh McAdams

2005-02-05, 3:55 am

Big and Blue wrote:
> Josh McAdams wrote:
>
>
>
> Well, %h isn't defined. Not sure why you expect Perl to somehow
> ignore this fact when you tell it to trap such things. You're trying to
> have your cake and eat it.
>
>


Poor example on my part, here is the real code:

perl -MData::Dumper -Mstrict -e 'print join "\n", keys %{eval(`perl
-MData::Dumper -e "print Dumper(\\\%ENV)"`)}'
Big and Blue

2005-02-05, 3:55 am

Josh McAdams wrote:
>
> Poor example on my part, here is the real code:
>
> perl -MData::Dumper -Mstrict -e 'print join "\n", keys %{eval(`perl
> -MData::Dumper -e "print Dumper(\\\%ENV)"`)}'


The inner perl command produces output of the form:

$VAR1 = \{k1 => v1, k2 => v2..... };

You are using the result of eval()ing that as a HASH reference, which seems
odd to me.

What do you actually wish to achieve?

--
Just because I've written it doesn't mean that
either you or I have to believe it.
Josh McAdams

2005-02-05, 3:55 am

Big and Blue wrote:
> Josh McAdams wrote:
>
>
>
> The inner perl command produces output of the form:
>
> $VAR1 = \{k1 => v1, k2 => v2..... };
>
> You are using the result of eval()ing that as a HASH reference, which
> seems odd to me.
>
> What do you actually wish to achieve?
>


The goal is to import the environment from a shell script into the
current Perl scripts environment. To do this, I am sourcing the shell
script and printing it's environment to standard output:

qx{
. /shell/script.sh
perl -MData::Dumper -e "print Dumper(\\\%ENV)"
}

This is occurring within a containing Perl script that rebuilds the hash
from Data::Dumper's output and then cycles through the hash picking out
changed environment variables.

I've gotten everything working pretty well right now, but am having to
turn off strict 'vars' for this hash-rebuilding section.

Thanks for all of your help!
Josh McAdams

2005-02-05, 3:55 pm

Josh McAdams wrote:
> Big and Blue wrote:
>
>
> The goal is to import the environment from a shell script into the
> current Perl scripts environment. To do this, I am sourcing the shell
> script and printing it's environment to standard output:
>
> qx{
> . /shell/script.sh
> perl -MData::Dumper -e "print Dumper(\\\%ENV)"
> }
>
> This is occurring within a containing Perl script that rebuilds the hash
> from Data::Dumper's output and then cycles through the hash picking out
> changed environment variables.
>
> I've gotten everything working pretty well right now, but am having to
> turn off strict 'vars' for this hash-rebuilding section.
>
> Thanks for all of your help!


I finally found the answer... Data::Dumper returns it's output as:

$VAR1 = { ...

In this case, $VAR1 is not scoped, so strict complains. When I changed
the code to:

perl -MData::Dumper -Mstrict -e 'print join "\n", keys %{eval('my ' .
`perl -MData::Dumper -e "print Dumper(\\\%ENV)"`)}'

it worked.

Thanks again for all of the help.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com