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

Re^5: Doing "it" only once

by Roy Johnson (Monsignor)
on Sep 21, 2005 at 17:24 UTC ( [id://493884]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Doing "it" only once
in thread Doing "it" only once

I think you want the match-only-once operator:
for ( @some_array ) { if ( ?^foo$? ) { print "skipping foo\n"; next; } if ( ?^bar$? ) { handle_bar($_); next; } handle_rest($_); }

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^6: Doing "it" only once
by Limbic~Region (Chancellor) on Sep 21, 2005 at 17:37 UTC
    Roy Johnson,
    Wow - thanks! That's neat, but I am not sure if it fits the bill. If my understanding of this is correct, it basically ignores subsequent matches if a match has already been found. If that is technically accurate, it fails in that it still checks to see if there is a match before deciding to ignore it.

    This really is a meditation about being able to modify the optree (or the p6 equivalent) while the code is running and what you would do with it if you could. I provided the example of removing a conditional block once the condition has been met and the block ran once. This means not even the condition itself is checked, it would just be "gone". This, I am sure, is not the only application.

    Cheers - L~R

      This code illustrates that the op only performs the match once:
      my $type; my $r = qr/(?{print "$type\n"})/; for (1..3) { $type = 'Regular'; /$r/; $type = 'Once only'; ?$r?; }
      On the broader subject of modifying the optree...well, it hasn't really occurred to me very often. The most common reason to do it is to introduce new syntax, which I guess is what you're suggesting, after a fashion. If I were up to it, I would probably want to implement a new operator.

      (update: But there's more to adding syntax than modifying the optree. It's gotta parse, too.)


      Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-03-29 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found