Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

use return in regex => allowed or side effects ?

by Anonymous Monk
on Apr 16, 2018 at 08:11 UTC ( [id://1212970]=perlquestion: print w/replies, xml ) Need Help??

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 !

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($_); }
program output:
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

    Statements that sneakily alter the control flow are indeed bad style.

    There are many alternative forms to choose from.

    • You could set a flag and test that after the s///.
    • Or a chain of m/\G.../g.
    • You could embed the test condition in the regular expression: s/\$(\w++)(?(?{ !defined $defs{$^N} })(?!))/$defs{$1}/; Care must be taken that the whole text is matched: use anchors, possessive matching, etc.
    • But if the %defs are not dynamic, the much simpler, and probably faster, approach is to construct the regex from your defs so that only valid tokens are accepted.
    • Use something more capable for parsing, like Marpa.

    Bottom line is, it sounds like you really need a lexer/scanner, not contorted regexes. Could you give more information pertaining to the problem at hand (the purpose of the wheel you're building)?

      Thanks for your answer !

      It is a very simple assembler (for a proprietary risc uP). The job can be done with 2 hashes and some regex.

      I never invested the time to get familiar with parser, like Marpa. Could be worth to do this.

      Would be interesting, whats less efford to program, if the language is very reduced, like in my example.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1212970]
Approved by hippo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-19 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found