[Courses] [Perl] Part 6: The "m//" Operator
Dan Richter
daniel.richter at wimba.com
Tue Aug 12 14:55:17 EST 2003
Hi Sneha!
>($protocol,$authority,$path)=($url=~m#(\w*)://(.*\.\w*)/(.*)#);
Okay, three comments.
First, for the benefit of other readers, I mention that the delimiter in
this case is a hash. That's perfectly legal, and many Perl users prefer to
do that, both because hashes are easy to see and because they're not used
very often in patterns. Just make sure you don't put a space between the
"m" and the hash. :-)
Second, there's a minor problem in this pattern. I foolishly neglected to
give diverse example cases. Try using this program on the URI
<ftp://example.com/foo.bar/baz>. This is a problem caused by "greediness",
meaning that each part of the regular expression will match as much as it
can before moving on. We'll see more about this later.
Third, I would have put a caret at the beginning and a dollar sign at the
end to make sure the whole string matches. But of course that depends on
exactly how the program is to be used; greediness assures that nothing will
be left out.
Finally, let me say that I am quite pleased to see your response, because
up until now I have seen no response whatsoever, and I was beginning to
wonder if anyone at all was benefiting from the course!
--
Nothing is sadder than the murder of a beautiful theory
by a gang of ugly facts.
- Jeffrey Zeldman, in A List Apart
More information about the Courses
mailing list