Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Adding a directory to $ENV{PATH}

by amelinda (Friar)
on Nov 03, 2000 at 03:50 UTC ( [id://39781]=perlquestion: print w/replies, xml ) Need Help??

amelinda has asked for the wisdom of the Perl Monks concerning the following question:

I'm (finally) writing some error checking on a program. It needs to find and make sure the user can run a few programs. Now, md5 and mimencode (yes, that program) are pretty common, and they're most likely to already be in the user's PATH, but openssl isn't. I'd like to add /usr/local/openssl/bin/ to the PATH, so that it can check in that place of last resort before dieing.

I've tried the following, none of which seemed to work: (no, not all at the same time)

$ENV{PATH} = $ENV{PATH} . ":/usr/local/openssl/bin"; use lib qw(/usr/local/openssl/bin/); push @INC, "/usr/local/openssl/bin/";
The next two lines are:
chomp($openssl = `which openssl`); die "openssl required\n" unless (-X $openssl);
And it always die's. These attempts are based on reading the Blue Camel book, and how to add path to @INC. That node, however, is about adding a path for a module, so I'm not sure if it's appropos.

Replies are listed 'Best First'.
Re: Adding a directory to $ENV{PATH}
by Adam (Vicar) on Nov 03, 2000 at 03:59 UTC
Re: Adding a directory to $ENV{PATH}
by dws (Chancellor) on Nov 03, 2000 at 04:47 UTC
    Any modifications you make to $ENV{PATH} effect the address space of the Unix process that the script is executing in, and not the processes environment. (The environment is kept in kernel memory.)

    If you want to add to the and have that honored by system (or backticks or exec()), you'll need to reinvoke the script. In doing so, perl passes $ENV{PATH} to which ever flavor of exec(1) the runtime uses, where it will truly be part of the environment of the nested invocation of your script.

    #!/usr/local/bin/perl5
    if ( $ARGV[0] eq "child" ) {
        print "child path: $ENV{PATH}\n";
        exit(0);
    }
    $ENV{PATH} .= ":/dev/null";
    print "parent path: $ENV{PATH}\n";
    system("/usr/local/bin/perl5", $0, "child");
    
      would adding the PATH modification in a BEGIN block fix that issue?
        No, because it merely side-effects the address space of the current Perl process. You need to get the new path into into kernel memory before it will be honored by exec().
RE: Adding a directory to $ENV{PATH}
by geektron (Curate) on Nov 03, 2000 at 03:59 UTC
    @INC and 'use lib' won't work because you're working with an external ( non-perl ) command. @INC is a library-location directory. it's where you would find perl functions/modules.

    the assignment to $ENV{PATH} looks like it would work . . .

    why not simply hard-code the openssl program:

    $openssl = '/usr/local/openssl/bin/openssl';
      It might not be there. Sometimes admins put things in weird places (I should know, I am one), and I'd rather code in more choice than less.

      I figured that @INC might not work, because the node I borrowed the idea from was talking about a module, but it was worth a try.

        if you're "putting things in wierd places", you'll always run into trouble with what you're attempting to do.

        you'll have to change the attempts to modify $ENV{PATH}, or have a long list of directories to attempt to shift into $ENV{PATH}.

        I think you need to go back and look at design specs. if you can't depend on the location of the external program, maybe you shouldn't be using it. . . .

Re: Adding a directory to $ENV{PATH}
by amelinda (Friar) on Nov 03, 2000 at 22:28 UTC
    Why does it always seem that I always have to come back and correct my stupid mistakes because they're never what I thought was the problem?

    As it turns out, the problem is not with $ENV{PATH} = $ENV{PATH} . ":/usr/local/openssl/bin"; after all. When followed by system("printenv");, /usr/local/openssl/bin is in there at the end, and if followed by system("openssl");, it runs openssl.

    So what the heck happens with my code? which is a shell built-in function, so it totally ignores the $ENV{PATH} in perl, and re-reads (or otherwise pulls out of its ass) my .cshrc, which doesn't have the "right" PATH. So, now I'm writing my own which (in perl), to get around this issue.

Re: Adding a directory to $ENV{PATH}
by amelinda (Friar) on Nov 03, 2000 at 04:25 UTC
    I probably should have mentioned, this is in UNIXland, not in Windowsland. I'm writing it on BSDi, but it should probably be as portable as possible to other unices.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (9)
As of 2024-03-28 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found