As per Tillys comments, using Data::Dumper is one of the ways to go
use Data::Dumper; use Carp; use strict; use warnings; sub write_to_file { my ($file,$data,$small)=@_; open my $OUT,">$file" or croak "Error with $file:$!"; my $dumper=Data::Dumper->new([$data],["my_dump"]); $dumper->Indent($small ? 0 : 2); print $OUT $dumper->Dump; close $OUT; return $dumper->Dump; } sub read_from_file { my $file=shift; local $/; open my $IN,"$file" or croak "Error with $file:$!"; my $data=<$IN>; close $IN; my $my_dump; eval $data; croak "from $file I get error $@" if $@; return $my_dump; } my $test={key=>[qw(this is a list)], subhash=>{qw(so is this one but it must have pairs inside)}} +; write_to_file("test_dumper.dmp",$test); my $var=read_from_file("test_dumper.dmp"); print Dumper($test,$var);
Of course you could use Storable, or Data::Denter or a variety of other methods depending on your needs and space/time/memory requirements. But the type of code above can be a simple way to do configuration files and other simple storage requirements.
Note that there can be security concerns doing this kind of thing with eval.

HTH

Yves
--
You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)

Update I see that I must learn to type faster, or check for replys posted since I started to reply. Nice one Amoe. Or post shorter comments. :-)


In reply to Re: Saving a %hash by demerphq
in thread Saving a %hash by smitz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.