Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: How to "source" a shell file in Perl?

by LanX (Canon)
on Jun 12, 2013 at 20:04 UTC ( #1038572=note: print w/ replies, xml ) Need Help??


in reply to Re^2: How to "source" a shell file in Perl?
in thread How to "source" a shell file in Perl?

Congratulations, works for me :)

open my $source,'-|',"bash /tmp/wrapper.sh" or die "$!"; my $dump= do { local $/; <$source>}; my $VAR1; eval $dump; print $VAR1->{MONK}; #> jfroebe

lanx@nc10-ubuntu:/tmp$ cat wrapper.sh . /tmp/src.sh perl -MData::Dumper -e' print Dumper \%ENV' lanx@nc10-ubuntu:/tmp$ cat src.sh export MONK=jfroebe lanx@nc10-ubuntu:/tmp$

Thats an extremely stable approach, I can't think of a way to make it break! =)

you could even try to generate the code from wrapper.sh dynamically on the fly, hence avoiding any security/dependencies issues.

Cheers Rolf

( addicted to the Perl Programming Language)

UPDATE

> you could even try to generate the code from wrapper.sh dynamically on the fly

works! =)

my $bashcode=<<'_bash_'; . /tmp/src.sh; perl -MData::Dumper -e "print Dumper \%ENV"; _bash_ open my $source, '-|', qq{bash -c '$bashcode'} or die "$!"; my $dump= do { local $/; <$source>}; my $VAR1; eval $dump; print $VAR1->{MONK}; #> jfroebe

UPDATE

far less code, same result

my $bashcode=<<'__bash__'; . /tmp/src.sh; perl -MData::Dumper -e 'print Dumper \%ENV'; __bash__ my $VAR1; eval qx{bash -c "$bashcode"}; print $VAR1->{MONK}; #> jfroebe


Comment on Re^3: How to "source" a shell file in Perl?
Select or Download Code
Replies are listed 'Best First'.
Re^4: How to "source" a shell file in Perl?
by jfroebe (Parson) on Jun 12, 2013 at 20:46 UTC
    Thanks :) It works even in the more oddly configured environments here.

    Jason L. Froebe

    Blog, Tech Blog

      I have to admit that it's kind of a genius solution I really didn't expected to be possible at first.

      But the credits go to Perl's awesome flexibility! =)

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (17)
As of 2015-07-29 18:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (267 votes), past polls