in reply to Re^2: globbed variable in parse cgi form - beyond me
in thread globbed variable in parse cgi form - beyond me

'$name' creates the 5 character string "$","n","a","m","e". Double-quoted strings interpolate, but no need for that either. Use ->param($name)

May I also recommend that you don't declare your variables until you need them? No need to make their scope larger than needed.

#! /usr/bin/perl -w use strict; use warnings; use CGI; use CGI::Carp qw( fatalsToBrowser warningsToBrowser ); my $query = CGI->new; my $webmaster = "http://www.mysite.com/"; my @names = $query->param; my @values; foreach my $name (@names) { my $value = $query->param($name); push(@values, $value); } print "Content-Type: text/plain\r\n\r\n"; print "names @names\n\n"; print "values: @values\n\n";

Replies are listed 'Best First'.
Re^4: globbed variable in parse cgi form - beyond me
by Don Coyote (Hermit) on Jun 27, 2011 at 15:31 UTC
    Ikegami,

    Noted

    • scope - not declaring variables before scope required.
    • interpolation - I will have to watch for these syntax traps with this too
    • To remove the single quotes was going to be my next move. Though it is good to understand why so thanks for explaining that. Declaring variables in scope to be used is also something I will adhere to going forward.

      While this is my first CGI script and I do like to learn quicka nd easy, I prefer to understand what it is I'm being quick and easy about. Security always being foremost in my mind. The glob variable is not something I have covered in my reading yet. (getting there..!). So my question was related to trying to get a quick explanation for how to define the glob and subsequent referenced % to get the code to work so that I could digest the principles in due course.

      Of course I was directed back to the CGI module and rightly as it is designed to do what I need to do. The reson I have not been using CGI up till now is that:

      • I had no data being posted to process.
      • CGI.pm produces xhtml and I am writing in 4.01 strict currently

      So in a way I am keeping in scope of DTD.

      However I have realised I am suffering from a race condition. Making my first $million in webification vs buying various animals from the "Zoo".

      Anyone want to sponser a dingbat?

      You said: CGI.pm produces xhtml and I am writing in 4.01 strict currently

      The documentation says:

      -no_xhtml

      By default, CGI.pm versions 2.69 and higher emit XHTML (http://www.w3.org/TR/xhtml1/). The -no_xhtml pragma disables this feature. Thanks to Michalis Kabrianis <kabrianis@hellug.gr> for this feature.

      If start_html()'s -dtd parameter specifies an HTML 2.0, 3.2, 4.0 or 4.01 DTD, XHTML will automatically be disabled without needing to use this pragma.

      It's been awhile since I really read the CGI documentation, and it's had multiple revisions since then. But I found that in about six seconds. Here's how you do it: First you point your browser to http://search.cpan.org. Next type, "CGI", and click on the first distribution listed. Then type into your browser's text-search box "xhtml". The paragraph I posted was the first thing that showed up. Consider this a brief tutorial on how to get a quick answer to a module concern.


      Dave

        Yes I expected there would be something to switch off xhtml. I tried - use CGI qw/:html4/; but maybe that has more to do with importing methods.

        As a third point I forgot to mention a lot of the styling is rendered from css so that negates the use of attributes a bit too.

        The real difficulty i am having now that i have extracted the data using the param methods is in relation to placing a textareas' value into a non textarea element such as a regular div and making it format to P or BR at the newline. I've tried mixing cgi methods with arrays and array scalars explicit and sometimes very explict. lol. And trying to regexp substitute P tags with existing carriage returns and newlines but I just cannot get that value to split and format from a predifined newline character(? i saw a ^M at one point if i can figure waht that is maybe im in with a shot). Maybe I have to tr/%daf/pack hex/ as i haven't done that yet.

        my $ctp = CGI->new; my $webmaster = "http\:\/\/www.mysite.com\/"; my @names= $ctp->param; my @values; foreach my $name(@names){ my $value = $ctp->param($name); push(@values, $value); } # print "Content-type: text/plain\r\n\r\n"; my @tone = $ctp->param('tone'); #print $ctp->p(tone); my $doob; #print $ctp->p("@tone"); print "\@tone: @tone\n\n"; if ($tone[0] =~ m/\\n/){ print "[^f]*. globals and carriage returns an +d zero array value arrays !!\n\n"; } foreach my $toneadd(@tone){ if ($toneadd =~ m/\\r\\n/){ print "[^f]*. globals and carri +age returns!!\n\n"; $toneadd =~ s/\\r\\n/<\/p><p>/;} $doob .= $toneadd; print "\$toneadd: $toneadd\n\n"; } my @tone = $ctp->param('tone'); foreach my $toneadd(@tone){ $line .= "<p>$toneadd<\/p>"; } print $line; print "\$doob: $doob\n\n"; print "<p>$doob<\/p>\n\n";

        Which after another quick & useless debug sesh, I think is shifting me back round to the initial importing through a glob query. Though I can just go and re-open the file again and them in afterwards maybe a good enough workaround even if time consuming.

        OR I gotta read some more stuff somewhere....

      CGI.pm produces xhtml and I am writing in 4.01 strict currently

      You can use CGI.pm for parameter handling without using it for markup generation.