>>>>> "SHC" == Shawn H Corey <shawnhco...@gmail.com> writes:

  SHC> On 10-05-16 11:17 PM, Uri Guttman wrote:
  >> it can be used to save data (e.g. a config
  >> file) in a file for reloading in the future (via running the dumper
  >> output with eval).

  SHC> By saving the output of Data::Dumper to a *.pm file, it can be
  SHC> reloaded via "use".

since you followed up my post, you might as well use my module to! :)

  SHC> #!/usr/bin/perl

  SHC> use strict;
  SHC> use warnings;

  SHC> use Data::Dumper;
use File::Slurp ;


  SHC> # Make Data::Dumper pretty
  SHC> $Data::Dumper::Sortkeys = 1;
  SHC> $Data::Dumper::Indent   = 1;

  SHC> # Set maximum depth for Data::Dumper, zero means unlimited
  SHC> local $Data::Dumper::Maxdepth = 0;

why do you localize some but not all of the dumper options? local will
make it work only for a call in this scope which is file scope. better
to also set those inside the sub or use the OO style where the only that
dumper object will have those options.

  SHC> use MyConfig;
  SHC> our $MyConfig;  #  reference to a hash

  SHC> sub save_config {
  SHC>   local $Data::Dumper::Purity = 1;
  SHC>   open my $cfg_fh, '>', 'MyConfig.pm' or die $!;



  SHC>   print {$cfg_fh} 'our ',
  SHC>                   Data::Dumper->Dump( [ $MyConfig ], [ 'MyConfig' ] ),
  SHC>                   "\n\n1;\n"

you don't even need to use our or name the thing. a do call will load up
the file and return its value which is what you dumped. dumper will use
$VAR1 but it will be ignored in the do call and its value will be used.

so you can do this (untested):


        write_file( 'Config.pm', Dumper $config );

note that the dumper call needs to have the right options to make it
pure (as you did). depth is rarely an issue with most common
configs. if you have a recursive or very deep structure, it isn't not
smart to dump it for saving but it can be done.

and some other place load it up with:

        my $config = do( 'Config.pm' ) ;

much simpler than your way.

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to