Hello
I'll try explaining what this code does. But first: about regular expressions.
Without getting too technical, Regular Expressions are one way of describing a set of words. So, when we say
if ($str =~ /some_re/){
do something ...
}
what we're really doing is asking if the string belongs to the set described by the regular expression.
I don't (and can't) explain the whole of perl's regular expressions but I'll explain enough to make you understand the code segment.
-
/A/
describes all words that contain the capital letter A.
-
/abc/
describes all words that contain the sequence 'abc' ("abc", "sjdhabcsd", aaabc", ...).
-
/^abc/
describes all words that begin with the sequence "abc" ("abc", "abcdefg", "abcccccc").
-
/abc$/
describes all words that end with the sequence "abc" ("abc", "sjdhjabc", "aaabc").
-
/a*/
describes all words that have zero or more of the letter 'a' ("", "abcj", or anything really).
-
/(ab)*/
describes all words that have zero or more of the sequence 'ab' ("", "ab", "abab", "cdr", "tryabtry", ...).
/[abc]/
contains any of 'a', 'b', or 'c'.
/[^abc]/
none of these: for example /a[^bc]d/
means 'a' followed by anything but 'b' or 'c', followed by a 'd'.
/./
matches anything so /a.c/
describes all words than contain 'a' followed by anything, followed by a 'c' ("aac", "abc", "a c", ...). To match a dot '.', you need to escape it with a \.
foreach (qw/one one.five two two.one two.ten
two.ten.12 three three.nine four five/){
print "$_\n";
if($_ =~ /\./){ # if the word has a dot ".".
# Keep everything after the last "." in the string
my ($label) = /\.([^\.]*)$/; # a dot, followed by zero
# or more letters that are
# not dots, attached to the
# end of the string.
# return those letters
# since they're between
# brackets.
$tree->add($_, -text => $label , # add the label to $tree
-image => $mw->Getimage(folder));
}
}
Hope this helped
Aziz,,,
Update: Thanks blakem for pointing out the need for escapes for \] in <pre> tags.