Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Win32 Apache && File::Basename

by Flame (Deacon)
on Jan 22, 2002 at 03:18 UTC ( [id://140519]=perlquestion: print w/replies, xml ) Need Help??

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

I've been using File::Basename on $0 to get the filename of the current program for quite a while, however, now I'm having a few problems. Although it all works perfectally from a DOS prompt, executing it under an apache server returns a dos filename (eg: guildmaster.cgi becomes (after an lc() was applied to it) guildm~1.cgi.)

To summerize my question, is there any way to get the long filename instead of the dos filename?

Thanks


-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GIT d- s:++ a--- C++++ UL P+++>++++ L+ E- W++>+++ N !o K- w+ O---- M-- V--
PS PE Y- PGP t++(+++) 5(+++)++++ X R+@ tv+ b+++ DI+ D- G e->+++ h! r-- y-
------END GEEK CODE BLOCK------
Translate


"Weird things happen, get used to it."

Flame ~ Lead Programmer: GMS

Replies are listed 'Best First'.
Re: Win32 Apache && File::Basename
by $code or die (Deacon) on Jan 22, 2002 at 04:01 UTC
    Win32::GetLongPathName($0) will return the long filename.

    Simon Flack ($code or die)
    $,=reverse'"ro_';s,$,\$,;s,$,lc ref sub{},e;$,
    =~y'_"' ';eval"die";print $_,lc substr$@,0,3;
Re: Win32 Apache && File::Basename
by trs80 (Priest) on Jan 22, 2002 at 05:18 UTC
    Well I was almost ready to post a $ENV{SCRIPT_FILENAME} option, but before I did I ran a benchmark on the three options (includes the option of the wrong filename). It seems that the Win32::GetLongPathName is in deed the fastest with the correct filename. Here is my simple Benchmark code:

    #!perl use strict; use File::Basename; use CGI; use Benchmark; use Win32; my $cgi = CGI->new(); print $cgi->header; print "Hello World<br>"; print "DOS NAME: " . basename($0) . "<BR>"; print "ENV BASENAME: " . basename($ENV{SCRIPT_FILENAME}) . "<br>"; print "Win32 BASENAME: " . basename(Win32::GetLongPathName($0)) . "<br +>"; timethese (100000, { 'basename_env' => sub { basename(Win32::GetLongPathName($0)) }, 'basename_win32' => sub { basename($ENV{SCRIPT_FILENAME}) }, 'basemane_dollar0' => sub { basename($0) }, } ); 1;

    The results
    Benchmark: timing 100000 iterations of 
        basemane_dollar0, 
        basename_env, 
        basename_win32...
    basemane_dollar0:  5 wallclock secs 
        ( 3.68 usr +  0.00 sys =  3.68 CPU) @ 27203.48/s (n=100000)
    basename_env: 121 wallclock secs 
        (34.99 usr + 81.07 sys = 116.06 CPU) @ 861.65/s (n=100000)
    basename_win32:  5 wallclock secs 
        ( 4.24 usr +  0.00 sys =  4.24 CPU) @ 23557.13/s (n=100000)
    

    Now I would like to know why the $ENV way is SO slow. I had expected there might be a little difference, but 10 times slower?

    UPDATE: The above code has a glaring error. The subs for env and win32 were flip flopped in the timethese function. The Win32 way is 10 times slower not the ENV way. I apologize for the late night code :^)

      Of course the basename_env solution takes the longest .. %ENV is a tied hash.

      But then, it *is* portable (if unnecessary under UNIX). And let's be honest, you won't be calling it 1,000,000 times in a given program; I'd be surprised if you called it more than once.

      dmm

Re: Win32 Apache && File::Basename
by Flame (Deacon) on Jan 22, 2002 at 05:48 UTC
    I'm a little hesitant to use the Win32 method because I want the program to be able to run on linux etc without any editing to speak of.

    Does anyone have any suggestions as to how I could do that?


    -----BEGIN GEEK CODE BLOCK-----
    Version: 3.12
    GIT d- s:++ a--- C++++ UL P+++>++++ L+ E- W++>+++ N !o K- w+ O---- M-- V--
    PS PE Y- PGP t++(+++) 5(+++)++++ X R+@ tv+ b+++ DI+ D- G e->+++ h! r-- y-
    ------END GEEK CODE BLOCK------
    Translate


    "Weird things happen, get used to it."

    Flame ~ Lead Programmer: GMS

      The my first post demonstrates the use of $ENV{SCRIPT_FILENAME} which is Unix and Win32 safe.
      Don't let the benchmark scare you off from using it.
      Did you need something different from that?
        Actually, I must confess, I don't really understand what the benchmarks reproted. Obviously I'd want to go for speed as much as possible, but compatability is more important at the moment. So I guess I will use $ENV{'SCRIPT_FILENAME'}.

        Out of curiosity, does anyone have any idea what MAKES it the slowest?


        -----BEGIN GEEK CODE BLOCK-----
        Version: 3.12
        GIT d- s:++ a--- C++++ UL P+++>++++ L+ E- W++>+++ N !o K- w+ O---- M-- V--
        PS PE Y- PGP t++(+++) 5(+++)++++ X R+@ tv+ b+++ DI+ D- G e->+++ h! r-- y-
        ------END GEEK CODE BLOCK------
        Translate


        "Weird things happen, get used to it."

        Flame ~ Lead Programmer: GMS

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-03-29 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found