[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] RE: CGI.pm -- strange argument results and $q->param.



I left the last two "|| undef,"'s off to explain behaviour...  The real code
has it.

Sean
 
-----Original Message-----
From: Jay Hannah [mailto:jhannah@omnihotels.com] 
Sent: Wednesday, March 16, 2005 4:03 PM
To: pbaker@omnihotels.com
Cc: omaha-pm@pm.org
Subject: RE: CGI.pm -- strange argument results and $q->param.


From: Sean Baker [mailto:pbaker@omnihotels.com] 
> Bummer.  If I try to pass an empty $q->param as a %args into 
> the set_personname method, it screws things up.
> 
> Look at the results below of nametitle and surnameprefix when 
> I leave "|| undef" off of nametitle and surnameprefix.
> 
>       $o_Contact->set_personname( 
>         contactid       => $pageid,
>         nameprefix      => $q->param("personname__nameprefix__$pageid") ||
undef,
>         givenname       => $q->param("personname__givenname__$pageid") ||
undef,
>         middlename      => $q->param("personname__middlename__$pageid") ||
undef,
>         surname         => $q->param("personname__surname__$pageid") ||
undef,
>         namesuffix      => $q->param("personname__namesuffix__$pageid") ||
undef,
>         nametitle       => $q->param("personname__nametitle__$pageid"),
>         surnameprefix   =>
$q->param("personname__surnameprefix__$pageid"),
>       );
> 
>   DB<5> x %args
> 0  'givenname'
> 1  'LYNN'
> 2  'middlename'
> 3  undef
> 4  'contactid'
> 5  564
> 6  'namesuffix'
> 7  undef
> 8  'nameprefix'
> 9  'MRS'
> 10  'nametitle'
> 11  'surnameprefix'
> 12  'surname'
> 13  'BLAKE'

Yup. The "problem" is that in array context $q->param('x') when 'x' isn't
defined returns an empty array. If it returned undef, your code would work.
An empty array messes up your hash like so:

my %a = (
   1 => 'one',
   2 => (),
   3 => 'three'
);

  DB<1> x %a
0  'three'
1  undef
2  1
3  'one'
4  2
5  3

I exchanged emails with the author/maintainer a couple years ago. He said
the behavior was documented. (Actually I just checked and I don't see where
it's documented.)

Anyhoo, this is where the weirdness lies:

  DB<1> x $q->param('x')
  empty array

You can fix it the way you did

  DB<3> x $q->param('x') || undef
0  undef

Or by forcing scalar context

  DB<2> x scalar($q->param('x'))
0  undef

Or, as he suggested to me, by pulling each param into a scalar before you
define your hash (doubling the # of lines in your code).

Bummer, huh? 

CGI.pm is still pretty cool though.

j

P.S. You should '|| undef' those last 2 parms too!