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

Greeting Monks,

This may be very simple question, but still some light on this would give me clear picture.

I have two files file1.pl and file2.pl. In the first file i have $var = 1 and i want it to get the value in second file.Please give me some ways to do it.

I tried to use the file1.pl in file2.pl but end up with vein.

Note: May be $var can be global variable.


--Prasanna.K
  • Comment on Exporting variables between two perl files

Replies are listed 'Best First'.
Re: Exporting variables between two perl files
by polettix (Vicar) on Jul 07, 2005 at 08:29 UTC
    Contrarily to gube, I assume that one of the scripts is some kind of "slave" (you talk about using one from the other) of the other, and not that they are allowed to be executed independently. What I mean is that if you need to have two scripts to be executed independently, and operate on some shared value, you should try to follow gube's suggestion. OTOH, if you just want to put some configuration values inside one file, and grab them from the other, read on.

    For reasonably recent Perl, you can use the our keyword, which works similar to my but it's not the same, of course. In file1.pl:

    #!/usr/bin/perl use strict; use warnings; our $var = 1;
    In file2.pl:
    #!/usr/bin/perl use strict; use warnings; our $var = 10; print "\$var is $var\n"; do "file1.pl"; print "\$var now is $var\n";
    You get:
    poletti@flaviox ~/sviluppo/perl> perl file2.pl $var is 10 $var now is 1
    For previous versions of Perl, you can obtain similar results with use vars (see http://perldoc.perl.org/vars.html, noting that it is now obsolete).

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Exporting variables between two perl files
by anonymized user 468275 (Curate) on Jul 07, 2005 at 08:43 UTC
    It depends how the programs are executed and how $var is declared.

    For example, if file1.pl uses do "file2.pl"; to run file2.pl, $var (including its value) will be inherited by file2.pl, unless $var is confined to the scope of file1.pl using 'my'.

    One world, one people

Re: Exporting variables between two perl files
by gube (Parson) on Jul 07, 2005 at 07:32 UTC

    Hi Prassana,

    Refer this How to declare global variable in CGI file?

    The above question was asked by me for cgi file same answer for perl file also. The global variable stored you have to read from the text file or stored in the database or u may done with apache some process. You refer the above link you may get some ideas.

Re: Exporting variables between two perl files
by tphyahoo (Vicar) on Jul 07, 2005 at 16:29 UTC
    Do it with objects.

    File1.pl:

    use strict; use warnings; use File2; my $file2obj = File2->new(); print "var: " . $file2obj->{var} . "\n"; #should print 1 $file2obj->{var} = 10 . "\n"; print "var: " . $file2obj->{var}; #should print 10

    File2.pm:

    package File2; use strict; use warnings; use fields qw(var); # to keep yourself from making stupid typos, like +calling $file2obj->{vaar}; sub new { my $package = shift; my $self = fields::new($package); $self->{var}=1; # initialize var; return $self; } 1;

    Now for a followup... how would you do that in perl6? ;)