[Courses] [Perl] Part 6: The "m//" Operator

sneha sneha at vit.ac.in
Tue Aug 12 13:38:39 EST 2003


** Hi ,
        I hope, there r lotza readers making use of this course, I am one of them.
        I appreciate ur work on this. Wish to learn more :).
        The following is a small correction I made in my code...after I received ur mail...
        
ftp://example.com/foo.bar/baz
($protocol,$authority,$path)=($url=~m#^(\w*)://(.[^/]*)(.*)$#); 

friendly
sneha :o)

----- Original Message ----- 
From: Dan Richter 
To: courses at linuxchix.org 
Sent: Tuesday, August 12, 2003 6:25 PM
Subject: Re: [Courses] [Perl] Part 6: The "m//" Operator


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

_______________________________________________
Courses mailing list
Courses at linuxchix.org
http://mailman.linuxchix.org/mailman/listinfo/courses


More information about the Courses mailing list