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

Hi all,

I have stored all the memory variables, arrays and hash tables in a separate perl script and I want this script to be executed in my main script. Can anyone give me the syntax to execute the script (in which memvars are stored) from the main script?

The script with memory variables looks like this...

$first_num=10.0095;

$last_num=72.0967;

$mem_first=77xptj52;

Replies are listed 'Best First'.
Re: Loading variables from a separate script
by monarch (Priest) on May 15, 2007 at 07:49 UTC
    Let's say you have two files, the first file1.pl:
    $first_num=10.0095; $last_num=72.0967; $mem_first="77xptj52";

    and a second file, file2.pl:

    use vars qw($first_num $last_num $mem_first); use strict; do "file1.pl"; print( "$first_num, $last_num, $mem_first\n" );

    I have tested the above code, which works fine. Now, importing every variable (with use vars) can be quite tiresome. So you'll often find in corporates that use Perl that they often import a single hash with all the data inside, like so (file3.pl):

    %everything = ( first_num => 10.0095, last_num => 72.0967, mem_first => "77xptj52", );

    Finally, file file4.pl would contain:

    use vars qw( %everything ); use strict; do "file3.pl"; print( join( ", ", $everything{first_num}, $everything{last_num}, $everything{mem_first}, ), "\n" );

    (Also tested, and perl -w file4.pl produces identical results to perl -w file2.pl), hope this helps!

Re: Loading variables from a separate script
by wfsp (Abbot) on May 15, 2007 at 07:49 UTC
    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' };
Re: Loading variables from a separate script
by CountZero (Bishop) on May 15, 2007 at 17:30 UTC
    What you are asking is akin to using a configuration file.

    There are lots of modules on CPAN which exactly do that.

    Have a look at (for example) Config::General or Config::Auto or Config::Framework or Config::Simple (this one has an option to either store the config-values in a hash or import them as variables in your namespace)

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Loading variables from a separate script
by Moron (Curate) on May 15, 2007 at 09:31 UTC
    Alternatively you can define constants and Readonly structures in the file you run with do.
    __________________________________________________________________________________

    ^M Free your mind!

Re: Loading variables from a separate script
by samurai_ (Initiate) on May 15, 2007 at 07:19 UTC
    I would like to suggest the following use do command in main script do file_name you variables should be loaded in main name space
Re: Loading variables from a separate script
by hiptoss (Novice) on May 18, 2007 at 09:32 UTC
    I've done this using both 'do' as well as the Storable CPAN module. ( CPAN link ) See the link for more details.