Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Convert Shell script to Perl

by akrrs7 (Acolyte)
on Oct 10, 2011 at 13:22 UTC ( #930628=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on Convert Shell script to Perl
Select or Download Code
Re: Convert Shell script to Perl
by Corion (Pope) 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."
      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...
      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'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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://930628]
Approved by keszler
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (16)
As of 2014-08-22 19:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (163 votes), past polls