Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^4: Read UNIX environment variable

by Corion (Patriarch)
on Feb 09, 2011 at 11:20 UTC ( [id://887175]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Read UNIX environment variable
in thread Read UNIX environment variable

Yes and no.

You can read an environment, but as cron jobs run their own environment, you need to load the environment you want. See Get default login environment.

Replies are listed 'Best First'.
Re^5: Read UNIX environment variable
by Anonymous Monk on Feb 09, 2011 at 12:25 UTC
    Thanks for the heads up. I'm a bit confused though. In he example you link to do I need to change the rest of my script or does $ENV get overwritten by the function? Do I just call %ENV = (%ENV, get_login_env()); and continue as I did before With my $varname = $ENV{'ENV_VAR_NAME'};? Thanks

      The function returns a hash. What you do with that hash is up to you. You haven't shown your program, so I can only give you general advice.

      One approach would be to set up %ENV with the newly found values. Another one would be to merge %ENV with the newly found values, as you (and the example) show. A third approach would be to keep the two sets of values separate.

      To simulate running under the login environment as closely as possible, %ENV = get_login_env() will certainly do that, but in the process, you lose any indication that your program is running as cron job. In general, I think the approach of merging %ENV and the values from the login environment, with the values from the login environment taking precedence is a good one.

        Well, it works from the prompt but not via cron.
        use strict; use warnings; use MIME::Lite; # from the link you gave sub get_login_env { local %ENV; my $shell = shift || (getpwuid($<))[8]; my $env = `echo env | perl -e 'exec {"$shell"} -sh'`; if (wantarray) { my @pieces = ( $env =~ m/^(.*?)=((?:[^\n\\]|\\.|\\\n)*)/gm ); s/\\(.)/$1/g foreach @pieces; return @pieces; } else { return $env; } } %ENV = (%ENV, get_login_env()); my $mailvar = $ENV{'TESTMAILUSRS'}; my @mailUsers; my $msg = new MIME::Lite(); $msg->build( From => 'us@here.com', Subject => 'Email from perl via cron', Type => 'text/html', Data => 'testing', Debug => 1 ); for (split /[\s\n]/, $mailvar){ if($_){ $msg->add(To => $_); } } eval { $msg->send }; die "MIME::Lite->send failed: $@\n" if $@;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-18 06:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found