hi rsriram

To add to samurai_'s suggestion perhaps you could consider using Data::Dumper to serialise the data.

#!/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; }
output:
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

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.