in reply to Re: Directory with space on command line
in thread Directory with space on command line

I am using the CPAN module

use File::Basename;

I am working with Ubuntu and using the Terminal application under gnome. The perl instruction I am using is

my $directname = dirname($ARGV[0]);

The actual command line varies but here is an example

perl Get_SQL_Template_file_for_updating.pl '/media/MR23M19P/MySQL Dat +afiles/Dufferin Hi-Lands/ ' 26 DH M

If I place the Space after the last slash before the single quote all is fine. If I do not, the dirname subroutine assumes that the directory is

/media/MR23M19P/MySQL Datafiles

It assumes that the

MySQL

is a file name since it ends with a space and ignores the rest of the parameter, but does pick up the other parameters on the command line. Any Assistance will be greatly appreciated.

Replies are listed 'Best First'.
Re^3: Directory with space on command line
by mvaline (Friar) on Jun 16, 2011 at 15:09 UTC

    I recommend you take a look at the documentation for dirname. The behavior you are experiencing is expected.

    This function is provided for compatibility with the Unix shell command dirname(1) and has inherited some of its quirks. In spite of its name it does NOT always return the directory name as you might expect. To be safe, if you want the directory name of a path use fileparse().
    When using Unix or MSDOS syntax this emulates the dirname(1) shell function which is subtly different from how fileparse() works. It returns all but the last level of a file path even if the last level is clearly a directory. In effect, it is not returning the directory portion but simply the path one level up acting like chop() for file paths.