How to use relative paths in your Perl scripts

Perl in Windows works a little differently than it does in Unix environments. One of those differences is how the Current Working Directory (cwd) is set. In IIS (Web Server), the cwd is set to the root of your Web Site by default. Because of this, you cannot put a Perl script in your cgi-bin and reference files without a path. If you do this, Perl will look for those files in the root of your site. To remedy this issue, you must set the cwd at the top of your script. This is done with the following 4 lines of code:

BEGIN{($_=$0)=~s![/][^/]+$!!;push@INC,$_}

use Win32;
$cwd = Win32::GetCwd();
Win32::SetCwd("$cwd/cgi-bin");

This code sets the cwd as yourdomain.com/cgi-bin. You will need to change this path to reflect where you want the cwd set.

Just put this code at the top of your script and presto, all of your path problems will be solved.

<< Back

 
 
 
Copyright © 2004 Creative Brain   || Home | Digital Photo Album | Online Store | Template Depot | Career | Help & Support | Contact Us