Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Saving CGI state to mySQL

by filmo (Scribe)
on Nov 30, 2001 at 11:55 UTC ( [id://128573]=perlquestion: print w/replies, xml ) Need Help??

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

I know that you can save a CGI state to a filehandle such as:
$q->save(FILEHANDLE);
But I'm interested in saving the state to a mySQL column instead of a flat file. How do I go about doing that. I'm sure it must be possible to open a filehandle as a variable instead of as a file, but I'm not sure how to do that. ( I don't want to write to a file and then have to read it back in order to get it into a variable.)

I've got the SQL down, just not clear on how to get the state into a variable. Thanks in advance.
--
Filmo the Klown

Replies are listed 'Best First'.
(crazyinsomniac) Re: Saving CGI state to mySQL
by crazyinsomniac (Prior) on Nov 30, 2001 at 12:46 UTC
    I can't type much, but here is save from CGI.pm. As you can see, its fairly easy to adopt this to your needs very quickly (me type left hamd only)
    #### Method: save # Write values out to a filehandle in such a way that they can # be reinitialized by the filehandle form of the new() method #### 'save' => <<'END_OF_FUNC', sub save { my($self,$filehandle) = self_or_default(@_); $filehandle = to_filehandle($filehandle); my($param); local($,) = ''; # set print field separator back to a sane value local($\) = ''; # set output line separator to a sane value foreach $param ($self->param) { my($escaped_param) = escape($param); my($value); foreach $value ($self->param($param)) { print $filehandle "$escaped_param=",escape("$value"),"\n"; } } foreach (keys %{$self->{'.fieldnames'}}) { print $filehandle ".cgifields=",escape("$_"),"\n"; } print $filehandle "=\n"; # end of record } END_OF_FUNC
    you can also look into perltie and tutorials to tie a filehandle to a scalar, but I think it'd be easier to adopt the above code.

     
    ___crazyinsomniac_______________________________________
    Disclaimer: Don't blame. It came from inside the void

    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

Re: Saving CGI state to mySQL
by mitd (Curate) on Nov 30, 2001 at 12:56 UTC
    There several approaches you can take and here is one.

    First a quote direct from CGI::save method comment.

    #### Method: save # Write values out to a filehandle in such a way that they can # be reinitialized by the filehandle form of the new() method ####
    Ok with this knowledge in hand we can start.
    1. sub-class CGI (an example of this is CGI::Pretty look at it if you are not sure how to do this)
    2. two choices in your new sub-class, overide CGI::save method or creat a new save_db method either way 90% of the code you need is in original save method.
    3. Must overide CGI::new to handle case when previous state has been save using your new save_db form.
    Hope this helps, good luck

    mitd-Made in the Dark
    'Interactive! Paper tape is interactive!
    If you don't believe me I can show you my paper cut scars!'

Re: Saving CGI state to mySQL
by tachyon (Chancellor) on Nov 30, 2001 at 21:12 UTC

    The easiest way to do it in a few lines is this:

    use CGI; my $q = new CGI; # to save object to a database as a single string my $string = $q->query_string(); save_to_db($string); # to retrieve object just get the string back my $string = get_from_db(); # and use it to initinalise a new CGI object my $reconstituted_q = new CGI($string);

    This works because the query_string() method generates a query string equivalent to your object data and you can ititialise a CGI object fron a query string like $q = new CGI( "foo=bar&baz=spaz" )

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-25 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found