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

Fellow Monks,

I have a script on a Win32 platform (Perl 5.6.1) that accepts command-line arguments via Getopt::Long. I also have a requirement that it be able to accept an argument like:

perl test.pl --str=$%^@*jimbo
Unfortunately, the caret is lost when I print the string.
#!/perl/bin/perl use strict; use warnings; use Getopt::Long; my $str; GetOptions ( 'str=s' => \$str ); print $str, "\n";

If I have no control over the contents of the string, how can I prevent losing the character?

Where do you want *them* to go today?

Replies are listed 'Best First'.
Re: Getopt::Long loses char
by jsprat (Curate) on Feb 13, 2003 at 19:55 UTC
    It's a Win32 thing. Wrap the offending argument in double quotes perl test.pl --str="$%^@*jimbo" and it will work as you'd expect. If you have any double quotes in the string, they'll need to be back-whacked.

    Just like any other shell, the command prompt (cmd.exe or command.com - you don't say what version of Windows you are running) has certain requirements for quoting on the command line. I took a quick look in the Win2k help file and at msdn for quoting recommendations, but didn't see any documentation. Good luck!

Re: Getopt::Long loses char
by Thelonius (Priest) on Feb 13, 2003 at 20:01 UTC
    There is nothing you can do in your Perl program. The characters are gone before Perl has begun running. The ^ is a continuation character for the Windows shell and % is the variable substitution character. It's the responsibility of whoever runs your program to make sure that any special characters are quoted/doubled/escaped.

    For example, you can say perl test.pl --str=$%^^@*jimbo

Re: Getopt::Long loses char
by zengargoyle (Deacon) on Feb 13, 2003 at 19:12 UTC

    it's likely Windows command line processing (maybe caret is used for control characters, ie ^G == Ctrl-G). try putting single or double quotes around the option. you may also be able to '\' escape the caret. it works fine under linux.

    $ perl test.pl --str=$%^@*jimbo
    $%^@*jimbo
    $ perl test.pl --str='$%^@*jimbo'
    $%^@*jimbo
    $ perl test.pl '--str=$%^@*jimbo'
    $%^@*jimbo
    $ perl test.pl --str="$%^@*jimbo"
    $%^@*jimbo
    $ perl test.pl "--str=$%^@*jimbo"
    $%^@*jimbo
    

      I tried these already, but always end up losing either '%' or '^'.

      Where do you want *them* to go today?
Re: Getopt::Long loses char
by steves (Curate) on Feb 13, 2003 at 19:23 UTC

    Also works fine with 5.6.1 on Solaris.

    Carat is a Windoze thing according this simple test:

    C:\WINNT\system32>echo foo^bar foobar C:\WINNT\system32>
Re: Getopt::Long loses char
by thezip (Vicar) on Feb 14, 2003 at 01:43 UTC

    When I doubled both the '%' and the '^', it did the trick:

    perl test.pl --str=$%%^^@*jimbo

    Thanks to all...

    Where do you want *them* to go today?