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

Hi

(This may be more of an XML-related question than a Perl one, but I would have thought that someone around here could help me out). I would like to pass some parameters to a Perl script via clicking on a link in an RSS feed. The XML file for the RSS feed itself is generated by another Perl script. I can pass the parameters as long as I have just one parameter in the link, but when I try to use two, the "&" sign seems to screw things up. I suspect it might be something to do with the XML header.
Here are the relevant lines from the first Perl script that generates the XML for the RSS feed:

...(snip) $rss_out_file = "/home/foo/bar/public_html/RSS_feed.xml"; (A load of stuff to query our database and write the rows to an array of arrays) open RSSFILE, ">$rss_out_file" or die "Couldn't open RSS feed file: $! +"; print RSSFILE '<?xml version="1.0" encoding="UTF-8" ?> <rss version="2.0"> <channel> <title>RSS feed</title> <link>http://(server IP address)/</link> <description>blah blah blah</description>'; foreach $n(@RSStable) { print RSSFILE '<item><title>'.$$n[0].'</title><link>'.$$n[1].'</link>< +description>'.$$n[2].'</description></item>'; } print RSSFILE '</channel></rss>'; close RSSFILE;
Now when the link is something like "http://(server IP address)/other_perl_script.pl?param1="foo"", there is no problem when I click on the link on the feed. However, when the link is "http://(server IP address)/other_perl_script.pl?param1="foo¶m2="bar"", the other perl script does not pick up the second parameter. I know it is not a problem with the other perl script, because I can manually type the URL into the browser and both parameters will be passed OK.

It must therefore be the way the XML is written (I suspect the header), but since I have little experience with XML, I'm not sure what I should be doing.

Can anyone out there help me?

TIA, Campbell

Replies are listed 'Best First'.
Re: Passing parameters to a Perl script via an RSS feed
by Corion (Patriarch) on Feb 08, 2008 at 11:54 UTC

    The ampersand (&) is a special character for XML, so you likely need to encode it into the appropriate XML entity:

    $link =~ s/&/&amp;/g;

    But somewhere down the road, you will encounter other problematic characters in URLs, like <, > and maybe others. So you could use XML::RSS to generate your RSS, or use HTML::Entities to encode all your URLs.

      Thanks, people, that worked very nicely!
Re: Passing parameters to a Perl script via an RSS feed
by moritz (Cardinal) on Feb 08, 2008 at 11:58 UTC
    Corions answer is correct, you should encode entities.

    But for URLs I recommend using ; instead of &. This works for perl scripts that use CGI and similar modules, and it is what is used on perlmonks.org.