nysus has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks, I'm trying to extract data from a string and assign them to variables like so:
The result is this, though:use Modern::Perl; my $string = " define('DB_NAME', 'blah'); /** MySQL database username */ define('DB_USER', 'blahbah'); /** MySQL database password */ define('DB_PASSWORD', 'blahblahblah'); "; my ($db, $db_user, $db_pass); my $pre_regex = qr/^\s*define\s*\(\s*['"]/; my $post_regex = qr/['"]\s*,\s*['"]([^'"]+)['"]/; ($db, $db_user, $db_pass) = map { ($string =~ /$pre_regex $_ $post_regex/mix || 'unknown') } q +w(DB_NAME DB_USER DB_PASSWORD); say $db; say $db_user; say $db_pass;
1 unknown unknown
$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Assigning mapped regex to a list
by haukex (Archbishop) on Feb 27, 2017 at 20:54 UTC | |
by huck (Prior) on Feb 27, 2017 at 21:02 UTC | |
by haukex (Archbishop) on Feb 27, 2017 at 21:15 UTC | |
by Anonymous Monk on Feb 27, 2017 at 21:26 UTC | |
by huck (Prior) on Feb 27, 2017 at 21:24 UTC | |
by nysus (Parson) on Feb 27, 2017 at 21:08 UTC | |
by Anonymous Monk on Feb 27, 2017 at 21:08 UTC | |
by Anonymous Monk on Feb 27, 2017 at 20:57 UTC | |
|
Re: Assigning mapped regex to a list
by Anonymous Monk on Feb 27, 2017 at 20:40 UTC |