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

I'm trying to have a command line argument that is a directory name. The directory name has a space in it. If I add an extra / and a space on the end and place it in single quotes it works. With out the extra space after the last slash it truncates at the space in the name making it invalid. Is there any way to avoid having to remember to add the extra space at the end of the quotes string ?

Replies are listed 'Best First'.
Re: Directory with space on command line
by mvaline (Friar) on Feb 05, 2011 at 23:13 UTC

    Command line argument parsing is handled by your shell, not by Perl. See the documentation for your shell for details on how it handles single and double quotes.

    Unix shell documentation is pretty easy to find. Under Windows, when running CMD.EXE, your options include enclosing the path in double quotes, escaping the space characters, or reverting to DOS-style 8.3 filenames. The new PowerShell is a bit more sophisticated, but enclosing paths in single-quotes is usually correct.

      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.

        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.
Re: Directory with space on command line
by toolic (Bishop) on Feb 05, 2011 at 21:11 UTC
    Perhaps you could clarify your problem with specific examples:
    • Show a few command lines that work.
    • Show a few command lines that don't work.
    • Show a few relevant lines of your Perl code.
    • What OS/shell are you using?