iamrobj has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have made my self a very simple bookmarks page so that I can manage my own links.

When I print each link, there is a link printed under it with a link to a script that will delete the link from a text file, except if the link contains certain characters ( ")" "%" "&" blah blah blah), the link is not deleted...

How can I first tell the script to ignore ALL characters and just delete the line?

open (FILE, "links.txt") || Error ('open', 'file'); flock (FILE, 2) || Error ('flock', 'file'); @list=<FILE>; close(FILE); foreach $list(@list) { $count++; if ($list =~ /$link/i) { $count--; splice(@list, $count, 1); open (FILE, ">links.txt") || Error ('open', 'file'); flock (FILE, 2) || Error ('flock', 'file'); print FILE @list; close(FILE); } }

Replies are listed 'Best First'.
Re: Ignore ALL characters and delete?
by Wonko the sane (Curate) on May 13, 2003 at 20:14 UTC
    The link is not getting deleted because the pattern is never found in the file.
    This is because of the meta characters in the link. To make sure that any special meta characters
    are escaped in your regex you need the \Q an \E operators.

    Try using this as your regex, then the you should be able to find the link.
    if ($list =~ /\Q$link\E/i)

    Wonko

      Worked perfect, thanks Wonko!

      "eq" did not... why is that?

        The /i modifier makes the pattern match case-insensitive. To get the same effect with eq, you can do something like: if (uc($list) eq uc($link)) { Substitute lc for uc if you prefer. Unicode is left as an exercise for the reader.

Re: Ignore ALL characters and delete?
by dws (Chancellor) on May 13, 2003 at 20:17 UTC
    Assuming you're using the URL as an argument to a CGI, consider an alternate encoding of the link, such as URL-encoding. Then, on the receiving end, simply URL-decode the argument. Then use \Q and \E in the regex to block certain characters from being interpreted as regex meta-characters.

Re: Ignore ALL characters and delete?
by hossman (Prior) on May 13, 2003 at 21:11 UTC
    if ($list =~ /$link/i) {
    why is this a regex, why not just use "eq" ?