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

Fellow Monks,

I know some information can be entered on the command line, but what about a directory path? For example I would like to have my script (in this case - script.pl) use the directory information contained after the script is called. For example at the prompt type:
perl script.pl c:\Documents and Dettings\All Users
And when return is pressed, "c:\Documents and Settings\All Users" is assigned to a scalar in the script for later use. Could this be done using GetOpt? or @ARGV? Any help would be much appreciated

Jonathan

Replies are listed 'Best First'.
Re: processing on the command line
by BrowserUk (Patriarch) on Mar 02, 2004 at 15:09 UTC

    Just quote the path or any single parameter if it contains spaces.

    C:\>perl -le "print for @ARGV" "c:\Documents and Settings\All Users" " +the quick brown fox" the quick brown fox c:\Documents and Settings\All Users the quick brown fox the quick brown fox

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
Re: processing on the command line
by dragonchild (Archbishop) on Mar 02, 2004 at 15:06 UTC
    tinita's question to you is very much on point. What have you actually tried? Have you attempted to research this question on your own? This is a very basic question which is address in nearly all the documentation I can think of.

    Now, if your question is "Should I use a Getopt module or @ARGV?", that's a style question, dependent primarily on how quick you need to get this up and running and how often you expect this module to be used in the future, and by whom. Personally, I feel that if a script is going to live for longer than one incantation, the time spent with Getopt is valuable.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: processing on the command line
by tinita (Parson) on Mar 02, 2004 at 14:59 UTC
    my $directory = shift @ARGV;
    had you actually tried @ARGV before posting? or do i miss something in your question?

    for future use, i want to recommend perlvar; this contains all special perl variables with explanation of their purpose.

    Update: ok, i see, you're asking a shell question. you should have stated that you only get the first part until the space. I'm on linux and i couldn't guess that.
    you can either escape the spaces correctly as it should be documented for your 'shell' which probably is DOS commandline,
    or you can join() all arguments together with spaces.

      "or do i miss something in your question?"

      I think you missed the part of the spaces and backslashes. To use your code, the input should be quoted (apostrophe) or escaped (backslashes):

      $ cat script.pl my $directory = shift @ARGV; print $directory."\n"; $ perl script.pl 'c:\Documents and Settings\All Users' c:\Documents and Settings\All Users $ perl script.pl c:\\Documents\ and\ Settings\\All\ Users c:\Documents and Settings\All Users

      Update Thanks to Corion I should add this disclaimer: "varying on the shell in use" ;)

      --
      b10m

      All code is usually tested, but rarely trusted.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: processing on the command line
by blue_cowdawg (Monsignor) on Mar 02, 2004 at 15:27 UTC

        Could this be done using GetOpt? or @ARGV? Any help would be much appreciated

    Very simply:

    foreach $item(@ARGV){ print $item,"\n"; }
    When I run that code with:
    C:\Documents and Settings\Administrator\My Documents>foo.pl "c:\Docume +nts and Settings\Administrator\My Documents"
    I very predictably get:
    c:\Documents and Settings\Administrator\My Documents
    Remove the bounding quotes and I get
    c:\Documents and Settings\Administrator\My Documents
    Why? Very simple: your (or Microsoft's shell more correctly) interprets the spaces and feeds things separated by spaces as separate arguments. If your script is to deal with that then it is up to you as a programmer to deal with that.


    Peter L. Berghold -- Unix Professional
    Peter at Berghold dot Net
       Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.