For Programmers: Free Programming Magazines  


Home > Archive > PERL Modules > July 2004 > strange behavior in File::Basename









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author strange behavior in File::Basename
perl coder

2004-07-22, 3:56 pm

I'm using Perl 5.6.1 on Debian Linux 3.0

I noticed the module File::Basename doesn't behave like the shell
commands basename/dirname in a special case that's not described in the
module's documentation.

When a full directory path is given to the shell commands, the result is
consistent, no matter if the path ends with a trailing / or not:

$ dirname /usr/local/; basename /usr/local/
/usr
local
$ dirname /usr/local; basename /usr/local
/usr
local

When a full directory path is given to File::Basename, the result only
makes sense when the path does not contain a trailing / character:

$ perl -MFile::Basename -we '$p="/usr/local/"; print dirname($p),"\n",basename($p),"\n";'
/usr

$ perl -MFile::Basename -we '$p="/usr/local"; print dirname($p),"\n",basename($p),"\n";'
/usr
local

If the given path ends in /, then the final path element dissapears!
Unless I'm writing code for the great Houdini, I think this is a bug,
what do you think? :-)


--
No crazy stuff in my email. ;-)
Slaven Rezic

2004-07-22, 8:55 pm

perl coder <perlcdr@mail.rumania> writes:

> I'm using Perl 5.6.1 on Debian Linux 3.0
>
> I noticed the module File::Basename doesn't behave like the shell
> commands basename/dirname in a special case that's not described in the
> module's documentation.
>
> When a full directory path is given to the shell commands, the result is
> consistent, no matter if the path ends with a trailing / or not:
>
> $ dirname /usr/local/; basename /usr/local/
> /usr
> local
> $ dirname /usr/local; basename /usr/local
> /usr
> local
>
> When a full directory path is given to File::Basename, the result only
> makes sense when the path does not contain a trailing / character:
>
> $ perl -MFile::Basename -we '$p="/usr/local/"; print dirname($p),"\n",basename($p),"\n";'
> /usr
>
> $ perl -MFile::Basename -we '$p="/usr/local"; print dirname($p),"\n",basename($p),"\n";'
> /usr
> local
>
> If the given path ends in /, then the final path element dissapears!
> Unless I'm writing code for the great Houdini, I think this is a bug,
> what do you think? :-)
>


Maybe it's a bug, but it's too late to change it --- existing scripts
may rely on this behavior. But an additional CAVEAT section in the
File::Basename doc would be nice...

Regards,
Slaven

--
Slaven Rezic - slaven <at> rezic <dot> de

Start a WWW browser - OS independent:
http://user.cs.tu-berlin.de/~eserte...erl/WWWBrowser/
Ilmari Karonen

2004-07-24, 8:55 am

On 2004-07-22, Slaven Rezic <slaven@rezic.de> wrote:
> perl coder <perlcdr@mail.rumania> writes:
>
> Maybe it's a bug, but it's too late to change it --- existing scripts
> may rely on this behavior. But an additional CAVEAT section in the
> File::Basename doc would be nice...


It is a bug. dirname() and basename() are documented to produce the
same results on Unix as the corresponding shell commands. In this
case, they obviously don't.

Besides, the current output just makes no sense. Nobody sane would
deliberately rely on it. Fixing it would probably cure more latent
bugs than it would introduce.

--
Ilmari Karonen
If replying by e-mail, please replace ".invalid" with ".net" in address.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com