Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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:


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


Replies are listed 'Best First'.
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[ ?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 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
        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.

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 Anonymous Monk on Aug 23, 2010 at 14:16 UTC
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?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://856714]
Approved by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2017-12-17 06:33 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (462 votes). Check out past polls.