in reply to CGI params

I can't figure out why it is reading 'wtf' from the command line rather than the value "Construction".

Try rewriting   my %params =  map ($_ => param('$_'),param()); as   my %params = map { $_ => param($_) } param();

Replies are listed 'Best First'.
Re: Re: CGI params
by devslashneil (Friar) on Sep 09, 2003 at 07:43 UTC
    Unfortunately that didn't help, thanks for the input though, it's a really confusing error, the only thing i can think of is that it's trying to evaluate param() as a hash?

    Neil Archibald
    - /dev/IT -
      Unfortunately that didn't help, ...

      Strange. I was able to duplicate your problem (by adding   use CGI qw(:standard); to the top of that fragment). The change I offer fixes the problem, at least does when I try it. What, exactly, does your code fragment look like when you try it?

      Mine looks like this:

      C:\test>type test.pl use CGI qw(:standard); # my %params = map ($_ => param('$_'),param()); my %params = map {$_ => param($_)} param(); print '-->' . $params{'listname'}. '<--' . "\n"; C:\test>perl test.pl listname=foo bar -->foo<--
        The problem is with the single quote around '$_', which tells param to look for an exact match of '$_', not an element from the param list. If you take away the single quotes from the $_, the code should work fine.

        The reason for the code to display "bar" for "listname" is because the hash was not correctly constructed. The failed mapping created a hash: listname=>bar as a side effect.
        Hrmm, mine looks very similar.
        use strict; use CGI qw/:standard/; use DBI; use Mail::Sendmail; use MIME::Lite; my $q = CGI::new(); my %params = map {$_ => param('$_')} param(); print '-->' . $params{'listname'}. '<--' . "\n";
        I am running this on a debian box however you seem to be using windows, i wonder if this is contributing as our code looks very similar.

        Neil Archibald
        - /dev/IT -
      Yet dws is right. This works for me:
      use CGI qw/:standard/; my %params = map { $_ => scalar param($_) } param(); print '-->' . $params{'listname'}. '<--' . "\n";
      Note that I added a "scalar". The error was produced by three things:
      1. You're trying to use A => B as an expression for map, which doesn't work, as "=>" is actually a comma. So map only saw the "A" as the expression, and the "B" as the first item of the list fed to it.
      2. You used single quotes around "$_" which made CGI.pm look for the parameter "'$_'", literally, which doesn't exist. Combine that with...
      3. You used param() in a list context, which for a non-existing parameter produces an empty list. "scalar" fixes that.
    So, in summary, you were filling %params with a list ( 'listname', 'wtf' ), as everything else dropped away.