Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: .env loading the dot env file?

by perlfan (Vicar)
on Mar 28, 2021 at 18:32 UTC ( #11130519=note: print w/replies, xml ) Need Help??


in reply to .env loading the dot env file?

.env is not standard in any traditional shell I am aware of, but that set is limited to: sh/bash and csh/tcsh.

If the format of that file indicates a standard shell "rc" type file with (for Bourne style), then I would not attempt to read it in your Perl program since it can contain anything the shell supports. The following is deceptive because it's just a basic shell script:

VAR="some value" export VAR

Rather, source it before your Perl program is invoked. This will do the right thing, and be available in %ENV.

If it's an ini format, use something like Config::Tiny - or whatever module is appropriate for the serialization format. Because that's all it is; don't invent your own. you would not do this for JSON, YAML, or XML - I hope :)

A few notes about understanding the %ENV in your Perl program; and assuming we're dealing strictly within the same process space on the same machine, etc:

  • your entire environment is available to a perl process under which it is invoked via %ENV
  • modifying %ENV in your program is respected by the current process and all child processes created by it (including via system, fork etc
  • child processes can affect their own env and that of their ancestors descendants, but not of their parent - even if you export - this means when your program returns from running to your interactive shell (e.g.,) the environment will not have changed based on anything your program did (and it can't)
  • best practice is to localize %ENV if you plan on mucking with it in your perl program, e.g., local %ENV = %ENV; - this is so you can control the scope of the changes in the same process using Perl's built in scoping; in other words, it allows you to "go back" to the original %ENV or at least only affect it in very precise parts of your program; as noted above, it is not needed to "protect" your interactive shell's environment once the program has returned

The last thing I'll end with is, don't parse files that are meant to affect your shell environment in Perl. Use the shell to source it; then invoke your program. HTH

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2022-08-08 13:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?