sourcing a file from perl

J

JAWS

I need to use the csh source command or anything equivalent form a perl
script but i can't access the source command because the shell fired by
perl is sh. Ideally, i also need to be able to use the new environment
variables in the rest of my perl program...

thanks in advance for suggestions

jp
 
B

Ben Morrow

JAWS said:
I need to use the csh source command or anything equivalent form a perl
script but i can't access the source command because the shell fired by
perl is sh. Ideally, i also need to be able to use the new environment
variables in the rest of my perl program...

In general what you want to do can't be done: csh and Perl are
different languages, and perl does not understand csh. It's the same
as if you tried to source a (complicated) sh script from in csh: it
just wouldn't work.

Your best bet is to read the file yourself in the Perl program, and
extract the variable definitions out of it. If it is a complicated
script (ie. the variable settings are not easy to parse) then you may
be better off writing a little csh script which sources the file and
prints out the variables, and then opening a pipe to that script (see
perldoc -f open, the bit about opening a name ending in '|') and
parsing the results instead.

Ben
 
T

Tony Curtis

I need to use the csh source command or anything
equivalent form a perl script but i can't access the
source command because the shell fired by perl is sh.
Ideally, i also need to be able to use the new
environment variables in the rest of my perl program...

Usually the only sane solution to this is to wrap the perl
program in a csh script that sources the (sub-)script
first.

If the source'd script is just a few setenv's though then
maybe you could just read it in as data and parse it into
$ENV assignments.

hth
t
 
M

Malcolm Dew-Jones

JAWS ([email protected]) wrote:
: I need to use the csh source command or anything equivalent form a perl
: script but i can't access the source command because the shell fired by
: perl is sh.

So use that shell to fire up csh to source the file.

Ideally, i also need to be able to use the new environment
: variables in the rest of my perl program...

The truth comes out, it's a faq, you can't do what you want directly (on
unix like systems).

o You can define the vars before you run perl. This is by far the easiest
in the long run.

$ . vars.sh
$ perl my_script.pl

o From within perl you check for the variables, and if they are not set
then use system to run a short shell script that a) sets the variables and
then b) (re)invokes your perl script

# just an illustration !!

if not $ENV{FOOBAR}
{ exit system(". stuff; export FOOBAR=1; perl $0 @ARGV");
}

o You can ask the shell script to print the variables and then parse the
output

# just an illustration

#!/bin/sh
# vars.sh
MY_VAR="set its value";

#!perl
$values = `. vars.sh; echo MY_VAR=$MY_VAR`;
my ($my_var)=$values=~m/MY_VAR=(.*)/;

o You can parse the shell script yourself, it's easy for simply X=Y
assignments.

#!perl
# YOU MUST TRUST THE CONFIG FILE (of course you do or you couldn't
# source it in the shell script either)
use strict;
my ($MY_VAR);

{ local @ARGV=qw(vars.sh);
# local $SIG{__WARN__}=sub{1}; # do we want to see warnings.
map {eval "\$$_"} grep {/^\w+=/}<>;
}

o There are probably modules that will do variations of the above to pull
configurations variables out of configuration files.
 
J

JAWS

Thanx for all the suggestions, i'll have to call a csh script from my perl
since i need to do some treatment before the sourcing and it is a pretty
nasty script that sources others that sources... you get the idea...

c-ya

jp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top