in reply to Re: map and each?
in thread map and each?

Because here's my real problem:

sub genHash { my $hashn = shift; return "\%$hashn = (\n" . join(",\n", map { $key = $_; "'$key' => " . (ref $$hashn{$_} ? '{' . join(",\n", map { "'$_' => qq~" . safeTilde($$hashn{$key}->{$_}) . '~' } keys %{$$hashn{$key}}) . '}' : 'qq~' . safeTilde($$hashn{$key}) . '~' ) } keys %$hashn) . ");\n"; } sub safeTilde { my $code = shift; $code =~ s/\~/\\~/g; return $code; }
then you would call it by genHash(\%somehash)

though it does work, it's just is a little long and messy..

Oh, by the way, it takes a hash and generates the perl to re-create it :)

Edit kudra, 2002-05-27 s/pre/code

Replies are listed 'Best First'.
Re: Re: Re: map and each?
by Kanji (Parson) on May 27, 2002 at 02:39 UTC
    Oh, by the way, it takes a hash and generates the perl to re-create it :)

    Have you tried Data::Dumper?

    It excels at what you're trying to do, and can stringify all sorts of Perl data structures in addition to hashes.

    use Data::Dumper; my $foo = Data::Dumper->Dump( [ \%ENV ], [qw( ENV )] );

        --k.


•Re: Re: Re: map and each?
by merlyn (Sage) on May 27, 2002 at 13:49 UTC
    Oh, by the way, it takes a hash and generates the perl to re-create it :)
    As long as the value doesn't contain dollar signs or at-signs or backslashes, since you used qq instead of q. Oh, and the key can't contain any single quotes.

    I agree with the other poster: don't reinvent Data::Dumper without understanding everything it does first.

    -- Randal L. Schwartz, Perl hacker