in reply to Re^3: pass perl variable to shell script
in thread pass perl variable to shell script

morgon, I only have one script -- shell script, in which i have a block of perl code. Lets forget I have 'print' statement in the perl block. Lets say I am setting str3 somewhere in the middle of the perl block. After perl block has finished executing, I am back in the shell part of the code and need to get the content of str3 variable that was set in the perl block of the code. How do I do it?
  • Comment on Re^4: pass perl variable to shell script

Replies are listed 'Best First'.
Re^5: pass perl variable to shell script
by Anonymous Monk on Aug 11, 2010 at 17:05 UTC
    How do I do it?

    Programs communicate via STDIN/STDOUT, so the way morgon and others showed you is the way to do it

      OK, so you are saying that in order for me to get access to a variable that was set in perl block, I woud need to print that variable either to stdout or to a file and then read it back in the shell part of the code? Since I don't want to print into a file, then I need to print to stdout. But when morgon says:
      variable=$(perl script.pl)
      what is 'script.pl'? I don't have a .pl script. I only have .sh script.
        You cannot simply reference a Perl-variable in a shell-script.

        You have to find a way to pass the *value* of the variable from Perl to the shell.

        The way via command-subsitution I showed above is one way to do this.

        Perl prints the value to STDOUT and the shell reads it into a shell-variable (which is different from the perl-variable).

        Now if you have the perl-script directly embedded in your shell-script as a here-doc then you will have all sorts of problems properly escaping stuff, which is why I suggested above that you put all the Perl-code into a script of it's own that you can call anything you like - I called it simply "script.pl" for illustration but what you have to do is to move the perl-code from the shell-script into a perl-script file and call that file via command-substitution.

Re^5: pass perl variable to shell script
by choroba (Cardinal) on Aug 11, 2010 at 17:20 UTC
    Without changing the perl code to report the value somehow, there is no way.