On Mon, May 29, 2006 at 05:28:53PM -0700, Ovid wrote:
> I also tried to do this:
>   my ($n, $v) = $nv.split('=').map(decode($_));
> That and a number of other variants all failed miserably with errors similar 
to:
>   Can't modify constant item: VStr ...

Many thanks to Joel and ovid, your suggestions are extremely
enlightening. The "Can't modify constant item: VStr..." is showing up
because my "decode" sub is trying to modify it's argument in-place
(not a copy of the argument). Adding the "is copy" phrase allows it to
work as expected. hmm... I like that.

Joel's point about auto-vivification is good, It's not a bug, it's a
feature, dammit. I'm gonna use it. Strangely %q<$n>.push($v); doesn't
work, but %q.{$n}.push($v); does. What's the difference?

#!/usr/bin/pugs
my %q = ();
for %ENV<QUERY_STRING>.split('&') -> $nv {
        my ($n, $v) = $nv.split('=').map: &decode;
        %q.{$n}.push($v); # auto-vivify!!
}

print "content-type: text/html\n\n";
for (%q.keys) { say "$_ => "~%q.{$_}.join(', ')~'<br>' }

sub decode(my $input is copy) {
        $input ~~ s:Perl5:g/\+/ /;
        $input ~~ s:Perl5:g/%(..)/{chr(:16($0))}/;
        return $input;
}

Reply via email to