To add to samurai_'s suggestion perhaps you could consider using Data::Dumper to serialise the data.
output:#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %cnf = (state_file => 'state.txt'); my $first_num = 10.0095; my $last_num = 72.0967; my $mem_first = q{77xptj52}; my %hash = (one => 1); my @array = qw{one two three}; save_state( { first_num => $first_num, last_num => $last_num, mem_first => $mem_first, hash => \%hash, array => \@array, } ); my $state = load_state(); print "first_num: $state->{first_num}\n"; print Dumper $state; sub save_state{ my ($state) = @_; my $state_file = $cnf{state_file}; open my $fh, '>', $state_file or die $!; print $fh Data::Dumper->Dump([$state], [qw(S)]); close $fh; } sub load_state { our $S; my $state_file = $cnf{state_file}; if (-e $state_file){ eval {do $state_file}; die $@ if $@; } else{ $S = {time => time}; } return $S; }
first_num: 10.0095 $VAR1 = { 'mem_first' => '77xptj52', 'hash' => { 'one' => 1 }, 'array' => [ 'one', 'two', 'three' ], 'last_num' => '72.0967', 'first_num' => '10.0095' };
In reply to Re: Loading variables from a separate script
by wfsp
in thread Loading variables from a separate script
by rsriram
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |