From perl to python
<p>I've got some code that I've translated from perl into python, but I am having a time trying to figure out this last part. </p>

<pre><code>my $bashcode=&lt;&lt;'__bash__';
. /opt/qip/etc/qiprc;
. /opt/sybase/sybase.sh
perl -mdata::dumper -e 'print dumper \%env';
__bash__
my $var1;
eval qx(bash -c "$bashcode");
</code></pre>

<p>While I understand (a bit) what this is doing, I can't seem to find out how to do this in python. Any help would be greatly appreciated.</p>

<p>This part:</p>

<pre><code>my $bashcode=&lt;&lt;'__bash__';
. /opt/qip/etc/qiprc;
. /opt/sybase/sybase.sh
perl -mdata::dumper -e 'print dumper \%env';
__bash__
</code></pre>

<p>Is a here doc and you would do this in Python:</p>

<pre><code>bashcode="""\
. /opt/qip/etc/qiprc;
. /opt/sybase/sybase.sh
perl -mdata::dumper -e 'print dumper \%env';
"""
</code></pre>

<p>You would shell out to Bash using something like <a href="https://docs.python.org/2/library/os.html#os.system" rel="nofollow">os.system</a> or <a href="https://docs.python.org/2/library/subprocess.html#subprocess.check_output" rel="nofollow">subprocess.check_output</a>.</p>

<p>The result of that is then fed to <code>eval</code> to be run as Perl code. </p>

<p>(The wisdom, and the result of the <code>eval</code> is dependant on what these Bash commands produce. It would appear that those commands are producing a Perl script. Obviously, that will not execute in Python. Python also has eval. Use <code>eval</code> in any language cautiously.)</p>

<p>The use of <code>\%env</code> in Perl would seem to indicate access to the environment of the host. However, the actual Perl variable to access the OS environment hash is <code>ENV</code> in uppercase. In Bash, you access the environment with a lowercase <code>env</code></p>

<p>To access the host environment in Python, use <a href="https://docs.python.org/2/library/os.html#process-parameters" rel="nofollow">os.environ</a></p>

python perl