Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Any idea to let Perl form data and scalar work in same page?

by GordonLim (Acolyte)
on Mar 19, 2013 at 14:08 UTC ( #1024304=perlquestion: print w/ replies, xml ) Need Help??
GordonLim has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I new in Perl CGI. Now I am facing a problem when I fetch my username and password from previous page to existing page, my form data will be not function. Form Data only function when the script is on the top and same to scalar it also only function if the script is on the top of the page. Script as below:
#!/usr/bin/perl use CGI; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use CGI::Pretty qw(:all); my $username = param('username'); my $password = param('password'); read(STDIN, $buffer,$ENV{'CONTENT_LENGTH'}); $buffer =~ tr/+/ /; $buffer =~ s/\r/ /g; $buffer =~ s/\n/ /g; @pairs = split(/&/,$buffer); foreach $pair(@pairs){ ($key,$value)=split(/=/,$pair); $formdata{$key}.="$value"; } #delete file $version=$formdata{'version'}; $Chassis=$formdata{'Chassis'}; $Assigned=$formdata{'Assigned'};.....

Comment on Any idea to let Perl form data and scalar work in same page?
Download Code
Re: Any idea to let Perl form data and scalar work in same page?
by mithaldu (Monk) on Mar 19, 2013 at 14:23 UTC
    Looking at your code, trying to fix that as it is would benefit neither you nor us. Please head over to the Perl Tutorial Hub and choose a current resource to start learning from. I can personally recommend Modern Perl 2012 and Ovid's Beginning Perl (which has a nice chapter on web apps).

    Especially the latter is recommended because CGI.pm is a module that is very old and will make development very difficult for you (as you are noticing). Better solutions described in that book will make it much easier to write web applications.

    As a matter of curiosity, could you please also tell us what resource you are currently learning from and how you found it?

      Especially the latter is recommended because CGI.pm is a module is very old and will make development very difficult for you (as you are noticing).

      His current issues have very little to do with CGI.pm being old

        Well, kinda. The OP's problem is that CGI.pm is exhausting STDIN, and it's impossible to seek back to the start of STDIN.

        The modern interface for Perl web apps is PSGI (implemented by Plack). The equivalent of reading from STDIN in PSGI is to read from $env->{"psgi.input"}. This is a filehandle that is usually seekable (depending on what handler you're using).

        package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      I copied this from my senior. Because I cannot find the method how to from loop grep the row of the data I need (data is from another .txt file). Previous page list out the data.txt file data with table, and click the "edit" button to grep the row I want to edit. This page is for filter and edit the data row I want. Sorry that I still very new to develop website.
Re: Any idea to let Perl form data and scalar work in same page?
by Anonymous Monk on Mar 19, 2013 at 14:36 UTC

    What tutorial are you reading?

    You should keep using param() instead of $formdata

    For form input to persist between page/loads , you have to save it, or resend it

    What are you trying to accomplish, maybe you want to use one of Mojolicious or Dancer or Catalyst::Manual::Tutorial...

Re: Any idea to let Perl form data and scalar work in same page?
by tobyink (Abbot) on Mar 19, 2013 at 14:53 UTC

    CGI.pm has a method called init which slurps all of STDIN. So if init is called, you can't read STDIN - it's already been read. (And STDIN is a one-time-only thing.)

    param internally calls init.

    Not sure why you want to be reading this data from STDIN anyway. Just do:

    $Chassis=param('Chassis');
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      Hi All, Thanks all of your help. The problem is solved after I disable
      read(STDIN, $buffer,$ENV{'CONTENT_LENGTH'}); $buffer =~ tr/+/ /; $buffer =~ s/\r/ /g; $buffer =~ s/\n/ /g; @pairs = split(/&/,$buffer); foreach $pair(@pairs){ ($key,$value)=split(/=/,$pair); $formdata{$key}.="$value"; }
      and change all the $formdata to param(). Last time I did changed to param without disable read(STDIN... Is my silly mistake. Thank you all of your advise. :) appreciated so much. At the end thank tobyink remind me to try param () again.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2014-12-29 14:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (188 votes), past polls