You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Dermot Paikkos <de...@sciencephoto.com> on 2004/07/30 17:58:04 UTC
Accessing form mutiples
Hi modperls,
Apache/1.3.26 (Unix) mod_perl/1.27
I have started getting odd results from requests that come from forms
with multiple options. If I pass the following uri all is well:
http://server.com/junk?one=1&two=2&three=3
But this doesn't
http://server.com/junk?no=1&no=2&no=3
Well to be fair it does work if I put the list into an array but if I
use a hash reference it doesn't and I can only access the first item.
What am I doing wrong?? Sorry if this is a bit OT but I am not sure
if this is a modperl issue or a perl reference knowledge void.
Thanx.
Dp.
========== JUNK.PM ===============
package SPL::junk;
use Apache::Constants qw(:common REDIRECT);
use Apache::Request;
use Apache::File;
use strict;
use warnings;
use diagnostics;
use Carp;
$| =1;
sub handler{
my $r = Apache::Request->new(shift);
# my $hash_ref;
my @users;
foreach my $param ($r->param) {
print STDERR "\$r->$param = ".scalar($r->param($param))."\n";
# $hash_ref->{$r->param($param)} = 0;
push(@users,$r->param($param));
}
# foreach my $ref (keys %{$hash_ref}) {
# print STDERR "$ref -> $hash_ref->{$ref}\n";
# }
foreach my $u (@users) {
print STDERR "user = $u\n";
}
return OK;
} # End of handler
1;
~~
Dermot Paikkos * dermot@sciencephoto.com
Network Administrator @ Science Photo Library
Phone: 0207 432 1100 * Fax: 0207 286 8668
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html
Re: Accessing form mutiples
Posted by Dermot Paikkos <de...@sciencephoto.com>.
Hi,
It's late and I shouldn't even be in the office but thanx for the
replies. The point that you can't have hash keys of the same name
makes a huge amount of sense and is something is mentioned about it
in the cookbook.
I'll try the slice on Monday, as two replies are stating that as the
solution. I am confident it will get me through.
Thanx.
Dp.
On 30 Jul 2004 at 13:51, Jeremy Brooks wrote:
> I'm a modperl newbie but a pretty good Perl programmer. My first
> thought is that you can't have multiple hash keys with the same name.
> That's why you can only see one key called 'on'. Maybe the 'on' value
> that you are seeing is an array or array reference? I hope I'm not way
> off base here but this would be the first thing I'd verify.
>
> HTH,
> Jeremy
>
> On Fri, 2004-07-30 at 13:01, Thomas Schindl wrote:
> > [...]
> >
> > As already stated by Joe you have two possibilities:
> >
> > 1. Use Hashslice-Sytanx (very short hand)
> > my %hash = ();
> >
> > foreach( $r->param() ) {
> > @hash{$r->param($_)} = ( 1 );
> > }
> >
> > 2. For a more verbose version you should use a inner foreach:
> >
> > my %hash = ();
> >
> > foreach( $r->param() ) {
> > foreach( $r->param($_) ) {
> > $hash{$_} = 1;
> > }
> > }
> >
> > Why are you using a hashref? do you really need it?
> >
> > Tom
> >
> > --
> > NEU: WLAN-Router fr 0,- EUR* - auch fr DSL-Wechsler!
> > GMX DSL = supergnstig & kabellos http://www.gmx.net/de/go/dsl
> >
>
>
> --
> Report problems: http://perl.apache.org/bugs/
> Mail list info: http://perl.apache.org/maillist/modperl.html
> List etiquette: http://perl.apache.org/maillist/email-etiquette.html
>
>
~~
Dermot Paikkos * dermot@sciencephoto.com
Network Administrator @ Science Photo Library
Phone: 0207 432 1100 * Fax: 0207 286 8668
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html
Re: Accessing form mutiples
Posted by Jeremy Brooks <jb...@austin.rr.com>.
I'm a modperl newbie but a pretty good Perl programmer. My first
thought is that you can't have multiple hash keys with the same name.
That's why you can only see one key called 'on'. Maybe the 'on' value
that you are seeing is an array or array reference?
I hope I'm not way off base here but this would be the first thing I'd
verify.
HTH,
Jeremy
On Fri, 2004-07-30 at 13:01, Thomas Schindl wrote:
> [...]
>
> As already stated by Joe you have two possibilities:
>
> 1. Use Hashslice-Sytanx (very short hand)
> my %hash = ();
>
> foreach( $r->param() ) {
> @hash{$r->param($_)} = ( 1 );
> }
>
> 2. For a more verbose version you should use a inner foreach:
>
> my %hash = ();
>
> foreach( $r->param() ) {
> foreach( $r->param($_) ) {
> $hash{$_} = 1;
> }
> }
>
> Why are you using a hashref? do you really need it?
>
> Tom
>
> --
> NEU: WLAN-Router fr 0,- EUR* - auch fr DSL-Wechsler!
> GMX DSL = supergnstig & kabellos http://www.gmx.net/de/go/dsl
>
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html
Re: Accessing form mutiples
Posted by Thomas Schindl <to...@gmx.at>.
[...]
As already stated by Joe you have two possibilities:
1. Use Hashslice-Sytanx (very short hand)
my %hash = ();
foreach( $r->param() ) {
@hash{$r->param($_)} = ( 1 );
}
2. For a more verbose version you should use a inner foreach:
my %hash = ();
foreach( $r->param() ) {
foreach( $r->param($_) ) {
$hash{$_} = 1;
}
}
Why are you using a hashref? do you really need it?
Tom
--
NEU: WLAN-Router f�r 0,- EUR* - auch f�r DSL-Wechsler!
GMX DSL = superg�nstig & kabellos http://www.gmx.net/de/go/dsl
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html
Re: Accessing form mutiples
Posted by Dermot Paikkos <de...@sciencephoto.com>.
Does that explain, if I use the version of junk.pm below, with this
uri:
http://dev.sciencephoto.co.uk/junk?no=1&no=2&no=3
I get:
$r->no = 1
user = 1
user = 2
user = 3
and
http://dev.sciencephoto.co.uk/junk?one=1&two=2&three=3
gives:
$r->one = 1
$r->two = 2
$r->three = 3
user = 1
user = 2
user = 3
Even as a list, you only seem to be given the form name once if all
the elements are the same. But your right what I want is the form
values as keys in my hash.
But what happens when I uncomment the $hash_ref lines is this:
$r->no = 1
1 -> 0
I a bit surprised as the cookbook says that using $r = shift "will
remove any likes named pairs". That's why I am using Apache::Request.
Thanx.
Dp.
On 30 Jul 2004 at 12:18, Michael wrote:
> Dermot Paikkos wrote:
> > Hi modperls,
> >
> > Apache/1.3.26 (Unix) mod_perl/1.27
> >
> > I have started getting odd results from requests that come from
> > forms with multiple options. If I pass the following uri all is
> > well: http://server.com/junk?one=1&two=2&three=3
> >
> > But this doesn't
> > http://server.com/junk?no=1&no=2&no=3
> >
> > Well to be fair it does work if I put the list into an array but if
> > I use a hash reference it doesn't and I can only access the first
> > item.
>
> Apache::Request::param will only return a list if it is called in list
> context. This means you may have to force it to return a list using ()
> like this ($r->param('my_param')).
>
> But it looks like you are trying to put the values of your params as
> keys to a hash. You can't have a list be a key to a hash.
>
> HTH
>
> Michael Peters
> Developer
> Plus Three
>
> >
> > What am I doing wrong?? Sorry if this is a bit OT but I am not sure
> > if this is a modperl issue or a perl reference knowledge void.
> > Thanx. Dp.
> >
> >
> > ========== JUNK.PM ===============
> > package SPL::junk;
> >
> > use Apache::Constants qw(:common REDIRECT);
> > use Apache::Request;
> > use Apache::File;
> > use strict;
> > use warnings;
> > use diagnostics;
> > use Carp;
> >
> > $| =1;
> >
> > sub handler{
> >
> > my $r = Apache::Request->new(shift);
> > # my $hash_ref;
> > my @users;
> >
> > foreach my $param ($r->param) {
> > print STDERR "\$r->$param =
> > ".scalar($r->param($param))."\n";
> > # $hash_ref->{$r->param($param)} = 0;
> > push(@users,$r->param($param));
> > }
> >
> >
> > # foreach my $ref (keys %{$hash_ref}) {
> > # print STDERR "$ref -> $hash_ref->{$ref}\n";
> > # }
> >
> > foreach my $u (@users) {
> > print STDERR "user = $u\n";
> > }
> >
> > return OK;
> > } # End of handler
> > 1;
> >
> >
> >
> >
> > ~~
> > Dermot Paikkos * dermot@sciencephoto.com
> > Network Administrator @ Science Photo Library
> > Phone: 0207 432 1100 * Fax: 0207 286 8668
> >
> >
>
> --
> Report problems: http://perl.apache.org/bugs/
> Mail list info: http://perl.apache.org/maillist/modperl.html
> List etiquette: http://perl.apache.org/maillist/email-etiquette.html
>
>
~~
Dermot Paikkos * dermot@sciencephoto.com
Network Administrator @ Science Photo Library
Phone: 0207 432 1100 * Fax: 0207 286 8668
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html
Re: Accessing form mutiples
Posted by Michael <mp...@plusthree.com>.
Dermot Paikkos wrote:
> Hi modperls,
>
> Apache/1.3.26 (Unix) mod_perl/1.27
>
> I have started getting odd results from requests that come from forms
> with multiple options. If I pass the following uri all is well:
> http://server.com/junk?one=1&two=2&three=3
>
> But this doesn't
> http://server.com/junk?no=1&no=2&no=3
>
> Well to be fair it does work if I put the list into an array but if I
> use a hash reference it doesn't and I can only access the first item.
Apache::Request::param will only return a list if it is called in list
context. This means you may have to force it to return a list using ()
like this ($r->param('my_param')).
But it looks like you are trying to put the values of your params as
keys to a hash. You can't have a list be a key to a hash.
HTH
Michael Peters
Developer
Plus Three
>
> What am I doing wrong?? Sorry if this is a bit OT but I am not sure
> if this is a modperl issue or a perl reference knowledge void.
> Thanx.
> Dp.
>
>
> ========== JUNK.PM ===============
> package SPL::junk;
>
> use Apache::Constants qw(:common REDIRECT);
> use Apache::Request;
> use Apache::File;
> use strict;
> use warnings;
> use diagnostics;
> use Carp;
>
> $| =1;
>
> sub handler{
>
> my $r = Apache::Request->new(shift);
> # my $hash_ref;
> my @users;
>
> foreach my $param ($r->param) {
> print STDERR "\$r->$param = ".scalar($r->param($param))."\n";
> # $hash_ref->{$r->param($param)} = 0;
> push(@users,$r->param($param));
> }
>
>
> # foreach my $ref (keys %{$hash_ref}) {
> # print STDERR "$ref -> $hash_ref->{$ref}\n";
> # }
>
> foreach my $u (@users) {
> print STDERR "user = $u\n";
> }
>
> return OK;
> } # End of handler
> 1;
>
>
>
>
> ~~
> Dermot Paikkos * dermot@sciencephoto.com
> Network Administrator @ Science Photo Library
> Phone: 0207 432 1100 * Fax: 0207 286 8668
>
>
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html
Re: Accessing form mutiples
Posted by Joe Schaefer <jo...@sunstarsys.com>.
"Dermot Paikkos" <de...@sciencephoto.com> writes:
[...]
> foreach my $param ($r->param) {
> print STDERR "\$r->$param = ".scalar($r->param($param))."\n";
> # $hash_ref->{$r->param($param)} = 0;
Have you tried using a slice there?
@{$hash_ref}{$r->param($param)} = (); # values are now undef, not 0
You need the slice syntax to put $r->param($param) in list context.
--
Joe Schaefer
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html