Page 1 of 1

perl script

Posted: Sun Jun 24, 2012 11:13 am
by srivalli_b
hi,

I am new to perl script. could you please explain below code
my $string = shift || q{};

if ($string =~ m{\A (?: ([^/,]+) /)? ([^/,]+) (?: , ([^/,]+))? \z}xms) {
$dbsys = $1 if (defined $1);
$dbuser = $2 if (defined $2);
$dbpass = $3 if (defined $3);
}

Posted: Sun Jun 24, 2012 2:30 pm
by chulett
I'm assuming someone will be along that could help, but please keep in mind the fact that this is DSXchange, not PerlXchange. You'd be better off posting questions like this in a dedicated Perl support forum.

Posted: Mon Jun 25, 2012 4:37 am
by PhilHibbs
Well, if you trust the variable names to serve as documentation, it takes the input to the script and splits it up into a system name, a user name, and a password. The server name and appears to be optional, that's what the ? after the ) in the middle means.

Code: Select all

=~ m{\A (?: ([^/,]+) /)? ([^/,]+) (?: , ([^/,]+))? \z}xms
This is a "regular expression", and they are notoriously difficult for a person to decode. At first glance, it is looking for substrings that are separated by slashes or commas, so system/user/password or system,user,password with the /password or ,password being optional. *Edit* I mean, with the system, or system/ being optional.