Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Any module for saving data as perl data?

by PerlBroker (Acolyte)
on Apr 29, 2016 at 07:25 UTC ( [id://1161853]=perlquestion: print w/replies, xml ) Need Help??

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

Is there any module or way, to simply save data as Perl, for example if I have $a = "hello", that I can save it in exact that form in a file: $a = "hello"; including the same for hashes and arrays?
  • Comment on Any module for saving data as perl data?

Replies are listed 'Best First'.
Re: Any module for saving data as perl data?
by Discipulus (Canon) on Apr 29, 2016 at 07:27 UTC
    Storable is a core module since ever. It does exactly what you ask for. See it's synopsis for examples.

    Data::Dumper is another core module that can store Perl's data to disk.

    In addition many other modules can store Perl datastructures into a file and retrieve them later: for example YAML

    Every module has some limitation or feature: read a this thread where afoken describes Data::Dumper JSON Storable YAML differences and others point to other possibilies to accomplish with your task.

    update: In the above thread i point to Sereal-Comparison-Graphs which can be intersting too, being Sereal a worth to try module.

    I used to convert a a datastructure (an array) from Storable to YAML and viceversa with two simple oneliners:

    # WARNING windows quotes # storable to yaml perl -MYAML -MStorable -e "print Dump @{retrieve ($ARGV[0])};" # yaml to storable perl -e "use YAML (LoadFile); use Storable qw(nstore); @ar = LoadFile( +$ARGV[0]); nstore(\@ar, $ARGV[1])"

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Any module for saving data as perl data?
by Ovid (Cardinal) on Apr 29, 2016 at 10:17 UTC

    It's a strange request and makes me wonder what you're actually trying to do (and thus, if you're not barking up the wrong tree). However, you want to save the variable name with the data? There are several options, none of which are particularly great.

    If you can hard-code the variable name, you can try this syntax from Data::Dumper:

    use Data::Dumper; my $a = 'hello'; my @foo = qw(1 2 3); my %bar = (this => 'that', one => 'un'); print Data::Dumper->Dump( [$a, \@foo, \%bar], [qw/$a *foo *bar/] );

    That's probably your best bet. If for some reason you can't hard-code the names into the call to the Dump() method, you could install my Data::Dumper::Names module:

    use Data::Dumper::Names; my $a = 'hello'; my @foo = qw(1 2 3); my %bar = (this => 'that', one => 'un'); print Dumper( $a, \@foo, \%bar );

    That uses PadWalker to get the variable names and thus only works on lexical variables. If you're desperate and are willing to try a source filter, you can try my Data::Dumper::Simple module:

    use Data::Dumper::Simple; my $a = 'hello'; my @foo = qw(1 2 3); my %bar = (this => 'that', one => 'un'); print Dumper( $a, @foo, %bar );

    However, that's a source filter. It will break. Be warned :)

Re: Any module for saving data as perl data?
by karlgoethebier (Abbot) on Apr 29, 2016 at 09:01 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1161853]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-12-06 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (39 votes). Check out past polls.