Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I would like to source some tools in a shell script, and then get the same envoirnment in my perl program. I saw the dotsh.pm,but the documentation says that it will not be supported in later versions of perl. Can anyone help me...?? Thanks

Replies are listed 'Best First'.
Re: substitute of dotsh.pm
by santang (Acolyte) on May 20, 2004 at 12:12 UTC

    Why not just let shell do the work?

    use YAML; %ENV = %{ Load `sh -c ". rcfile; perl -MYAML -e'print Dump \\%ENV'"` } +;

    Of course, the above will only work if the RC file that you sourced exported all of the variables that it set. Otherwise, you'd need to parse the output of `env' or something like that.

    Forging your own path does not mean that you should avoid asking for directions.
        Thanks for your help, Well i did install YAML in my perl and i run the script
        eval 'exec perl $0 ${1+"$@"}' # -*- perl -*- if $running_under_some_shell; use YAML; print "Path is "; print $ENV{PATH}; %ENV = %{ Load qx{.rcfile; perl -MYAML -e 'print Dump \\%ENV'} }; print "Path is now "; print $ENV{PATH};
        where my .rcfile contains
        #!/bin/csh -f source /usr/applis/common/sourcefiles/solaris/unicad_24
        But when i run the perl script , i get the error
        --- !perl/YAML::Error code: YAML_LOAD_USAGE msg: 'usage: YAML::Load($yaml_stream_scalar)' ... at test.pl line 8 Path is /usr/applis/perl/5.005_03/bin: /usr/applis/common/bin:/usr/a +pplis/common/bin: /usr/bin:.:/usr/dt/bin:/usr/openwin/bin: /bin:/usr/ +ucb:/usr/local/bin:/usr/applis/common/bin: .:/home/rishis:/home/rishi +s/bin:/usr/local/bin: .:/home/rishis:/home/rishis/bin:/usr/local/bin: + .:/home/rishis:/home/rishis/bin:/usr/applis/unicad/24/Ukernel
        As i am a newbee to YAML i am unable to figure the problem THanks for your help in advance!!!

        Edited by Chady -- added code tags (and the spaces in the path list so that it wraps)

Re: substitute of dotsh.pm
by greenFox (Vicar) on May 21, 2004 at 02:59 UTC
    I've never used it and I am not sure where it came from (read untested) but I found this in my code archive, it might be useful as a starting point if for some reason you can't use the YAML solution.
    sub source_shell_profile { # source a shell profile and extract ENV vars my($profile) = $_[0]; local(*STDIN,*STDERR); # could just close STDIN instead, but some programs # don't deal with that very well... open(STDIN,"/dev/null"); open(STDERR,">/dev/null"); my %env = map {chomp;split(/=/,$_,2)} `. $profile; env`; return %env; }

    --
    Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho