Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re^2: Use a Variable in a Separate Perl Script

by NinjaOne (Initiate)
on Jun 07, 2012 at 20:11 UTC ( #975034=note: print w/replies, xml ) Need Help??

in reply to Re: Use a Variable in a Separate Perl Script
in thread Use a Variable in a Separate Perl Script

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.
  • Comment on Re^2: Use a Variable in a Separate Perl Script

Replies are listed 'Best First'.
Re^3: Use a Variable in a Separate Perl Script
by aaron_baugher (Curate) on Jun 07, 2012 at 22:10 UTC

    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.

Re^3: Use a Variable in a Separate Perl Script
by rovf (Priest) on Jun 08, 2012 at 09:37 UTC
    $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 <>
      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>, ./ 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.
        OK, so let's clarify: Since you have two completely unrelated processes (unrelated from the Perl- OS-viewpoint), it doesn't make sense to talk about "one script refering to a variable in another script". However, it makes sense to pass a value to the other script. Now, your case is a bit more complicated because you don't execute the other script directly, but have a bash process in between.

        Of course, this bash process is unnecessary, if all it does is to do a chdir. You can do the chdir in Perl as well (see chdir). However, if you expect that this intermediate bash script later will do additional stuff, which you, for whatever reason, would prefer implementing in bash, it would make sense to stick with that.

        As for passing the value of your variable to the other script, you have several options. Since in your case, the value is only a fairly short string, I would consider two possibilities: Pass them as a parameter to your bash script, which then hands it over as a parameter to your Perl script; or use an environment variable to pass the information.

        The latter solution is faster (simpler) to implement, but the former one is, IMO, cleaner.
        Ronald Fischer <>

        This depends on whether the bash script is being executed by, or by some other parent process which runs, waits for it to complete, then runs the bash script. In the first case, simply pass the value in the command line like I did in my example, and then do the same from within your bash script. In the latter case, would need to return the value to its parent process, which would then pass the value to the bash script.

        I think it's very likely that the best solution would be to redo the whole thing as a single Perl script. However, if you already have two Perl scripts and you're just hoping to pass a value from one to the next without making any other changes, use my example, except stick chdir "directory"; at the beginning of That will eliminate the need for a bash script sitting between the two.

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://975034]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2018-01-18 21:57 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (215 votes). Check out past polls.