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

Dear Monks,

I seek to find a way for getopt to accept a option value with spaces, e.g.

myhack.pl --foo='bar1 bar2 bar3'

Your's humbly, /Mats

Replies are listed 'Best First'.
Re: Space in getopt option value
by toolic (Bishop) on Aug 01, 2012 at 18:32 UTC
    Here is one way:
    use warnings; use strict; use Getopt::Long; use Data::Dumper; $Data::Dumper::Sortkeys=1; my %opt; GetOptions(\%opt, 'foo=s') or die; print Dumper(\%opt); __END__ myhack.pl --foo 'bar1 bar2 bar3' $VAR1 = { 'foo' => 'bar1 bar2 bar3' };
    The you can process $opt{foo} if you wish.
Re: Space in getopt option value
by frozenwithjoy (Priest) on Aug 01, 2012 at 18:53 UTC
    Are you passing multiple values to the --foo argument or one string value with spaces? If the former, you can use something like this:
    myhack.pl --foo bar1 --foo bar2 --foo bar3

    Be sure to change GetOptions ("foo=s" => \$foo); to GetOptions ("foo=s" => \@foo);

    see: Options with multiple values

Re: Space in getopt option value
by Anonymous Monk on Aug 01, 2012 at 21:47 UTC

    I seek to find a way for getopt to accept a option value with spaces, e.g.

    What operating system and or shell are you using?

    Getopt parses from @ARGV, and how @ARGV gets populated depends on your shell

    On windows cmd.exe

    $ perl -MData::Dump -e " dd\@ARGV " -- --foo='bar1 bar2 bar3' ["--foo='bar1", "bar2", "bar3'"]

    This is correct, as ' is not a quoting character on cmd.exe -- you have to quote according to your shell -- more on this in perlrun, Behind the GUI lives the Shell and How Command Line Parameters Are Parsed