in reply to (Ovid - Dang, I love this place -- and one more question :)
in thread Typeglobs and Symbol tables

Ah, better. This:

params { my ( $self, $cgi ) = @_; my %formdata; $cgi = CGI::self_or_default if ( ! defined $cgi or ! ref $cgi ) an +d $cgi; if ( defined $cgi and $cgi ) {
deserves a bit of "improvement". I'll go through step-wise as I hope that will be more enlightening.

I was not able to understand this:     $cgi = CGI::self_or_default if ( ! defined $cgi or ! ref $cgi ) and $cgi; so I did some "mechanical" translation which made it easy for me to understand (this may have more to do with the strange ways in which my mind works, though):     $cgi = CGI::self_or_default   if  $cgi  and  ! ( defined $cgi and ref $cgi ); So we don't want to override $cgi if it is false and we don't want to override $cgi if it is a good reference. But, my testing shows that ref $cgi doesn't produce a warning even if $cgi is undef so we can shorten this to:     $cgi = CGI::self_or_default   if  $cgi  and  ! ref $cgi;

Similarly, $cgi in a "Boolean context" doesn't elicit a warning in the face of undef so:     if ( defined $cgi and $cgi ) { can become:     if (  $cgi  ) {

This brings us to:

params { my ( $self, $cgi ) = @_; my %formdata; $cgi = CGI::self_or_default if $cgi and ! ref $cgi; if ( $cgi ) {
But I'd factor out the if($cgi):
if ( $cgi ) { $cgi = CGI::self_or_default if ! ref $cgi;
Finally you can swap if ! with just unless, but I find that mostly a matter of taste.

Well, anyway, that "works" better for me. (:

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
(Ovid) Re: (tye)Re: (Ovid - Dang, I love this place :-)
by Ovid (Cardinal) on Jun 02, 2001 at 00:19 UTC

    Much nicer. One little issue, though:

    # This: $cgi = CGI::self_or_default if $cgi and ! ref $cgi; # is not equivalent to this: $cgi = CGI::self_or_default if ! ref $cgi;

    The reason this is the case is how it's called:

    print $foo->list_params( 1 );

    If someone supplies a false value, it should return "0". With the second version of your assignment to $cgi, it will always return the params, regardless of whether or not a false value has been passed. That might seem irrelevant, but this is a reduced test case of the actual code. Here's how this will be used in real life:

    $write->table( -params => 1, -condition => "$x % 7 == 3", -active => 1, -caller => 0, SomeVar => $some_var, DATA => \%some_hash );

    Since this is part of a debugging module, the programmer may want to turn off the param display once the params are verified, or turn them back on again if there is a problem. Rather than going through and deleting or re-entering the '-param' key, the programmer can just toggle the value at will. (I know we discussed this with /msgs, but this is for the benefit of those not privy to the conversation).

    Cheers,
    Ovid

    Update: Hmm... I seem to be posting a lot of updates lately :(

    Seems I missed a little if which drastically changes the meaning of tye's code:

    if ( $cgi ) { $cgi = CGI::self_or_default if ! ref $cgi;

    sigh

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.