http://qs321.pair.com?node_id=930641


in reply to Convert Shell script to Perl

You could set the environment variables and run exec to call the script
[skrynesaver@busybox ~]$ cat ~/tmp/test.pl #!/usr/bin/perl $ENV{'THE_VAR'}= "This has been set in the environment of the script"; exec "$ENV{HOME}/tmp/test.sh" [skrynesaver@busybox ~]$ cat ~/tmp/test.sh #!/bin/bash echo $THE_VAR [skrynesaver@busybox ~]$ perl tmp/test.pl This has been set in the environment of the script
print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

Replies are listed 'Best First'.
Re^2: Convert Shell script to Perl
by cdarke (Prior) on Oct 10, 2011 at 14:41 UTC
    exec does not behave in the same way on Windows and UNIX, so it might be better to use system instead, which is similar on both (although there are still a few differences).

    Although the latest ksh93 versions exec on the last statement within a script, I don't think that bash does.

    One other thing to be aware of, environment variables on UNIX are case sensitive but on Windows are not - that can catch you out.
      This is the code:
      use strict; use warnings; $ENV {'LIBDIR'}='$MY_XALANHOME/bin'; $ENV {'LOCALCLASSPATH'}='$ENV{LIBDIR}:/xalan.jar'; $ENV {'LOCALCLASSPATH'}='$ENV{LOCALCLASSPATH}:$ENV{LIBDIR}/xml-apis.ja +r'; $ENV {'LOCALCLASSPATH'}='$ENV{LOCALCLASSPATH}:$ENV{LIBDIR}/xercesImpl. +jar';
      When I run the perl program from the command line using - perl abc.pl - I don't get any error messages. When I run the following command from the commandline:
      perl $ENV{'LIBDIR'}='C:\SOMEFOLDER';
      I get a message saying - can't open perl script "$ENV": no such file or directory. I would've expected to create a new environment variable called LIBDIR with the c:\somefolder as the value ? Thanks
        Two things, you need to tell the Perl interpreter to execute the code rather than look for a file of that name and the environment variables will not be set int the parent process (your shell) only in the Perl process and its children.

        So while perl -e "$ENV{LIBDIR'}='C:\SOMEFOLDER';" would create the varable, it would cease to exist when Perl exited.

        print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re^2: Convert Shell script to Perl
by akrrs7 (Acolyte) on Oct 10, 2011 at 14:15 UTC
    I guess..I know that $ENV is similar to a hashmap and has a key and a value...
    $ENV {'LIBDIR'}='$MY_XALANHOME/bin';
    seems to be the correct way to set that. Please confirm. What I am not sure is - should I also set the JAVACMD as an environment variable using $ENV ? Thanks...
        This is my current code:
        use strict; use warnings; $ENV {'LIBDIR'}='$MY_XALANHOME/bin'; $ENV {'LOCALCLASSPATH'}='$ENV{LIBDIR}:/xalan.jar'; $ENV {'LOCALCLASSPATH'}='$ENV{LOCALCLASSPATH}:$ENV{LIBDIR}/xml-apis.ja +r'; $ENV {'LOCALCLASSPATH'}='$ENV{LOCALCLASSPATH}:$ENV{LIBDIR}/xercesImpl. +jar';
        I've commented out the JAVACMD lines. When I run this on the cmd line say - perl a.pl this does not give me any errors. But I should be able to find these env variables in the command window - I cannot. I've also simply tried this on the command line
        perl $ENV{'LIBDIR'}='C:\somefolder';
        I get a message saying - 'Can't open perl script "$ENV":no such file or directory
Re^2: Convert Shell script to Perl
by akrrs7 (Acolyte) on Oct 10, 2011 at 15:46 UTC
    I've tried the following code:
    use strict; use warnings; $ENV {'LIBDIR'}='C:/eWorkspace/myPerlProgram'; $ENV {'LOCALCLASSPATH'}='$ENV{LIBDIR}:/xalan.jar'; $ENV {'LOCALCLASSPATH'}='$ENV{LOCALCLASSPATH}:$ENV{LIBDIR}/xml-apis.ja +r'; $ENV {'LOCALCLASSPATH'}='$ENV{LOCALCLASSPATH}:$ENV{LIBDIR}/xercesImpl. +jar'; system "$ENV{LIBDIR}/xalan_perl.pl"
    I've then run this program from the command line to get the following message: Can't spawn "cmd.exe": No such file or directory at C:\eWorkspace\myPerlProgram\ xalan_perl.pl line 33.
      Sorry for necroposting, but in Perl, the running code should be:

      my $JAVACMD = "$JAVA_HOME/bin/java"; my @OUTPUT = `$JAVACMD -cp "$LOCALCLASSPATH" org.apache.xalan.xslt.Pro +cess "@ARGV"`;