Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: source a file in Linux using perl script

by Corion (Patriarch)
on Nov 30, 2020 at 07:21 UTC ( [id://11124387]=note: print w/replies, xml ) Need Help??


in reply to source a file in Linux using perl script

The environment variables of the shell are available through the %ENV hash in Perl. See perlvar for it.

The easiest way to make the variables available to a Perl script is to set them up beforehand:

#!/bin/bash . ~/.bash_profile /path/to/your/perlscript.pl

If that is not possible, you can follow the approach outlined in Get default login environment to get the environment variables that the script sets up. Specifically, RE (tilly) 3: Get default login environment has a subroutine that does what you want.

Replies are listed 'Best First'.
Re^2: source a file in Linux using perl script
by salva (Canon) on Nov 30, 2020 at 09:23 UTC
    TIMTOWTDI:
    #!/usr/bin/perl use POSIX; use String::ShellQuote qw(shell_quote); unless (defined $ENV{CTC_HOME}) { $ENV{DONT_LOOP_FOREVER}++ or die "infinite loop detected"; exec "source ~/.bash_profile && ".shell_quote($^X, $0, @ARGV); POSIX::_exit(255); } chdir($ENV{CTC_HOME}); ...

    Though, I have to admit I prefer the simpler shell wrapper solution given by Corion.

      I think the one drawback of re-executing yourself like that is that you lose Perl command line switches like -Ilib etc. ... There is Devel::PL_origargv, which gives you the original command line, but I'm not sure that I would want to go such an esoteric length when a shell script can do the same.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-18 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found