Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How do I find what directory the perl script is running from?

( #47035=categorized question: print w/ replies, xml ) Need Help??
Contributed by punksk8er on Dec 16, 2000 at 13:05 UTC
Q&A  > GUI Programming


Answer: How do I find what dir the perl script is running from?
contributed by Fastolfe

Since you put this in the CGI section, I'm assuming that by Perl script you mean CGI script. Web servers typically run these out of your cgi-bin directory, using that directory as the script's current working directory. Unfortunately your question is a bit vague. Do you mean the script's current working directory, or where the script physically resides on the filesystem?

If it's the former, use the Cwd module for a portable way to do this. If it's the latter, this isn't really a CGI issue and is already discussed elsewhere. See: How do I get the full path to the script executing? The only catch is that this information can be faked or made unreliable, but it's usually OK so long as you're not relying on it as a security mechanism.

Answer: How do I find what dir the perl script is running from?
contributed by merlyn

No. There are partial answers based on examining $0 and $ENV{PATH}, with algorithms rolled up into File::FindBin, but nothing will ever be completely unspoofable or accurate.

Answer: How do I find what dir the perl script is running from?
contributed by cei

I picked up this cross-platform directory checker somewhere... Works on both NT and *NIX

if ($0=~m#^(.*)\\#) { $cgi_dir = "$1"; } elsif ($0=~m#^(.*)/# ) { $ cgi_dir = "$1"; } else {`pwd` =~ /(.*)/; $ cgi_dir = "$1"; }
Answer: How do I find what dir the perl script is running from?
contributed by Theodorus

I use:

use FindBin qw($Bin);

$Bin is set to the value of the script's path.

Answer: How do I find what dir the perl script is running from?
contributed by Vennis

This works quite well:

$0=~/^(.+[\\\/])[^\\\/]+[\\\/]*$/; $cgidir= $1 || "./";

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (8)
    As of 2014-12-21 12:28 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (104 votes), past polls