in reply to Perl Regular Exp

It is probably worth pointing out what would happen if there weren't the parentheses "()" on the LHS of the assignment. Without the parentheses the assignment is in scalar rather than list context and records whether the match was successful (1) or not (empty string '') rather than assigning the list of captures in the match. The following script illustrates this

use strict; use warnings; my $dir = q{/some/path/to/afile}; print qq{\nMatching $dir\n}; print qq{\nWith parentheses, list context\n}; my ($fileName) = $dir =~ m{([^/]+)$}; print defined $fileName ? qq{-->$fileName<--\n} : qq{Not defined\n}; print qq{\nWithout parentheses, scalar context\n}; $fileName = $dir =~ m{([^/]+)$}; print defined $fileName ? qq{-->$fileName<--\n} : qq{Not defined\n}; $dir = q{/some/path/with/no/file/}; print qq{\nMatching $dir\n}; print qq{\nWith parentheses, list context\n}; ($fileName) = $dir =~ m{([^/]+)$}; print defined $fileName ? qq{-->$fileName<--\n} : qq{Not defined\n}; print qq{\nWithout parentheses, scalar context\n}; $fileName = $dir =~ m{([^/]+)$}; print defined $fileName ? qq{-->$fileName<--\n} : qq{Not defined\n};

Here's the output

Matching /some/path/to/afile With parentheses, list context -->afile<-- Without parentheses, scalar context -->1<-- Matching /some/path/with/no/file/ With parentheses, list context Not defined Without parentheses, scalar context --><--

I hope this is of use.

Cheers,

JohnGG