Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: when $$s =~ m/\G.../gc is too verbose

by radiantmatrix (Parson)
on Feb 07, 2006 at 17:53 UTC ( [id://528576]=note: print w/replies, xml ) Need Help??


in reply to when $$s =~ m/\G.../gc is too verbose

Why not just store $$s in a local copy of $_?

#Either local $_ = $$s; #Or s//$$s/; #tricky.. ;-)

Actually, in this case, I'd be tempted to alter your approach altogether and use a regex table.

sub lexer { my ($parser) = shift; my $s = $parser->YYData->{INPUT}; # I don't get your line: 'm/\G\s+/gc; skip any spaces' my %dispatch = ( INT => qr/\G(\d+)/gc, ID => qr/\G([A-Z]\w*)/gc, #.. and so on .. ); while (my ($key, $regex) = each %dispatch) { return ($key, $1) if $$s =~ $regex; } }
<-radiant.matrix->
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet

Replies are listed 'Best First'.
Re^2: when $$s =~ m/\G.../gc is too verbose
by Corion (Patriarch) on Feb 07, 2006 at 17:58 UTC

    Your second solution is no solution:

    $_ = '!'; $s = \'No'; s//$$s/; print;

    You'd need to empty out $_ first, so the local $_ is the way.

      Absolutely. That's the tricky part. ;-) It will work when $_ is undefined, but not otherwise. Of course, you could always change it to s/.*/$$s/, but still not advisable. More of an obfu trick...

      <-radiant.matrix->
      A collection of thoughts and links from the minds of geeks
      The Code that can be seen is not the true Code
      I haven't found a problem yet that can't be solved by a well-placed trebuchet
Re^2: when $$s =~ m/\G.../gc is too verbose
by stefp (Vicar) on Feb 07, 2006 at 22:31 UTC
    About the copy: before even thinking about positions in strings, using a string copy is a no-no. Copying the string to be parsed for each token is madness.

    About a table for lexing : this is irrelevant to the discussion. Also, lexing can be more complex than matching. Yes, one can insert regular code in regex but that the sign that a table based lexing is not appropriate.

    As I said tye, using for is the right way to alias to $_. I don't like it because in the programming space, for is a loop... for me. :)

    In the natural language space, well, English is not my first language.

    So to paraphrase Churchill, for is the worst solution, but it is the only one.

    Hopefully, like said TimToady, Perl6 will be cleaner.

    -- stefp

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-03-29 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found