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

All, id like to be able to store txt from a comment box form into an array but im having problems just trying a simple test. A user will enter the elements as such:
test1 test2 test3
The test script called by the browser:
print "Content-type: text/html\n\n"; use strict; use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); print "The following options have been selected:<br>\n"; print "<br>\n"; my %form; foreach my $p (param()) { $form{$p} = param($p); print "$p = $form{$p}<br>\n"; print LOG "$form{$p}\n"; } my @apps = qw($form{'AppName'}); foreach my $i (@apps) { print "<br>\n"; print "$i\n"; } print end_html;

The values get stored in $form{'AppName'}. When calling $i, I only get $form{'AppName'} returned to the browser, not the actual values.

Replies are listed 'Best First'.
Re: Arrays with CGI perl help
by SuicideJunkie (Vicar) on Jun 24, 2009 at 15:04 UTC
    qw/STRING/ does not do what you think it does (it does not interpolate variables)
    If you are trying to split on whitespace in the appname field, try my @apps = split '\s+', $form{'AppName'};
      The actual equivalent would be
      my @apps = split ' ', $form{'AppName'};

      The difference is in how they treat leading spaces. See split

      $ perl -le'print 0+( @a = qw( a b c ) )' 3 $ perl -le'print 0+( @a = split /\s+/, " a b c " )' 4 $ perl -le'print 0+( @a = split " ", " a b c " )' 3
        Thanks everyone!
Re: Arrays with CGI perl help (More fundamental issues)
by ww (Archbishop) on Jun 25, 2009 at 04:03 UTC
    SuicideJunkie and ikegami have provided answers to your nominal question but even with their corrections, I'm not quite sure why your test script produces any content renderable in the browser, since it lacks several elements required by html standards, notably <html>, <head>, <title>, </title>,</head> and <body>:

    The CGI.pm doc (available on your local machine at perldoc CGI.pm) provides a simple example of (one way) to do so (and throws in a recommended-but-not-required DTD for free):

    use CGI qw/:standard/;
    print header,
    start_html('A Simple Example'),
    ...

    which will provide, for rendering:

    Content-Type: text/html; charset=ISO-8859-1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-U +S"> <head> <title>A Simple Example</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> </head> <body>

    Note that the print header, took care of the required Content-Type: text/html; charset=ISO-8859-1-followed-by-two-newlines. Since you didn't use print header your manual print of the Content-Type is NOT redundant (if interested in the issue, you can read numerous nodes here where the problem was coding in such a way as to emit Content-Type... twice).

    If you chose to avoid print header, you MUST supply the <html>, <head>, <title>, title-content, </title>, </head><c> and <c><body>. (IMO, the best reason to eschew print header,... is to allow you to select another DTD and add additional meta data.)

    Update: edited html to fix cut'n'paste error which doubled "<!DOCTYPE html" - thanks ikegami!

      Browsers will attempt to render even if the code is not to standard, in fact no actual html tags are needed whatsoever to get text to display.
        Indeed. In fact, early versions of Internet Explorer would render plain text or even binary data as HTML if the first few bytes appeared to contain a '<' character, no matter what content-type the server specified :-|

        -- Time flies when you don't know what you're doing

      You have been greatly misinformed. Under strict HTML4,

      • You failed mention that the DOCTYPE decleration is required.
      • No, the <html> tag is optional.
      • No, the <head> tag is optional.
      • While the title element is required (and its start and end tags are required), browsers cope with its absence.
      • No, </head> tag is optional.
      • No, <body> tag is optional.

      Of course, the OP gave no indication he was interested in issuing strict HTML4.

        Given your cite, I stand corrected on my statement re the DOCTYPE being optional; I can't even recall which 'authority' put that notion in my head.

        However, I would continue to hold that the <head>...</head> is required (*1 qualifier below) because <title>...</title> is required and §7.4 of w3c's html 4 documentation says:

        "Every HTML document must have a TITLE element in the HEAD section." (emphasis in the original at 7.4.3)

        *1 And yes, that directly contradicts the w3c cite you provided ...suggesting that:

        1. w3c needs to get its act together
          or
        2. I'm mis-reading something, or failing to distinguish properly between its "normative" and "informative" documentation.

        So, while I'm uncertain which citation governs here, I think each of us has an arguably sound basis for our assertions (at least re <head>...<head>).

        Moving on to into calculations of how many angels can dance on the head of a pin...

        §7.3, "The HTML element," appears to me to be directive and thus to offer some support for my notion that the <html ...> is required:

        "After document type declaration, the remainder of an HTML document is contained by the HTML element."

        But, I must also concede that the quote above does not quite offer explicit contradiction of the earlier indication that the <html> element is optional... and I cannot re-find the (vividly/spuriously?) remembered 'authority' which -- as I recall it -- asserted that tags like <p>, <h#>, <ol>, <table>, <div> and company can appear only inside a <body>...</body> pair.

        That said, I belatedly noticed (and only after your msg re the doubled <!DOCTYPE> for which, thanks!) that CGI.pm produced an XHTML <!DOCTYPE>, making the example pretty much irrelevant to the thrust of my comment ((or irrelevant, confusing and stupid: ie, I forgot to include -no_xhtml in the use CGI line).