Your execution environment inherits the environment set up via ~/.bash_profile. I recommend you not try to source that file in the script. If you need to access the environment and manipulate it, you do so via %ENV. Then anything you run under that perl process will inherit this environment. A child process can't affect the parent process's (e.g., bash shell) environment once the process is complete. In other words, you can't use a script or program to update the environment of the shell in a persistent way (after the perl script has exited).

If you wish to make CTC_HOME available as part of the environment in your script you can update %ENV in the code:

use strict; use warnings; local %ENV = %ENV; # NOTE: local is used here in case you wish to # restore the original %ENV in a different scope # of this script $ENV{CTC_HOME} = q{/Inf/work/}; # ... now "CTC_HOME" is available for all child # processes (via fork, system, ``, etc) of $0 (this script)

Given this idiom, you could then pass in CTC_HOME as a commandline argument or, better still, as a configuration file that you can then parse via Config::Tiny. That said, you could also just make sure the environment is set up as you wished via ~/.bash_profile before execution to avoid all of this. I am still not sure why you want to effect ~/.bash_profile. It almost seems like you're wishing to ensure an environment via command invoked via ssh. In this case, note that ssh has options that allow you to pass remote environmental viariables via the ssh command itself.


In reply to Re: source a file in Linux using perl script by perlfan
in thread source a file in Linux using perl script by perl_007

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.