Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Convert Shell script to Perl

by Utilitarian (Vicar)
on Oct 10, 2011 at 14:02 UTC ( #930641=note: print w/ replies, xml ) Need Help??


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."


Comment on Re: Convert Shell script to Perl
Select or Download Code
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 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 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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-09-16 11:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (10 votes), past polls