Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Pass arguments in URL

by kalyanrajsista (Scribe)
on Aug 23, 2010 at 14:13 UTC ( #856714=perlquestion: print w/ replies, xml ) Need Help??
kalyanrajsista has asked for the wisdom of the Perl Monks concerning the following question:

Monks

I'm trying to prepare URL as below with lots of arguments. Is there any way that I can pass only one argument like using FreezeThaw Module by freezing all the parameters in form of hash and pass that reference in the URL. But it is printing some unnecessary characters in the URL. FYI, I'm using CGI Module

/cgi-bin/details?period=$period&vid=$vid&person=$person

Comment on Pass arguments in URL
Download Code
Re: Pass arguments in URL
by Anonymous Monk on Aug 23, 2010 at 14:16 UTC
    What?
Re: Pass arguments in URL
by moritz (Cardinal) on Aug 23, 2010 at 14:22 UTC

    Please read How (Not) To Ask A Question, and then ask your question again, this time providing enough information for us to actually answer it.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Pass arguments in URL
by BrowserUk (Pope) on Aug 23, 2010 at 14:39 UTC

    Storable freeze produces a binary string that may contain a whole bunch of characters that would be incompatible with passing as part of a url. You might try url encoding the results of freeze, but I wouldn't like to rely upon that.

    It would be better to hexify the frozen data before adding it as a part of your url.

    Given oneScript like this:

    #! perl -slw use strict; use Storable qw[ freeze ]; my %params = ( period => 30, vid => 'xyz1234', person => 'homer', ); my $encoded = unpack 'H*', freeze \%params; system qq[otherScript.pl ?arg=$encoded];

    Calling another script like this:

    #! perl -slw use strict; use Data::Dump qw[ pp ]; use Storable qw[ thaw ]; my( $name, $payload ) = split '=', $ARGV[ 0 ]; my $params = thaw pack 'H*', $payload; pp $params;

    The result is:

    c:\test>oneScript { period => 30, person => "homer", vid => "xyz1234" }
Re: Pass arguments in URL
by marto (Bishop) on Aug 23, 2010 at 14:46 UTC

    Like most others I'm confused by what you are trying to ask for. Are you looking for something like CGI::Session?

Re: Pass arguments in URL
by ikegami (Pope) on Aug 23, 2010 at 14:50 UTC
    What freeze does is called serialising. You could serialise using Storable, but you didn't indicate any reason to do that.
    use URI qw( ); sub url_freeze { my ($base_url, @args) = @_; return URI->new($base_url, 'http')->query_form(@args); } my $url = url_freeze('/cgi-bin/details', %args);

    I may have the exact usage a bit off.

      Usage is more like this:
      use URI; use strict; use warnings; my $hostname = 'localhost'; my $path = '/cgi-bin/details'; my $period = 4; my $vid = 11; my $person = 'abcdef'; my @args = ( period => $period, vid => $vid, person => $person ); my $url = URI->new("http://$hostname/"); $url->path($path); $url->query_form( \@args ); print $url->canonical;
      The URI accessors do not support chaining.

      Update: I misread the parent as using chained accessors, which it wasn't, this is just a little more verbose than that
        You changed
        ->query_form(@args);
        to
        ->query_form(\@args);
        but both are ok. So that leaves the bad return value. Fixed:
        use URI qw( ); sub url_freeze { my $url = URI->new(shift, 'http'); $url->query_form(@_); return $url } my $url = url_freeze('/cgi-bin/details', %args);

        It won't freeze recursively, of course.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://856714]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2014-12-26 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (174 votes), past polls