J
juliani.moon
I am using perl v5.8.5.
I made a simple construct to make email addresses hyper-linkable:
if ($_ =~ / ([a-zA-Z\.]+\@[a-zA-Z\.]+) /g) {
s!$1!<a href="mailto:$1">$1</a>!g;
}
However it only effectively takes out the email address, as if only
the first "$1" matched and the "$1" at the 2nd and 3rd places becomes
empty because it replaces an email address only with:
<a href="mailto:"></a>
Then I tried to use "$&" in places of "$1", as in:
if ($_ =~ / ([a-zA-Z\.]+\@[a-zA-Z\.]+) /g) {
s!$&!<a href="mailto:$&">$&</a>!g;
}
This time the email addresses are found and replaced with html codes
like:
<a href="mailto: (e-mail address removed) "> (e-mail address removed) </
a>
-- but "$&" carries a space on both sides of the email address.
It seems trivial and unnecessary although I can work around by this:
if ($_ =~ / ([a-zA-Z\.]+\@[a-zA-Z\.]+) /g) {
$emem = $1;
s!$emem!<a href="mailto:$emem">$emem</a>!g;
}
My wonders are (1) I saw examples where "$1" was used as in my first
example. Why it fails to work as in my first example? (2) Is it by
default that "$&" carries spaces on both sides of its content?
(To"clean" it, I would have to introduce a new variable as "$&" is a
"read-only value"). Things shouldn't be that complicated, are they?
Joe
I made a simple construct to make email addresses hyper-linkable:
if ($_ =~ / ([a-zA-Z\.]+\@[a-zA-Z\.]+) /g) {
s!$1!<a href="mailto:$1">$1</a>!g;
}
However it only effectively takes out the email address, as if only
the first "$1" matched and the "$1" at the 2nd and 3rd places becomes
empty because it replaces an email address only with:
<a href="mailto:"></a>
Then I tried to use "$&" in places of "$1", as in:
if ($_ =~ / ([a-zA-Z\.]+\@[a-zA-Z\.]+) /g) {
s!$&!<a href="mailto:$&">$&</a>!g;
}
This time the email addresses are found and replaced with html codes
like:
<a href="mailto: (e-mail address removed) "> (e-mail address removed) </
a>
-- but "$&" carries a space on both sides of the email address.
It seems trivial and unnecessary although I can work around by this:
if ($_ =~ / ([a-zA-Z\.]+\@[a-zA-Z\.]+) /g) {
$emem = $1;
s!$emem!<a href="mailto:$emem">$emem</a>!g;
}
My wonders are (1) I saw examples where "$1" was used as in my first
example. Why it fails to work as in my first example? (2) Is it by
default that "$&" carries spaces on both sides of its content?
(To"clean" it, I would have to introduce a new variable as "$&" is a
"read-only value"). Things shouldn't be that complicated, are they?
Joe