Re: [Catalyst] Re: retrieving multiple values from forms

2007-12-20 Thread Сергей Мартынов
   i dare say that's not what i'd suggested.  i wrote:
  
   my @titles = ref $c-request-params-{title}
  ? @{ $c-request-params-{title} } : ($c-request-params-{title} || 
   '');
  
   the @{ ... } bit  was not extraneous.
  
   (on the other hand, if there's a more idiomatic way of doing this i'd
   love to hear about it.)
 
  I use perlish way to get rid of excessive method calls and make the
  statement a bit more compact:
 
  my @list = map { ref $_ ? @$_ : ($_ || '') } $c-request-params-{title}

 That'll break if the submitted value is zero 0

Well, okay, I just copied from the code above :) I usually write
simply map { ref $_ ? @$_ : $_ } - passing undef values as they are.
The other nice practice is

my @list = map { ref $_ ? @$_ : defined $_ ? $_ : () }
$c-request-params-{title}

which gives us an expected empty list when no value was passed.

___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[EMAIL PROTECTED]/
Dev site: http://dev.catalyst.perl.org/


Re: [Catalyst] Re: retrieving multiple values from forms

2007-12-19 Thread Сергей Мартынов
 i dare say that's not what i'd suggested.  i wrote:

 my @titles = ref $c-request-params-{title}
? @{ $c-request-params-{title} } : ($c-request-params-{title} || '');

 the @{ ... } bit  was not extraneous.

 (on the other hand, if there's a more idiomatic way of doing this i'd
 love to hear about it.)

I use perlish way to get rid of excessive method calls and make the
statement a bit more compact:

my @list = map { ref $_ ? @$_ : ($_ || '') } $c-request-params-{title}

___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[EMAIL PROTECTED]/
Dev site: http://dev.catalyst.perl.org/


Re: [Catalyst] Re: retrieving multiple values from forms

2007-12-19 Thread Carl Franks
On 20/12/2007, Сергей Мартынов [EMAIL PROTECTED] wrote:
  i dare say that's not what i'd suggested.  i wrote:
 
  my @titles = ref $c-request-params-{title}
 ? @{ $c-request-params-{title} } : ($c-request-params-{title} || 
  '');
 
  the @{ ... } bit  was not extraneous.
 
  (on the other hand, if there's a more idiomatic way of doing this i'd
  love to hear about it.)

 I use perlish way to get rid of excessive method calls and make the
 statement a bit more compact:

 my @list = map { ref $_ ? @$_ : ($_ || '') } $c-request-params-{title}

That'll break if the submitted value is zero 0

Carl
___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[EMAIL PROTECTED]/
Dev site: http://dev.catalyst.perl.org/


Re: [Catalyst] Re: retrieving multiple values from forms

2007-12-18 Thread Peter Karman


On 12/17/2007 05:27 PM, A. Pagaltzis wrote:

 The sane thing is to have *TWO* methods, one that *always*
 returns a scalar, and one that *always* returns a list. (Or
 rather, an arrayref, because if it’s just a list, it is easy to
 improperly treat it like a scalar, whereas if it’s an arrayref
 it’s impossible to forget to unpack the array when you meant to
 do that.)
 

I did something similar in my last $job, where I just had a begin() block in my 
Root.pm
that reset params-{foo} to always return an arrayref.

-- 
Peter Karman  .  [EMAIL PROTECTED]  .  http://peknet.com/


___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[EMAIL PROTECTED]/
Dev site: http://dev.catalyst.perl.org/


Re: [Catalyst] Re: retrieving multiple values from forms

2007-12-18 Thread jagdish eashwar
On Dec 18, 2007 6:56 AM, Andrew Rodland [EMAIL PROTECTED] wrote:

 On Monday 17 December 2007 05:27:46 pm A. Pagaltzis wrote:
  The *sane* thing is neither.
 
  The sane thing is to have *TWO* methods, one that *always*
  returns a scalar, and one that *always* returns a list. (Or
  rather, an arrayref, because if it's just a list, it is easy to
  improperly treat it like a scalar, whereas if it's an arrayref
  it's impossible to forget to unpack the array when you meant to
  do that.)
 
  Then you can say what you mean, you can say it *easily* either
  way, and the result is always completely determined by what the
  code says and not at all by what the data looks like. Sanity.

 Yeah, I can agree with this. I realized the same thing myself... after
 posting
 my original message of course. Teach me to wait a bit longer before
 hitting
 send in the future. Anyway yeah, what I really want is param_list
 or param_aref, without any of the actual CGI compat of param -- param
 just comes closer to my goal than params. Not sure what the ideal behavior
 for the only one version would be, but params is at least practical. If
 you
 see ARRAY(0xbadfad) where you expected useful data it's not that hard to
 trace back and see what you did wrong.

 Andrew
 Hi,

 I am unable to keep up with the discussion.

 When I use @role_id = $c-req-param(role_id) as Andrew suggested, I am
 getting an error in Catalyst  saying Can't use string (4) as a HASH ref
 while strict refs in use. 4 is one of the values that the code is
 fetching.

 So I changed the code to read @role_id = $c-req-params(role_id) as
 Kevin suggested and put [EMAIL PROTECTED] in the stash. Now there was no 
 error, but
 I got ARRAY(0x987e5e0) in the template instead of the role_id values.

 So I further changed the code to $role_id = $c-req-params(role_id) and
 put $values in the stash. Now I could access in the template the multiple
 role_ids that the code was picking up. I realised then that I pick up single
 values also in the same way.

 So is the way for picking up multiple values the same as for picking up
 single values?

 Moving on from here, I want to pick up the corresponding role descriptions
 for each role_id from a database table and show role descriptions rather
 than the role_id's in the template.

 When I am picking up only a single role_id, I am able to achieve this by
 using
 my @role_desc = $c-model('myleavedb::role_types')-find($role_id)
 and putting [EMAIL PROTECTED] in the stash.

 I am unable to figure out how the coding is to be done when I retrieve
 multiple role_id's.
 I tried looping over the multiple role_ids in the arrayref ($role_id), but
 couldn't get the code right.


Jagdish Eashwar
___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[EMAIL PROTECTED]/
Dev site: http://dev.catalyst.perl.org/


[Catalyst] Re: retrieving multiple values from forms

2007-12-18 Thread kevin montuori
 je == jagdish eashwar [EMAIL PROTECTED] writes:

  So I changed the code to read @role_id =
  $c-req-params(role_id) as Kevin suggested and put [EMAIL PROTECTED] in
  the stash. Now there was no error, but I got ARRAY(0x987e5e0) in
  the template instead of the role_id values.

i dare say that's not what i'd suggested.  i wrote:

my @titles = ref $c-request-params-{title} 
   ? @{ $c-request-params-{title} } : ($c-request-params-{title} || ''); 

the @{ ... } bit  was not extraneous.  

(on the other hand, if there's a more idiomatic way of doing this i'd
love to hear about it.)  

but this is not a catalyst issue, 'perldoc perlref' for more on
references.


cheers.
k.

-- 
kevin montuori

___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[EMAIL PROTECTED]/
Dev site: http://dev.catalyst.perl.org/


Re: [Catalyst] Re: retrieving multiple values from forms

2007-12-18 Thread Matt S Trout
On Mon, Dec 17, 2007 at 07:26:53PM -0600, Andrew Rodland wrote:
 On Monday 17 December 2007 05:27:46 pm A. Pagaltzis wrote:
  The *sane* thing is neither.
 
  The sane thing is to have *TWO* methods, one that *always*
  returns a scalar, and one that *always* returns a list. (Or
  rather, an arrayref, because if it’s just a list, it is easy to
  improperly treat it like a scalar, whereas if it’s an arrayref
  it’s impossible to forget to unpack the array when you meant to
  do that.)
 
  Then you can say what you mean, you can say it *easily* either
  way, and the result is always completely determined by what the
  code says and not at all by what the data looks like. Sanity.
 
 Yeah, I can agree with this. I realized the same thing myself... after 
 posting 
 my original message of course. Teach me to wait a bit longer before hitting 
 send in the future. Anyway yeah, what I really want is param_list 
 or param_aref, without any of the actual CGI compat of param -- param 
 just comes closer to my goal than params. Not sure what the ideal behavior 
 for the only one version would be, but params is at least practical. If you 
 see ARRAY(0xbadfad) where you expected useful data it's not that hard to 
 trace back and see what you did wrong.

Exactly my point. Except you should ideally use query_params and body_params
explicitly in most cases; it's fairly rare that you don't know which is which.

I'd love to see a patch for say 'query_value', 'query_array', plus body_* and
any_param_* methods (names made up on the spot and neither final nor necessarily
even any good :)

-- 
  Matt S Trout   Need help with your Catalyst or DBIx::Class project?
   Technical Directorhttp://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment platform?
http://chainsawblues.vox.com/http://www.shadowcat.co.uk/servers/

___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[EMAIL PROTECTED]/
Dev site: http://dev.catalyst.perl.org/


[Catalyst] Re: retrieving multiple values from forms

2007-12-17 Thread A. Pagaltzis
* Andrew Rodland [EMAIL PROTECTED] [2007-12-17 22:40]:
 On Monday 17 December 2007 03:01:53 pm Matt S Trout wrote:
  On Sat, Dec 15, 2007 at 01:10:52PM -0600, Andrew Rodland wrote:
   See the perldoc for Catalyst::Request -- the 'param' method
   comes in handy here.
  
   @values = $c-req-param('whatever');
 
  No it doesn't.
 
  That method is there for CGI.pm compatibility. Don't use it
  in new code.
 
 It's also the only one that has a sensible semantic. Might be
 an unblessed scalar or an arrayref is silly for something
 that's naturally a _list_. If param wasn't there I would have
 to write a method that did the same thing, and use that.

None of these interfaces is sane. The problem with CGI.pm’s
interface is this:

Foo-new(
bar = $q-param( 'bar' ),
baz = $q-param( 'baz' ),
);

Now if someone sends you this query string:

?bar=1;baz=2;baz=is_admin;baz=1

the call evaluates to


Foo-new(
bar = 1,
baz = 2, 'is_admin', 1,
);

Woops.

So CGI.pm forces you to pepper your code with `scalar`s to make
it safe:

Foo-new(
bar = scalar $q-param( 'bar' ),
baz = scalar $q-param( 'baz' ),
);

Catalyst’s interface avoids that. However, Catalyst’s way of
“either you get an arrayref or a scalar” isn’t sane either since
it forces you to pepper your code just as much if not more, if
you want it to react predictably regardless of how many arguments
were passed.

The *sane* thing is neither.

The sane thing is to have *TWO* methods, one that *always*
returns a scalar, and one that *always* returns a list. (Or
rather, an arrayref, because if it’s just a list, it is easy to
improperly treat it like a scalar, whereas if it’s an arrayref
it’s impossible to forget to unpack the array when you meant to
do that.)

Then you can say what you mean, you can say it *easily* either
way, and the result is always completely determined by what the
code says and not at all by what the data looks like. Sanity.

-- 
*AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(,$\/, )[defined wantarray]/e;$1}
Just-another-Perl-hack;
#Aristotle Pagaltzis // http://plasmasturm.org/

___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[EMAIL PROTECTED]/
Dev site: http://dev.catalyst.perl.org/


Re: [Catalyst] Re: retrieving multiple values from forms

2007-12-17 Thread Andrew Rodland
On Monday 17 December 2007 05:27:46 pm A. Pagaltzis wrote:
 The *sane* thing is neither.

 The sane thing is to have *TWO* methods, one that *always*
 returns a scalar, and one that *always* returns a list. (Or
 rather, an arrayref, because if it’s just a list, it is easy to
 improperly treat it like a scalar, whereas if it’s an arrayref
 it’s impossible to forget to unpack the array when you meant to
 do that.)

 Then you can say what you mean, you can say it *easily* either
 way, and the result is always completely determined by what the
 code says and not at all by what the data looks like. Sanity.

Yeah, I can agree with this. I realized the same thing myself... after posting 
my original message of course. Teach me to wait a bit longer before hitting 
send in the future. Anyway yeah, what I really want is param_list 
or param_aref, without any of the actual CGI compat of param -- param 
just comes closer to my goal than params. Not sure what the ideal behavior 
for the only one version would be, but params is at least practical. If you 
see ARRAY(0xbadfad) where you expected useful data it's not that hard to 
trace back and see what you did wrong.

Andrew



___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[EMAIL PROTECTED]/
Dev site: http://dev.catalyst.perl.org/