1

One of my colleagues wrote a perl script that asks for the user's windows domain/user name, which of course we enter the the following format domainname\username. The Getopt:Long module then converts this into a string dropping out the '\' character and rendering the string incorrect. Of course, we could just ask all our users to enter their domain/user combo as domainname\\username but I really don't want to be guilty "fix the user, not the programme". We also use a module we made for this, I'll call OurCompany::ColdFusionAPI since it accesses ColdFusion.

Our code looks like this:

#!/usr/bin/perl

use common::sense;
use Getopt::Long;

use OurCompany::ColdFusionAPI;

my ($server_ip, $username, $password, $need_help);

GetOptions (
    "ip|server-address=s" => \$server_ip,
    "user-name=s"         => \$username,
    "password=s"          => \$password,
    "h|help"              => \$need_help,
);
$username   ||= shift;
$password   ||= shift;
$server_ip  ||= shift;

if (!$server_ip or $need_help){
    print_help();
    exit 0;
}

my $print_hash = sub { my $a = shift; say "$_\t=> $a->{$_}" foreach keys %$a; };

...

If I add the line say $username then it just gives the string without the '\'. How can I get perl to keep the '\'? Something along the lines of read -r in bash.

Konerak
  • 39,272
  • 12
  • 98
  • 118

2 Answers2

11

Your shell does that, not Getopt::Long. You need to escape the \ in order for your shell to interpret it as a literal backslash rather than an attempt to escape something.

Dan
  • 10,531
  • 2
  • 36
  • 55
9

Are you sure this is due to Getopt::Long? Most likely your shell is already parsing what you're typing, and messing with the backslashes.

Why not ask Domain and Username seperately? That would solve the problem somewhat elegantly.

Konerak
  • 39,272
  • 12
  • 98
  • 118