peterbrown has asked for the wisdom of the Perl Monks concerning the following question:

Hi Folks,

I just ran into a problem that I haven't been able to figure out or find an answer to in the Data::Dumper documentation. I hope a Perl Genius can give me a hand :-).

For quite a long time, for a number of different clients, I've been using Data::Dumper to store different types of hashes of hashes to files. They usually have 3 or 4 levels to them.

They syntax that I've always used to print the hash of hashes to a file (successfully until now) is:

use Data::Dumper;
... open file
print FILE Data::Dumper->Dump([\%hash], [*hash]);
... close file


(where 'hash' is the hash name)

It has always worked flawlessly until yesterday.

With this particular script, running on a Linux system with Perl 6.0, the output replaced the usual hash name header, e.g:

%main::email_addresses = (

... with

$VAR1 = {

It also replaced the closing ); with a };

(That is, replacing the () parens with {} curly brackets and replacing the %hash syntax with $VAR1.

Thus, of course, when I tried to read the hash from the file, it didn't exist as a hash.

My kludgy work around was to replace the $VAR1 and other incorrect elements in the file with the right ones.

I noted in the Data::Dumper documentation the line that stated:

"Any references that are the same as one of those passed in will be named $VARn...".

However, in this case the name of the hash is being replaced. Yet, the hash name is not being duplicated anywhere.

This behavior has never happened to me, and I'm completely puzzled.

I also tried to use the statement:

$Data::Dumper::Sortkeys = 1;

in order to force the keys to sort, but when I did so, the hash was completely wiped out.

Does anyone have any thoughts?

Thank you!

Peter Brown

Replies are listed 'Best First'.
Re: Problems with Data::Dumper
by fglock (Vicar) on Sep 27, 2003 at 03:58 UTC

    How about

    perl -MData::Dumper -e " $a = 1; print Data::Dumper->Dump( [$a], [qw(* +a)] ) " $a = 1;

    instead of

    perl -MData::Dumper -e " $a = 1; print Data::Dumper->Dump( [$a], [*a] +) " $VAR1 = 1;
      Thanks...

      So, the "qw(" and ")" makes a difference? Why would it work on most systems without the ()s but not on that one system?

      Thank you very much!

      I just modified my Data::Dumper line and it worked with the qw(*hash_name) inside the square brackets.

      I'm still curious, however... why does it work on some systems without the qw( ) and not on others. What exactly does the use of parens do at that point in the code?

      Thanks again!

      Peter

        Perl was "fixed" between 5.005_03 and 5.6.1, such that it better understands references.

        If you run this program under 5.005_03, it (wrongly) understands *a as a string. Under 5.6.1 or later, you have to say "*a" to get a string.

Re: Problems with Data::Dumper
by BazB (Priest) on Sep 27, 2003 at 20:30 UTC

    With this particular script, running on a Linux system with Perl 6.0...

    What! Perl 6 is out!? Already?! :-)


    If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
    That way everyone learns.

      Oy gevalt. What a typo!

      I meant, of course, Perl v5.6.0.

      It was a long day :-).

      Thanks for the correction.
      Peter