Subject: | support for clearing a single param with clear() |
The attached patch adds the functionality and documentation to support clearing a
single parameter by passing a scalar to clear(). It uses the well-test _arrayify() routine
from Data::FormValidator.
It still needs a little test case, though. It took me hours today to figure out why
clear('param') wasn't working.
I still don't fully understand what happened. The 'confess' that's in CGI::Session didn't
appear to be writing anything to STDERR, but a blank web page was being return to my
browser in some cases, despite the presences of 'print' statements anywhere in the
code. Very strange.
So, save some other programmers this pain by making clear() do what they expect it to
do in this case. :)
Thanks. Mark
--- /usr/local/lib/perl5/site_perl/5.8.0/CGI/Session.pm Fri May 2 15:10:48 2003
+++ /usr/home/mark/tmp/Session.pm Tue Jul 15 15:13:05 2003
@@ -630,10 +630,12 @@
=item C<clear()>
+=item C<clear($scalar)>
+
=item C<clear([@list])>
-clears parameters from the session object. If passed an argument as an
-arrayref, clears only those parameters found in the list.
+clears parameters from the session object. If passed an argument as a scalar or
+an arrayref, clears only those parameters found in the list.
=item C<flush()>
@@ -944,21 +946,21 @@
}
-
-
-
# clear() - clears a list of parameters off the session's '_DATA' table
sub clear {
my $self = shift;
$class = ref($self);
+ my $clear_params = shift;
- my @params = $self->param();
- if ( defined $_[0] ) {
- unless ( ref($_[0]) eq 'ARRAY' ) {
- confess "Usage: $class->clear([\@array])";
- }
- @params = @{ $_[0] };
+ my @params;
+ if ( defined $clear_params ) {
+ @params = _arrayify($clear_params);
}
+ # clear everything if there are no arguments
+ else {
+ @params = $self->param();
+ }
+
my $n = 0;
for ( @params ) {
@@ -976,6 +978,25 @@
return $n;
}
+
+# takes string or array ref as input
+# returns array
+sub _arrayify {
+ # if the input is undefined, return an empty list
+ my $val = shift;
+ defined $val or return ();
+
+ if ( ref $val eq 'ARRAY' ) {
+ # if it's a reference, return an array unless it points an empty array.
+ return (length $val->[0]) ? @$val : ();
+ }
+ else {
+ # if it's a string, return an array unless the string is missing or empty.
+ return (length $val) ? ($val) : ();
+ }
+}
+
+
# save_param() - copies a list of third party object parameters