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

Hi, I'm writing a basic perl script for an application which uses mainly unix scripts (korn shell based). To launch an external program I need to read some shell variables which are set by a unix script (this unix script is also used by other applications, so I'd prefer not to change it). So, I first launch the unix script using:
system ". \/path\/urmset_01 rp 010101";
this results in the error: SCRIPTS=/ontw/bin/app: is not an identifier; this line is a part of the unix script urmset_01:
#!/bin/ksh FILE=$2 PAR="ONT" PATH=/usr/bin:/ontw/bin/app/:$PATH export PATH PCISERV=IS_L70 export PCISERV if [ "$PAR" = "ONT" ]; then # Development Environment export SCRIPTS=/ontw/bin/app export FUNC=/ontw/lib/app ...
when I run the unix command from the shell:
ksh>. /path/urmset_01 rp 010101
it runs fine, i.e. ECHO $SCRIPTS gives the right result. can someone think of what's causing this problem and/or a solution? thanks

Replies are listed 'Best First'.
Re: interaction perl - Korn shell variables
by ikegami (Patriarch) on Apr 17, 2009 at 14:16 UTC

    Assuming Perl doesn't given an error for trying to execute a directory ("."), it'll launch /bin/sh to execute it. That means using "." is wrong here. You're executing a ksh script in sh.

    Each process has it's own environment. While changes are inherited by children, changes don't affect the parent. So while you are changing ksh's environment, your Perl script's environment will remain unchanged.

    This has been discussed a number of times before. The solution usually involves printing out the environment and have it parsed by your Perl script.

    my @env = `ksh -c '. /path/urmset_01 rp 010101 && set'`; ...
      thanks @everyone for the replies. I finally 'solved' it by just using the ksh -c command and piping all the commands in the same system call. Makes it quite unreadable, but works like a charm.
Re: interaction perl - Korn shell variables
by targetsmart (Curate) on Apr 17, 2009 at 14:15 UTC
    Every time you call system, it does a fork and exec, so the variables you set are only visible to that temporary shell.
    so try other methods.

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re: interaction perl - Korn shell variables
by cdarke (Prior) on Apr 17, 2009 at 18:31 UTC
    The reason for SCRIPTS=/ontw/bin/app: is not an identifier is that (as others said) you are executing the script under the Bourne shell. You might say, "what about the #!/bin/ksh?", well that is ignored as a comment with the "dot" command. You might also reasonably say, "why is the export failing, when that is a POSIX complient command?". Unfortunately many commercial Bourne shells are still not POSIX complient.
    Although even if it did not give the error you still cannot execute the korn shell language from the Perl language.

    I suggest you do the following: have a shell script wrapper which executes the required preamble, and then calls the "real" script. The name of that script can be passed as a command-line argument from perl to the wrapper.