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

Hi I have an embedded perl test script I'm playing with. I'm noticing if I try to assign a variable in perl like $dog, the shell script gives me an error. How can I assign Perl variables in embedded perl script in a UNIX shell.

Here is my script. I have a UNIX and a Perl variable next to each other. How can I display the perl value?

#/bin/ksh fruit=basket pltst() { /usr/bin/perl <<- _perl sub first { $tombstone = 'pizza'; <--- I want to assign this value. HOW?? print "Your ",index($fruit,'k')," and ",oct('65')," other things g +o here. $fruit , $tombstone \n"; } first _perl } # end of shell function pltst mytree=`pltst` echo mytree - $mytree exit 0

Replies are listed 'Best First'.
Re: assigning a perl variable in shell
by Anonymous Monk on Jan 13, 2015 at 19:14 UTC

    You might want to reconsider your design, since embedding Perl scripts in shell scripts has a couple of tricky aspects and can make maintenance a bit less fun. The easiest might be to put your Perl script into a separate file and just run that, or even inverting the design, that is, write most of your script in Perl, and then call external commands via system or a module like IPC::System::Simple.

    Having said that, two relatively easy ways to pass shell script variables into Perl: Pass the variables as arguments on the command line of perl and then in your script access them via @ARGV, or, if the variables are visible in the environment, use the %ENV hash to access them.

    I'm noticing if I try to assign a variable in perl like $dog, the shell script gives me an error.

    What's the error message? There is no $dog in your sample code.

      Thanks for the response!

      Use $Tombstone in place of $dog as the example of a perl variable in the script that throws an error.

      It looks like I'd use %ENV on the UNIX shell variable $fruit. Is that true or am I to use the %ENV on the perl variable $tombstone?

      Also I like the idea of going all Perl or calling an entire Perl script. The trouble is we are an all UNIX shell shop and If I started throwing Perl scripts around it would blow peoples minds. I don't want to stir that up. I just want to be able to access perl from time to time to use it's vast array of functions instead of using awk or exp or something else. For example UNIX doesn't have an index function that returns the positional value of a string when fed. But Perl does! So just simple "one offs" like that are what I'd want to use this for.

        Also I like the idea of going all Perl or calling an entire Perl script. The trouble is we are an all UNIX shell shop and If I started throwing Perl scripts around it would blow peoples minds.
        If you'd like to blow people's minds with specific reasons to write the whole thing in Perl see:

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: assigning a perl variable in shell
by LanX (Saint) on Jan 13, 2015 at 19:26 UTC
    Welcome to the monastery!

    That's a ksh question, not Perl.

    I suppose you need to escape the \$vars from shell interpolation before passing them.

    But I won't test cause it's

    • off topic
    • haven't ksh installed
    • your code isn't minimal enough to be easily understood.

    Please see how (not) to ask a question

    Cheers Rolf

    PS: Je suis Charlie!

      Ya' know what guys? I figured it out.

      If you just put an escape character in front of $tombstone like so : \$tombstone, it works! It successfully masks the '$' to UNIX and Perl still picks it up and uses it. So My output ends up bing:

      mytree - Your 3 and 53 other things go here. basket , pizza

      Everything prints!

        > I figured it out.

        Great that you figured it out on your own!

        edit

        NB: This kind of code generation is prone to ugly bugs on the long run.

        You should really consider AnoMonks' suggestions to pass data either via @ARGV or %ENV.

        Cheers Rolf

        PS: Je suis Charlie!

        Sounds like the here document in the original Korn script was interpolating variables - I don't know ksh, but in bash you can stop it from doing that like so:

        #!/bin/bash cat <<END foo <$HOME> END cat <<'END' bar <$HOME> END ### Output: # foo </home/foobar> # bar <$HOME>

        That happens to be the equivalent behavior as Perl in regards to stopping interpolation in here documents. If you can, I'd strongly suggest always giving your scripts to perl this way - as LanX said, trying to get values into Perl via interpolation is going to end up being a pain, so use @ARGV (that would be my suggestion) or %ENV.