Contrived Example:
#!/usr/bin/perl -w
use strict;
use warnings;
use HTML::Entities;
sub sanitize {
# no name?
$_[0] ||= "anonymous";
# add missing protocol in $url
if ($_[1] && $_[1] !~ /^\w+:/) {
$_[1] = "http://" . $_[1]
}
# add missing protocol in link in $body
$_[2] =~ s{\[(.*?)\]}{
my $x = $1;
if ($1 !~ /^\w+:/) {
if ($1 =~ /\@/) {
"[mailto:$x|$x]";
} else {
"[http://$x]"
}
} else {
"[$x]"
}
}gex;
# don't let people put in html
$_[2] = HTML::Entities::encode($_[2]);
}
my $body = "Mail me at [bugs\@microsoft.com].";
my $url;
my $name;
sanitize("", $url, $body);
print $body, "\n";
Running this script will yield this error:
Modification of a read-only value attempted at
perl-alias.pl line 9.
To prevent this error, how would I check to see if $_[0] is a scalar that I can write to? Thanks in advance. :)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.