Ask a Question related to Perl / CGI, Design and Development.
-
John Strauss #1
Re: How do you simulate "." or "source" in a perl script ?
On 7 Aug 2003 08:21:27 -0700
[email]c_j_marshall@hotmail.com[/email] (Chris Marshall) wrote:Shell::Source>
> Is there a good way of simulating the bourne shell's "." command - or
> csh's "source" command inside a perl script ?
>
> Specifically I would like to set environment variables inside a perl
> script which are stored inside a separate config file (used by many
> other programs)
>
> At the moment I have a separate ksh script wrapper around my perl
> script which simply does something like
>
> #!/bin/ksh
>
> . /dir/config.sh
>
> /dir/perlscript.pl
>
> but there's got to be a better way than that.
>
> Unfortunately neither "." nor "source" are particularly easy things to
> search for and I've not spotted anything in either perldoc nor
> googling this newsgroup.
>
> I guess I could have the perl script read the config file explicitly
> and act upon what it finds there - but someone must have tackled this
> already.
>
> Thanks
> Chris
perhaps this CPAN module will do it for you.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drop the .thetenant to get me via mail
John Strauss Guest
-
Proj cannot run on LCDS 2.6 ES due to "Unable to resolveresource bundle "datamanagement" for locale "en_US"
hi, all, We have developped an application on Flex Build 3 (run successfully), but failed when we try to deploy it on Tomcat with LCDS 2.5 ES... -
"Devel::DProf" on a PERL script uses "Test::More"
Dear all, I encountered a problem while run profiling on a script which uses "Test::More". May I ask whether anybody have some idea or wrap... -
CFINPUT type="radio" w/ "value" requires "label"
On a Flash form, when you specify type='radio' and value='whatever', the value of the 'value' attribute will be displayed as a label if no 'label'... -
dr("field").toString returns "400.0000" instead of "400"
I have just installed VS.NET 2003 on my computer. I have a project that I have been developing on VS.NET 2002. I haven't upgraded this project to... -
"Start" "Program" "Menu" list is empty
For what ever reason my list of installed programs in my "Start" "Programs" menu is empty. Anyone know how to restore the list. Thanks for your... -
James Willmore #2
Re: How do you simulate "." or "source" in a perl script ?
> Specifically I would like to set environment variables inside a perl
If you want to set environmental variables from within Perl, %ENV is> script which are stored inside a separate config file (used by many
> other programs)
the place to set such items. perldoc perlvar for more information.
No offense, but what's the issue with this? It seems like it does>
> At the moment I have a separate ksh script wrapper around my perl
> script which simply does something like
>
> #!/bin/ksh
>
> . /dir/config.sh
>
> /dir/perlscript.pl
what you wnat it to do. If you want ALL scripts to have the SAME
environmental variables, why not set them from within .profile or
..bashrc or.cshrc or some variation of the three based upon your shell?
Because it's not portable. Perl is designed with portablity in mind.>
> but there's got to be a better way than that.
>
> Unfortunately neither "." nor "source" are particularly easy things
> to search for and I've not spotted anything in either perldoc nor
> googling this newsgroup.
WIN32 systems don't use '.' or 'source' without some help (ie
Cygwin). Might I suggest you read some more of the Perl
documentation. Perl is not your average scripting language (like
C-Shell, BASH, KSH, etc).
This is a workable solution if you can't keep the environmental>
> I guess I could have the perl script read the config file explicitly
> and act upon what it finds there - but someone must have tackled
> this already.
variables set in the parent shell. It's also more portable. If you
decide, one day, to run the scripts on a WIN32 machine, then you don't
have to re-invent the wheel to do so. There are several modules to
aid in this task -or- you could roll your own.
HTH
Jim
James Willmore Guest
-
Abigail #3
Re: How do you simulate "." or "source" in a perl script ?
Chris Marshall (c_j_marshall@hotmail.com) wrote on MMMDCXXVIII September
MCMXCIII in <URL:news:cb9c7b76.0308070721.2f4cd3f5@posting.goo gle.com>:
&& Is there a good way of simulating the bourne shell's "." command - or
&& csh's "source" command inside a perl script ?
&&
&& Specifically I would like to set environment variables inside a perl
&& script which are stored inside a separate config file (used by many
&& other programs)
&&
&& At the moment I have a separate ksh script wrapper around my perl
&& script which simply does something like
&&
&& #!/bin/ksh
&&
&& . /dir/config.sh
&&
&& /dir/perlscript.pl
&&
&& but there's got to be a better way than that.
&&
&& Unfortunately neither "." nor "source" are particularly easy things to
&& search for and I've not spotted anything in either perldoc nor
&& googling this newsgroup.
&&
&& I guess I could have the perl script read the config file explicitly
&& and act upon what it finds there - but someone must have tackled this
&& already.
An often asked question is 'how to "source" a file', like you do in the
shell, to set one or more environment variables. The easy answer is:
first source the file, then start the program. But what if the file
to source isn't known in advance? Or it's impossible to write a wrapper
script? Put the code below at or near the beginning of your program.
The code below runs in combination with the Korn shell, and probably any
other Bourne shell decendent, including the POSIX shell. I don't know
whether this works in the standard Windows shell - probably not due to
quoting issues, but getting it to run under Cygwin/bash should not be
a problem.
Abigail
my $ENVIRONMENT = "/some/file/with/environment/variables/";
if (@ARGV && $ARGV [0] eq '--sourced_environment') {
shift;
}
else {
if (-f $ENVIRONMENT) {
#
# Now we perform a double exec. The first exec gives us a shell,
# allowing us the source the file with the environment variables.
# Then, from within the shell we re-exec ourself - but with an
# argument that will prevent us from going into infinite recursion.
#
# We cannot do a 'system "source $ENVIRONMENT"', because
# environment variables are not propagated to the parent.
#
# Note the required trickery to do the appropriate shell quoting
# when passing @ARGV back to ourselves.
#
# Also note that the program shouldn't normally be called with
# '--sourced_environment' - if so, pick something else.
#
@ARGV = map {s/'/'"'"'/g; "'$_'"} @ARGV;
exec << " --";
source '$ENVIRONMENT'
exec $0 --sourced_environment @ARGV;
--
die "This should never happen.";
}
}
--
$_ = "\112\165\163\1648\141\156\157\164\150\145\1628\12 0\145"
. "\162\1548\110\141\143\153\145\162\0128\177" and &japh;
sub japh {print "@_" and return if pop; split /\d/ and &japh}
Abigail Guest
-
Chris Marshall #4
Re: How do you simulate "." or "source" in a perl script ?
James Willmore <jwillmore@cyberia.com> wrote in message news:<20030808111536.71614720.jwillmore@cyberia.co m>...
Only that it means having two scripts instead of one - and it seems a>> > Specifically I would like to set environment variables inside a perl
> > script which are stored inside a separate config file (used by many
> > other programs)
> >
> > At the moment I have a separate ksh script wrapper around my perl
> > script which simply does something like
> >
> > #!/bin/ksh
> >
> > . /dir/config.sh
> >
> > /dir/perlscript.pl
> No offense, but what's the issue with this? It seems like it does
> what you wnat it to do. If you want ALL scripts to have the SAME
> environmental variables, why not set them from within .profile or
> .bashrc or.cshrc or some variation of the three based upon your shell?
>
shame that the perl script would be reliable on a ksh script to run.
Chris Marshall Guest
-
Chris Marshall #5
Re: How do you simulate "." or "source" in a perl script ?
Abigail <abigail@abigail.nl> wrote in message news:<slrnbj7gfm.39l.abigail@alexandra.abigail.nl> ...
> Chris Marshall (c_j_marshall@hotmail.com) wrote on MMMDCXXVIII September
> MCMXCIII in <URL:news:cb9c7b76.0308070721.2f4cd3f5@posting.goo gle.com>:
> && Is there a good way of simulating the bourne shell's "." command - or
> && csh's "source" command inside a perl script ?
> &&<snip most of the code example>> The code below runs in combination with the Korn shell, and probably any
> other Bourne shell decendent, including the POSIX shell. I don't know
> whether this works in the standard Windows shell - probably not due to
> quoting issues, but getting it to run under Cygwin/bash should not be
> a problem.
>> exec << " --";
> source '$ENVIRONMENT'
> exec $0 --sourced_environment @ARGV;
Brilliant - thank you.
BTW I changed "source" in your script to be "." to get it working with
ksh. Should source have worked ? I thought that source was just for
csh.
Chris Marshall Guest



Reply With Quote

