Mailing List Archive

Dynamic URI query strings
Heya,

I coming across a problem and I'm hoping that someone has solved it
in a slightly cleaner way than I have.

We have Embperl pages which include Embperl "components". These
components need to link to the same pages but with additional parameters
(intended for the component) passed in the URL. [The components
are done with Execute($name, \%conf)]

ie the page is called with:
/investment/detail.epl?code=FOO&display=WOTSIT&it=BAR
but the component wants to link to:
/investment/detail.epl?code=007&display=WOTSIT&it=BAR&new=YES

The way I have done it is for the page to pass the component
a "prefix" URI, which the component then manipulates. I haven't
found a Perl module which'll do what I want, so I've had to come
up with the following which uses the CGI and URI modules:

my $uriprefix = '/investment/detail.epl?code=FOO&display=WOTSIT&it=BAR';
my $uri = URI->new($uriprefix);
$uriprefix = $uri->query();
my $q = CGI->new($uriprefix);
$q->param('code', '007');
$q->param('new', 'YES');
$uriprefix = $uri->path . '?' . $q->query_string;
print $uriprefix, "\n";

Have I missed something? Is there a module on CPAN which makes this
manipulation easier? Should there be?

Cheers, Leon
--
Leon Brocard | perl "programmer" | leon.brocard@iii.co.uk
RE: Dynamic URI query strings [ In reply to ]
>
> my $uriprefix = '/investment/detail.epl?code=FOO&display=WOTSIT&it=BAR';
> my $uri = URI->new($uriprefix);
> $uriprefix = $uri->query();
> my $q = CGI->new($uriprefix);
> $q->param('code', '007');
> $q->param('new', 'YES');
> $uriprefix = $uri->path . '?' . $q->query_string;
> print $uriprefix, "\n";
>

Why don't you simply append to the string? Am I missing something?


my $uriprefix = '/investment/detail.epl?code=FOO&display=WOTSIT&it=BAR';
$uriprefix .= '&code=007&new=YES';
print $uriprefix, "\n";

Gerald
RE: Dynamic URI query strings [ In reply to ]
Gerald wrote:

> Why don't you simply append to the string? Am I missing something?

Because that would end up with something like:
/investment/detail.epl?code=FOO&display=WOTSIT&it=BAR&code=007&new=YES
I don't want 'code' twice!

HTH, Leon
--
Leon Brocard | perl "programmer" | leon.brocard@iii.co.uk
RE: Dynamic URI query strings [ In reply to ]
>
> > Why don't you simply append to the string? Am I missing something?
>
> Because that would end up with something like:
> /investment/detail.epl?code=FOO&display=WOTSIT&it=BAR&code=007&new=YES
> I don't want 'code' twice!
>
Aah, I see...


Then I would use a regex and a hash:

my $uriprefix = '/investment/detail.epl?code=FOO&display=WOTSIT&it=BAR';
my @parts = split (/\?/, $uriprefix) ;
my %parms = split (/\&|=/, $parts[1]) ;
$parms{'code'} = '007' ;
$parms{'new'} = 'YES' ;
$uriprefix = parts[0] . join ('&', %parms) ;
print $uriprefix, "\n";

It's not much shorter, but faster anyway

Gerald
RE: Dynamic URI query strings [ In reply to ]
Gerald wrote:

> Then I would use a regex and a hash

Oh. I'm still convinced this could be wrapped up in a module. I'll
see if there's any interest on clpm.

Thanks for your time, Leon
--
Leon Brocard | perl "programmer" | leon.brocard@iii.co.uk