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

According to CGI.pm docs, we can save state to any filehandle:

$query->save(FILEHANDLE)

I need to dump the whole lot as BLOB/TEXT to database, but can anyone suggest how I would go about this using DBI ? The following incorrect (!) code probably illustrates my need a bit clearer:

$dbh->do("UPDATE table SET field=?", undef,$query->save());

cheers

David

Replies are listed 'Best First'.
•Re: CGI.pm saving state to database
by merlyn (Sage) on Jun 09, 2003 at 17:23 UTC
    Some of the other answers in this thread talk about "saving the CGI object", but you really don't need to save all of that. If you're looking to just save the key/value pairs, create a quick HoA like this:
    use CGI qw(param); my %ParamSaver = map { $_ => [param($_)] } param();
    then dump %ParamSaver by any means you are familiar with. Once restored, you can load them into the new CGI object with:
    # presume %ParamSaver has been fetched... use CGI qw(param delete_all); delete_all(); # reset all parameters for (keys %ParamSaver) { param($_, @{$ParamSaver{$_}}); }

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: CGI.pm saving state to database
by gellyfish (Monsignor) on Jun 09, 2003 at 16:42 UTC
    save() won't do what you want in this context most probably - but you can create a new CGI object from an existing one so what you probably want to do is rather than use save() you serialize the CGI object (using Data::Dump, Storable, Data::DumpXML or whatever) and then store the serialized data in the database. Then when you want to get it back you can deserialize it into a variable and then pass that to CGI->new(). An example using Storable (omitting the database bit):
    #!/usr/bin/perl -w use strict; use CGI; use Storable qw(freeze thaw); my $cgi1 = CGI->new(); # do some stuff my $save_cgi = freeze($cgi1); # # save $save_cgi to a database # time passes and you get it back into $save_cgi # (this is possibly in another process my $restore_cgi1 = thaw($save_cgi); my $cgi2 = CGI->new($restore_cgi1);
    You should be extremely cautious when deserializing data from a database and ensure that the permissions do not allow people to put random stuff into the table that could be deserialized in such a way that it could potentially be harmful. Hope that helps
    /J\
    
Re: CGI.pm saving state to database
by hardburn (Abbot) on Jun 09, 2003 at 16:30 UTC

    Depends a lot on what you're using CGI.pm for. If you're simply grabbing name/value pairs, you could grab all of them with $cgi->Vars() and dump them into the database that way. Saving cookies shouldn't be too difficult. File uploads are perhaps a bit harder.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: CGI.pm saving state to database
by PodMaster (Abbot) on Jun 10, 2003 at 07:22 UTC
    Here you go
    sub SaveCgi { my( $self, $cgi ) = @_; $cgi = UNIVERSAL::isa($cgi,'CGI') ? $cgi : CGI->new($cgi); my $ret = ""; for my $param ($cgi->param) { my($escaped_param) = $cgi->escape($param); for my $val ($cgi->param($param)) { $ret .= "$escaped_param=".$cgi->escape($val)."&"; } } chop $ret; # simples way to remove last & return $ret; }
    Then when retrieving the value from the database, simply feed it to CGI->new.

    You may also wanna investiage CGI::Session as well as CGIS.

    update: I adopted SaveCgi a long time ago from CGI.pm's save

    #### 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


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
    ** The Third rule of perl club is a statement of fact: pod is sexy.