Creating a CGI Query String from a hash using Perl

| | Comments (0) | TrackBacks (0)

Imagine the scenario: you have a hash of arguments you want to send as a CGI query string to somewhere on the web. Or you might, for instance, need to create an unsubscribe URL to include in your newsletter emails so that people can click a link to stop getting your newsletter.

Here's a one line solution:

my %args = (
             id     => 102546,
             key    => 'kljdlasjd3289290mkdlmca',
             list   => 12,
             action => 'unsubcribe', 
           );
		
my $query_string = join '&', map { "$_=$args{$_}" } keys %args;
my $url = "http://example.com/newsletter?$query_string";

That should create your query string out of all the elements of your hash.

0 TrackBacks

Listed below are links to blogs that reference this entry: Creating a CGI Query String from a hash using Perl.

TrackBack URL for this entry: http://www.robbiebow.co.uk/mt/mt-tb.cgi/65

Leave a comment

About this Entry

This page contains a single entry by Robbie Bow published on July 21, 2008 3:56 PM.

Countries Lists was the previous entry in this blog.

Property Rights is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 4.21-en