Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

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


in reply to How to "source" a shell file in Perl?

> I'm unable to use a wrapper script that sources the file and then calls the Perl script ...

well that's the only sane solution, buddy.

> The shell script file isn't simply a set of "export VAR=VALUE" where I can simply parse the file...

Too bad, you can't export variables to a parent process.

The only (insane) solution which come to my mind is to call a shell script which sources your stuff and dumps it somewhere where the parent Perl can restore it from, e.g a file or piped to STDOUT.

That's such an insane approach that I don't even wanna elaborate more on it.

Maybe try to find some ksh, csh or sh ready to use snippets, able to export JSON.

> for political reasons.

Send them here and I'll tell them they are idiots.

Cheers Rolf

( addicted to the Perl Programming Language)


Comment on Re: How to "source" a shell file in Perl?
Re^2: How to "source" a shell file in Perl?
by jfroebe (Parson) on Jun 12, 2013 at 19:33 UTC

    Pushing the environment variables to the parent process isn't possible I know but what I could do is create a child process, capture the environment variables that are set and send that back to the parent process to handle. A bit of a hack but that should satisfy the requirements.

    Jason L. Froebe

    Blog, Tech Blog

      That was my idea.

      Actually you could call a wrapper-shell-script which sources and calls a second perl-script.

      The perl-script use Data::Dumper to serialize %ENV and prints the string to STDOUT.

      opening the shell script with "wrapper.sh |" should¹ be sufficent to grab the dump.

      the second perl-script might look overhead but it's cleaner and you don't need to worry about k/ba/c-sh compatibilities and about JSON.

      Cheers Rolf

      ( addicted to the Perl Programming Language)

      ¹) open

      and if the filename ends with a '|', the filename is interpreted as a comma +nd which pipes output to us.
      ...
      For three or more arguments if MODE is '|-', the filena +me is interpreted as a command to which output is to be piped +, and if MODE is '-|', the filename is interpreted as a command +which pipes output to us.
      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
        Thanks :) It works even in the more oddly configured environments here.

        Jason L. Froebe

        Blog, Tech Blog

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2015-07-02 03:54 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 (27 votes), past polls