Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Passing variables to another script (theory)

by Anonymous Monk
on Mar 31, 2004 at 04:37 UTC ( [id://341198]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Say I have two files. file1.cgi and admin.cgi . Admin.cgi has a whole bunch, over 20 and will probably become greater than 50 checkboxes that are grouped together. The admin file acts as a configuration/backbone for the other script, something that's common with admin files.

The question is, what would be the easiest way to transport the checkbox values into the other script so file1.cgi can use it? What I need to do is store which boxes are checked and pass this somehow to the file1.cgi. The admin can change these values at any time; some times s/he may have one checkbox marked, other times maybe half or all of them.

The point of this is I need to check against all these values in file.cgi. The only idea I came up with is using DB_File or SDBM and storing them in a joined string then inside file1.cgi I could split them back into variables. This seems like a tedious task and there must be an easier way.

To make this one bit harder, how can I keep the check boxes checked for the next time they log back in? I need the choices they selected to remain the same the next time they access the admin panel, so the method used to pass this to the other script must also store these in a manner I can do this.

If you need a better description, let me know and I'll try to revise it. Thank you all for your time.

  • Comment on Passing variables to another script (theory)

Replies are listed 'Best First'.
Re: Passing variables to another script (theory)
by DamnDirtyApe (Curate) on Mar 31, 2004 at 05:47 UTC

    It sounds to me like all you need is a mechanism to write to a config file from your admin.cgi script, and read from the config file in admin.cgi and any number of other CGI scripts.

    Perl will do this well in any number of ways; you might consider:

    • Config::IniFiles, which will read and write config files in a simple INI format,
    • XML::Simple, which will read and write XML,
    • Your own hand-rolled solution. Decide on a simple format you can parse with split or a regexp, and write the code that will write your checkbox values in this format.

    To keep the checkboxes checked, simply read the config file as discussed above, and generate the CGI form elements accordingly.

    HTH


    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
      Either that or just get over the hump and use a database.
      ------------ :Wq Not an editor command: Wq
Re: Passing variables to another script (theory)
by matija (Priest) on Mar 31, 2004 at 05:53 UTC
    To answer your last question first: To be able to restore the values of chekboxes, you could either re-write the page with placekeepers fpr HTML::Template
    <input type=checkbox name=foo <TMPL_IF name="foo_checked">CHECKED </TM +PL_IF> >
    Or, for ease of programming, you could use HTML::FillInForm which parses the form on the fly, and fills in the values you pass it.

    As for how to pass values from one CGI to another, I suggest that your data structure should be as close as possible to the parameter passing as implemented by CGI - after all, you're already prepared to handle that.

    If you use a DB_File, then you make the name of each param the key, and it's value the value. And yes, you'll have to run a join on those params that return an array. Be carefull to use a separator unlikely to appear in the name of the param (or checkbox). 0x00 is a good value for such uses.

    Of course, since you gave the file the .cgi extension, you could just call it with a complete CGI style request.

Re: Passing variables to another script (theory)
by Hofmator (Curate) on Mar 31, 2004 at 13:15 UTC
    Do it like DamnDirtyApe so nicely explained it. But for persistence I'd recommend Storable.

    Assuming your configuration information is stored in admin.cgi in a hash %config, it's as easy as:

    use Storable; store \%config, 'config.file'; # for reading back $hashref = retrieve('file');
    straight from the docs of Storable.

    -- Hofmator

Re: Passing variables to another script (theory)
by sulfericacid (Deacon) on Mar 31, 2004 at 14:16 UTC
    I support DB_File and SDBM_File a million percent! Might be tougher to use in some instances, but in all that I've ever done..I got it to work with using these two databases.

    Why would you have to join and split variables? Just make a new hash key for each of the items and check for existance in the other script.

    if ($database{"test1"}) { print qq(<input name="test" type="checkbox" id="test" value="test1" + checked> test1 } else { print qq(<input name="test" type="checkbox" id="test" value="test1"> test1 }
    This will save you a lot of time from not having to join and split your variables/form fields. Just set them up as their own hash keys, you can set values to them this way as well. There is no need for the modules the other monks suggested. Not saying they won't work or they might not be better, but this is how I would go about doing this.


    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 23:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found