Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks
Can I use a return statement, leaving the regex and a sub, within a regex, like in the following example ? Or should this not be done because of any side effect ? (like loosing memory ...)
I do not see any side effect, but I have not looked intensivly.
(Remark: in my program, the sub convert is doing much more and the hash %def has many different strings as keys and vals)
Many thanks for your opinion !
program output:use strict; use warnings; my %defs=(map {$_ => '-'}'a'..'p'); # generate simple example pairs my $test1='a1 $a $q $c 2b'; # use undefined hash. => ERROR my $test2='a1 $a $b $c 2b'; # only defined hashes => no ERROR print '<'.convert($test1).">\n"; print '<'.convert($test2).">\n"; sub convert { $_=shift; s/\$([\w_\d]+)/unless(defined($defs{$1})) { print "ERROR: <\$$1> is not defined in "; return($_); # ok or bad style ?? } $defs{$1} /egx; return($_); }
ERROR: <$q> is not defined in <a1 $a $q $c 2b> <a1 - - - 2b>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: use return in regex => allowed or side effects ?
by AnomalousMonk (Archbishop) on Apr 17, 2018 at 03:09 UTC | |
|
Re: use return in regex => allowed or side effects ?
by Anonymous Monk on Apr 16, 2018 at 13:22 UTC | |
by Anonymous Monk on Apr 16, 2018 at 18:09 UTC |