in reply to Use a Variable in a Separate Perl Script

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.

Replies are listed 'Best First'.
Re^2: Use a Variable in a Separate Perl Script
by NinjaOne (Initiate) on Jun 07, 2012 at 20:11 UTC
    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>
        Good point. You are correct, Script1 and Script2 are separate processes. At the completion of Script1, I have a mini Bash script (cd <into directory>, ./script2.pl) which executes Perl Script2. I need Script2 to recognize the $final variable defined from Script1. Is this possible? $final will be given a name, such as "Basketball". So I need Script1 to attain the variable (from user input). Then once Script2 is executed, which is completely separate from Script1, it will be able to use the given variable of "Basketball" and continue with the actions of Script2.