[prog] web programming with perl

Jacinta Richardson jarich at perltraining.com.au
Mon May 2 20:08:47 EST 2005


Shuying Wang wrote:
> Hi,
> 
> A Perl CGI program I'm extending uses Mason and one of the required
> bits to the <%ARGS> is a hash $changes=>{}. Could anyone tell me how I
> can pass arguments as a hash to it? I know the basic URL should be
> something like http://someserver/scriptx/component/new?key=value but I
> can't find any information on passing data structures in.

G'day Shuying,

I was wondering whether you could give more information.  When you refer to 
<%ARGS> are you referred to the <%args> block, or the %ARGS hash?

The %args block is written like this:

<%args>
    $color
    $size => 20
    @items => ( 1, 2, 'something else' )
    %pairs => ( key1 => 1, key2 => 'value' )
</%args>

and specifies the arguments your component expects as well as some defaults for 
them.  The %args block is optional.

The %ARGS hash is a hash of all arguments given to the component, whether 
expected or not.  These values can be accessed just by looking up the hash 
values.  ie:
     $ARGS{color};
     $ARGS{pairs}{key1};

Typically if you're expecting the arguments to come in from the outside world 
then you'll provide them through a POSTed HTML form.  Ie:

         <form method="POST" action="... ">
         <input type="text" id="color">
         <input type="submit" id="submit" value="Submit">
         </form>

If you supply multiple values for the same key, then they will be put into a 
list on the other side.  Thus:

         <form method="POST" action="... ">
         <input type="text" id="items">
         <input type="text" id="items">
         <input type="text" id="items">
         <input type="submit" id="submit" value="Submit">
         </form>

will populate @items with three values, each value set to that in the 
corresponding text field.

       @ARGS{items};

To provide these values via a GET request (in the URL) you provide each value 
separated by either a & or a ;

       ..items=i1;items=i2;items=i3;color=blue

You can pass arguments to your component in two ways.  Either through HTML or 
when you call the component from within other Mason code.  These are clearly 
explained in the Mason book, so I'll give you a URL rather than repeat it here:

    http://www.masonbook.com/book/chapter-2.mhtml#TOC-ANCHOR-13

Let me know if you need any more help.

All the best,

     Jacinta

-- 
    ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
     `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
     (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
   _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
  (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |




More information about the Programming mailing list