Page 1 of 1

Perl current directory always is c:/xamp/

PostPosted: 01. June 2010 14:26
by Roland684
I've got xampp with mod_perl running out of the box.

I have a small perl script to list all files in the current directory (the directory the script is in, "./") which works on servers with proper perl (not mod_perl), but xampp (with mod_perl) on my desktop seems to think c:/xamp/ is the current directory, no matter where the perl script is placed.

Hardcoding the correct path will break the script for other servers and building a function to detect wether or not the script is running on xampp is just silly. The current directory should just be the directory the script is placed in.
I'm sure this is just a configuration error, but I'm not sure what to look for.

How can I fix this?

Re: Perl current directory always is c:/xamp/

PostPosted: 01. June 2010 15:09
by Nobbie
Roland684 wrote:How can I fix this?


You *must not* use relative pathnames, but fixed full pathnames. For compatibility reasons, build the pathnames via using Server Variables like DOCUMENT_ROOT, SCRIPT_FILENAME, SCRIPT_NAME etc.

It is indeed true, that for CGI scripts the default directory is the "ServerRoot" Directory (see httpd.conf), but for moduled PHP Scripts for example it is the directory which holds the script. Using the above mentioned method, you are able to build full qualified pathnames that do NOT depend on the default directory but are still fully portable anyway.

Roland684 wrote:to detect wether or not the script is running on xampp is just sill


No, its not silly - its necessary. SCRIPT_FILENAME and/or SCRIPT_NAME will help you.

Re: Perl current directory always is c:/xamp/

PostPosted: 01. June 2010 16:56
by Roland684
Thanks for your replay, it helped.

but... yes, it IS silly.

When running perl standalone, SCRIPT_FILENAME and SCRIPT_NAME do not exist. On linux there is the PWD environment variable I could use, but pwd is very *nix style, so that probably doesn't exist on windows. And on a mac?

Besides not being able to use relative paths making things more complicated/insecure, it's going to be an ugly piece of code I'll have to write just to find something trivial as the current directory.

Re: Perl current directory always is c:/xamp/

PostPosted: 01. June 2010 20:30
by Nobbie
Roland684 wrote:it's going to be an ugly piece of code I'll have to write just to find something trivial as the current directory.


No! Instead of "rumbling around" about silly ugly piece of code, simply use the (very very very very very old) standard function "getcwd" (as well known for PHP as for Perl)

http://www.php.net/manual/en/function.getcwd.php

and/or

http://perl.active-venture.com/lib/Cwd.html

Many things are getting easier if one reads documentation.... (RTFM).

Re: Perl current directory always is c:/xamp/

PostPosted: 01. June 2010 20:42
by JonB
Nobbie is right, cwd is built-in and old (core Perl)

Code: Select all
use FindBin;
use Cwd qw(abs_path);
use File::Basename;

$path = abs_path($0);
$scriptname = basename(__FILE__);
$0 = "stealth";

print "The actual path to this script is: $FindBin::Bin/$FindBin::Script\n <br>";

print "The Script Path is: $path\n <br>";

print "The scriptname is: $scriptname\n <br>


:mrgreen:

Re: Perl current directory always is c:/xamp/

PostPosted: 02. June 2010 13:52
by Roland684
@nobby: don't blame me, you're the one who told me to use the environment variables in the first place.
And shouting RTFM is easy, but do you have any idea how big cpan is?

Nonetheless I do appreciate that you knew something I didn't and I was able to learn me something very usefull, even with my years of perl experience. (I'm just new to mod-perl)
I hope you can find any pride in outsmarting me and didn't just find me an annoyance wasting your time.

Both you guys, thank you very much for you help!

PS.
this did the trick:
Code: Select all
use File::Basename;
my ($file,$path)=fileparse($0);
print $path;


What johnB prints as "The actual path to this script" resolves not to the script, but to C:/xampp/apache/bin/httpd.exe
And the abs_path isn't always a defined function (I'm not going to install additional packages for this). Fortunately I do not need the absolute path, a relative path will do. As long as it is a path to the script, not to httpd.exe or perl.exe.

Re: Perl current directory always is c:/xamp/

PostPosted: 02. June 2010 20:21
by JonB
Good code snip (haven't tested)

BUT I assure my code does not resolve to httpd.exe (at least on my configuration), Here's my actual output:

The actual path to this script is: C:/xampp/htdocs/perl/test.pl
The Script Path is: C:/xampp/htdocs/perl/test.pl
The scriptname is: test.pl

http://bravo.newnetenterprises.com/perl/test.pl

8)

"neener neener" :mrgreen:

Re: Perl current directory always is c:/xamp/

PostPosted: 02. June 2010 21:59
by JonB
After giving it some thought, I'm pretty sure why my code resolved that way.
"I'm not running mod_perl."

In mod_perl, Perl is virtualized into Apache, thus, in fact, the script is C:\xampp\apache\httpd.exe.

Thus - that piece of code is working right, but giving an easily misunderstood result.

:shock:

Re: Perl current directory always is c:/xamp/

PostPosted: 27. May 2011 12:00
by feddd


Thanks Nobbie. I just started doing course work on php-stream.
I didn't know about site php.net. It helps me a lot!
:)