http://www.perlmonks.org?node_id=930628

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

This is the shell script

#!/usr/bin/sh export LIBDIR=$MY_XALANHOME/bin export LOCALCLASSPATH=${LIBDIR}/xalan.jar export LOCALCLASSPATH=${LOCALCLASSPATH}:${LIBDIR}/xml-apis.jar export LOCALCLASSPATH=${LOCALCLASSPATH}:${LIBDIR}/xercesImpl.jar JAVACMD=$JAVA_HOME/bin/java $JAVACMD -cp $LOCALCLASSPATH org.apache.xalan.xslt.Process "$@"

This is the perl equivalent script

#!/usr/bin/perl use strict; use warnings; $ENV {'LIBDIR'}='$MY_XALANHOME/bin'; $ENV {'LOCALCLASSPATH'}='$LIBDIR/xalan.jar'; $ENV {'LOCALCLASSPATH'}='$LOCALCLASSPATH:$LIBDIR/xml-apis.jar'; $ENV {'LOCALCLASSPATH'}='$LOCALCLASSPATH:$LIBDIR/xercesImpl.jar'; JAVACMD=$JAVA_HOME/bin/java; $JAVACMD -cp $LOCALCLASSPATH org.apache.xalan.xslt.Process "$@";

Please review and advise where I could be off on the perl script. I need this to work in Windows as well as Unix.

Thanks

Replies are listed 'Best First'.
Re: Convert Shell script to Perl
by Corion (Patriarch) on Oct 10, 2011 at 13:28 UTC

    What you posted is not valid Perl. Running what you think your Perl script is will make Perl tell you so. Please correct your script. Maybe you want to read system, but before that, also do read perlintro and perlsyn.

    Perl is not a shell or shell-alike language.

      Hello... I am really new at Perl...as you've surmised. I have read the perlintro document but am still confused on how to set environment variables. Thanks...

        You are confused about so many things that it does not make sense to give you small pointers. You need to take a step back and start learning Perl. See the books section of learn.perl.org, and maybe the Modern Perl book by chromatic, if you're in need of a free book and already know how to program in some other programming language.

Re: Convert Shell script to Perl
by Utilitarian (Vicar) on Oct 10, 2011 at 14:02 UTC
    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."
      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
      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...
      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"`;