Methods in a Regex

So I had some old code I was debugging, trying to figure out why something wasn’t triggering when I came upon this:

grep /$event->pkgevent/, @finished_ids

That is when something twitched in the back of my head. I’m sure everybody has felt something similar at some point in there life. It’s the feeling you get when you realize something is quite _wrong_, and it should have been obvious to you.

If you don’t know what’s going on there, it is _trying_ to check and see if the $event object’s pkgevent value (via an accessor method) is inside an array called finished_ids. Here’s the problem, it’s using pattern detection, and inside the pattern, it is *not* running the subroutine. It needed to be:

grep /${\$event->pkgevent}/, @finished_ids

which marks the method as code, and says run that first, then use the value for the pattern. Of course, this is just if you want to use the method inside the pattern (barring other stuff), instead of pulling the value out to search in another manner.

Props to “HarleyPig”:http://blog.harleypig.com/ for remembering the ${\ } syntax off the top of his head.

This entry was posted in Geeky.