Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

BrainPain-Help

by coreolyn (Parson)
on Aug 10, 2000 at 19:43 UTC ( [id://27308]=perlquestion: print w/replies, xml ) Need Help??

coreolyn has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks please help me to find the error of my ways in the following cgi script.

my $Requestor = $q->param('Requestor'); print $q->p("ChangeAdm::Review::Requestor = $Requestor"); my %Requestor_fields = ( RequestorID=>$q->param('RequestorID') +, Requestor=>$q->param('Requestor'), Department=>$q->param('Department'), Extention=>$q->param('Extention'), ); print $q->p("ChangeAdm::Review::Requestor_fields{Requestor} = +$Requestor_fields{Requestor}");

This produces the following output:

ChangeAdm::Review::Requestor = Jane Doe ChangeAdm::Review::Requestor_fields{Requestor} =

What is wrong with the way I am creating the hash?

coreolyn Duct tape devotee.

Replies are listed 'Best First'.
RE: BrainPain-Help
by merlyn (Sage) on Aug 10, 2000 at 20:07 UTC
    Another way to do that code in the middle, now that I see it:
    my %Requestor_fields; $Requestor_fields{$_} = $q->param($_) for qw(RequestorID Requestor Dep +artment Extention);

    -- Randal L. Schwartz, Perl hacker

Re: BrainPain-Help
by merlyn (Sage) on Aug 10, 2000 at 19:47 UTC
    A my variable is not in the package, so is not visible with a package spec. Remove the my, or rethink what you are doing.

    -- Randal L. Schwartz, Perl hacker

      Bleh - it was an entirely different problem. The problem is this (simplified):
      %x = ( A => f(1), B => f(2), C => f(3) );
      In this code, function f is being invoked in a list context, and may return 0 or more items. This will really mess up the key/value pairing required for %x.

      I suggest your code be hacked so that you're doing:

      %x = ( A => scalar f(1), B => scalar f(2), C => scalar f(3) );

      -- Randal L. Schwartz, Perl hacker

        But you are still right to complain about the my. The name that is advertised in the debugging statement refers to a package variable and not to the variable actually being printed. This indicates some confusion. Plus my and mod_perl (tyop fix, had been CGI) when not understood correctly leads to lack of sharing which is a deep problem.

        I know why it is a problem in Perl, and I think I have a sense of why there is no good fix for the problem, but I cannot articulate it very well.

        For those who don't know, if you have a my around a sub and the my is executed multiple times, you then wind up with multiple copies of the my variable (one per invocation), and there are deep naming problems making it impossible to give a clean answer as to which copy of the my goes with the globally named sub.

        Perl doesn't answer it, it just gives you a "Cannot stay shared" warning and proceeds to not do what you might expect. If you use my variables like globals in your script in mod_perl, you will hit this because mod_perl executes your script on each call. Execute it twice and now all of those global mys are not shared properly and you have serious voodoo.

        This is a very good reason to be in the habit of using vars when you really are thinking of globals. (Based on first principles I would want to check in 5.6 whether carelessness with "our" could also hit this problem. I honestly don't know the answer.)

        /tell tilly if anyone needs clarification on this.

        I've seen this bite people more than once (it bit me once). It makes me think => providing scalar context to its right-hand side would be a good enhancement. Of course, that probably won't happen since I've seen people feel gleeful about abusing => for things like kill INT=>@pids. ):

        We could sneak it in by first adding a warning if the list context after => made a difference (wantarray was called, @array was evaluated, etc.). But that sounds quite difficult. Dang, I need that time machine again!

                - tye (but my friends call me "Tye")

        Gotcha!

        Thanks merlyn! What bites is this construct works in other places in the script... Man I've a lot of code to fix!

        coreolyn Duct tape devotee.

      I am still confused as all the variables in use are local to the snippet with the exception of the cgi query object $q

      Why does the parameter store correctly when used as:
      my $Requestor = $q->param('Requestor') but not when it is being packed in the hash?

      Requestor=>$q->param('Requestor')

      coreolyn Duct tape devotee.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://27308]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-24 00:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found