Tobias Eichner wrote:
Giving it a closer look, I experience a strange behaviour. I'm using ActiveState Perl on WinXp here.
The following script has been used for testing:

use File::Spec;

BEGIN
    {
    my ($volume,$softwaredir,$librarydir);

    ($volume,$softwaredir) = File::Spec->splitpath(__FILE__);
    $librarydir = File::Spec->catpath($volume,$softwaredir,'librarydemo');

    unshift (@INC,$librarydir);
    printf "%s\n" x @INC,@INC;
    };

require "sample.pl";
print "\nCalling test variable from sample.pl:\n";
print $SAMPLE::test;

Using the IDE (Perl Express, not really a pro tool, but free), it brings this output:

C:\Programme\Perl Express\Debug\librarydemo
C:/Perl/site/lib
C:/Perl/lib
.

Then it stops executing with the following error:
Can't locate sample.pl in @INC...

Guess the problem is the "\" instead of "/" in the path.

No, backslashes or forward slashes shouldn't matter. I suspect that you made some other mistake.

HOWEVER running the same on command line works as intended, although (!) a strange path is added to @INC:

C:\Dokumente und Einstellungen\Tobias\Eigene Dateien\Meine Perl-Programme>perl require1.cgi
librarydemo
C:/Perl/site/lib
C:/Perl/lib
.

As Jenda pointed out, calling a script as

    perl scriptname.pl

results in __FILE__ not containing the full path, but just the actual file name.

And a second HOWEVER that makes it more strange, at least for me. I gave it a try on my local Apache 2 web server (127.0.0.1/cgi-bin/require.cgi; just added the line print "Content-type: text/plain; charset=utf-8\n\n"; to ensure that no malformed header error is generated). It gives this output:

C:/Programme/Apache Software Foundation/Apache2.2/cgi-bin/librarydemo
C:/Perl/site/lib
C:/Perl/lib
.

Calling test variable from sample.pl:
12345
There, anything is as it should - correct slashes used in the path, no error. Since I intend to write CGI programs, I'm happy so far :-)

But I have no explanation for this behavior nor do I know how this may or may not influence reliability or cross platform suitability of programs using this way of adding paths to @INC.

To me it seems as there is no perfect method. As long as you invoke your program from command line, perhaps FindBin is the best choice. But since you are going to write CGI programs, FindBin's failure as regards mod_perl is not insignificant. Therefore I'd stick with "the __FILE__ way".

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to