Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Use a Variable in a Separate Perl Script

by NinjaOne (Initiate)
on Jun 07, 2012 at 16:19 UTC ( #974982=perlquestion: print w/ replies, xml ) Need Help??
NinjaOne has asked for the wisdom of the Perl Monks concerning the following question:

Good day wise Perl Monks, To keep this short, I will abbreviate my problem. I have a simple Perl script which creates a variable called $name. I would like to use this variable ($name) in a separate Perl script, but I am not having very much luck in doing so. How can I "pass" this variable, so that Perl Script #2 can utilize the contents of $name. I am relatively new to Perl, and so I cannot seem to find this solution anywhere. Thanks in advance for whatever help you may be able to share.

Comment on Use a Variable in a Separate Perl Script
Re: Use a Variable in a Separate Perl Script
by choroba (Abbot) on Jun 07, 2012 at 16:31 UTC
    For example, you can make script1.pl output the value of the variable. Then, in script2.pl, you should call the script1.pl and capture its output:
    #!/usr/bin/perl my $name = `perl1.pl`;
Re: Use a Variable in a Separate Perl Script
by temporal (Pilgrim) on Jun 07, 2012 at 17:15 UTC

    Depends on what you are doing with these two scripts. It might be time to rethink your design. Maybe what you really want to do is create a package with some functions and variables and then put the program logic into a second script.

    #MyModule.pm package MyModule; use strict; our $variable = 'hello!';
    #MyScript.pl use strict; use MyModule; print $MyModule::variable;

    Also, you can use Exporter if you want to add optionality to your exports.

    Strange things are afoot at the Circle-K.

Re: Use a Variable in a Separate Perl Script
by aaron_baugher (Deacon) on Jun 07, 2012 at 19:24 UTC

    There are a variety of ways, depending on your goal. Here's one way (minus error checking and meta-character escaping):

    $ cat script1.pl #!/usr/bin/perl # script1.pl my $name = '123abc'; my $results = `./script2.pl "$name"`; # $results now contains the output of script2.pl $ cat script2.pl #!/usr/bin/perl # script2.pl # run locate on the first argument given to this script print `locate "$ARGV[0]"`;

    Aaron B.
    Available for small or large Perl jobs; see my home node.

      Sorry all, thanks for the help thus far, but let me be more specific. Perl Script #1 has a variable, let's call it $final. $final is defined by user input, so it is always dynamic. I would like to use the input from $final in Perl Script #1 and use it to pass that input into Perl Script #2. Thanks.

        My example does that, except that my variable is named $name instead of $final. I assigned a static string to mine, but you could get a value into $name using any dynamic method you like, and then pass it to a subprocess (written in Perl or any other language) on the command line using backticks as I did.

        Aaron B.
        Available for small or large Perl jobs; see my home node.

        $final is defined by user input, so it is always dynamic.
        Well, variables are always dynamic, that's why they are called variables.

        Some of the confusion in the replies to your question seem to be caused by the term "script", which you are using. My understanding is, that script1 and script2 MUST be separate process, in the sense of independent commands, and you need to pass information from one script to the other. Is this correct?

        Some replies to your question imply that there is a closer coupling of the script possible (so that you can run the whole code within a single process). Both approaches make sense, but maybe you should explain more in detail what your requirements are. Only then can we propose a better solution. Also it helps if you explain, what kind of value $final is supposed to have. Proposed solutions might be slightly different, dependent on whether this value, for instance, is supposed to be a one-digit number, or the content of a whole Word document...
        -- 
        Ronald Fischer <ynnor@mm.st>
Re: Use a Variable in a Separate Perl Script
by patcat88 (Deacon) on Jun 07, 2012 at 21:01 UTC
    Use require and package name spaces. Your 2nd script becomes an library that is used/loaded by the first.
Re: Use a Variable in a Separate Perl Script
by Athanasius (Monsignor) on Jun 08, 2012 at 16:40 UTC

    Or just use the ‘obvious’ solution: save $final to a temporary file?

    # in Script #1: my $file = 'script1_final.txt'; # add path as required open(my $fh, '>', $file) or die "Cannot open file '$file' for writing: + $!"; print $fh $final; close $fh; # in Script #2: my $file = 'script1_final.txt'; # add path as required open(my $fh, '<', $file) or die "Cannot open file '$file' for reading: + $!"; my $final = <$fh>; close $fh; unlink $file or warn "Cannot delete file '$file': $!"; # Use $final as needed...

    The only coupling required here is that the name of the shared file must be known to both scripts.

    Not saying this is the best solution, but it does have the virtues of simplicity and extensibility (since it is straightforward to add data to the file if additional data sharing between the scripts is required down the track).

    HTH,

    Athanasius <°(((><contra mundum

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://974982]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2014-08-20 13:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (114 votes), past polls