Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: use return in regex => allowed or side effects ?

by Anonymous Monk
on Apr 16, 2018 at 13:22 UTC ( [id://1212988]=note: print w/replies, xml ) Need Help??


in reply to use return in regex => allowed or side effects ?

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)?

Replies are listed 'Best First'.
Re^2: use return in regex => allowed or side effects ?
by Anonymous Monk on Apr 16, 2018 at 18:09 UTC

    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: note [id://1212988]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-29 01:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found