A
Alan J. Flavell
Absolutely. Reading decent code is one of the best ways to learn.
Well, yes, but there's a massive difference between the elaborate code
that might be found in a well-tested and peer-reviewed module,
intended to deal well with all possible situations that it's going to
encounter in the Real World(tm), on the one hand; and a
straightforward little script to use that module, checking that all is
well but otherwise simply baling out when it recognises that it's not.
Or in clear text: CGI.pm internally appears to be contorted code, but
there's generally good reasons for what it does and how it does it;
however, it's probably not the kind of code that the average *user* of
CGI.pm should be seeking to emulate.
You do have to be sure your source is reliable, though: there is one
hell of a lot of very bad Perl floating around the web.
That too, for sure. But that's a different axis of evaluation.
I don't think this is guaranteed,
I would ask anyone interested in the following to read all of it,
carefully, or not at all. Half-measures are inadvisable.
Point 1. Read
http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1 , item 2.
Read also
http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2
in the paragraph beginning 'A "multipart/form-data" message contains a
series of parts'.
Thus, both of the mandatory submission formats specify that the items
are required to be submitted in the same order that they appeared in
the form.
Point 2. Client agents don't necessarily conform to the spec
(although most of them do nowadays).
Point 3. In Perl, f you get your submitted name/value pairs from the
module as a "hash", then of course the ordering has been lost by then.
However, in every other respect, the hash is very much the "natural"
way to represent these things in Perl.
Point 4. The whole point of defining the values by name/value pairs
is surely to make them accessible by name rather than by position?
If the designers of HTML forms had wanted to implement positional
parameters, they could have done so (in fact they already did - check
the <ISINDEX> element, now deprecated, from earlier versions of HTML).
My conclusion: although the HTML4 spec requires the name/value pairs
to be transmitted in same order they appear in the form, it seems to
me that it's utterly pointless to want to rely on all client software
actually doing that. I've often met writers of scripts who seemed
completely obsessed with needing to process the items in the same
order as which they were present in the form, but on closer study I've
never found any justification for doing so, and as soon as the writer
agreed to drop their insistence that they "needed" this, they found
their scripts were easier to write, with no loss of functionality.
While I'm sure that someone could devise a requirement that depended
on the ordering, I can't see any advantage in doing so.
IMHO and YMMVWV.
You may very well want to re-write the form e.g with existing inputs
filled-in and waiting for further input from the user - but the right
way to do that is probably to use the same code to write the original
empty form as re-writes the partially completed form, and that code
will certainly know what is the proper ordering of the items on the
HTML form itself. But when the boss says the items have to come in a
different order on the web page, there will be no need for a major
rewrite of the code to take that into account, if you've written code
that isn't sensitive to the ordering in the first place.
by which I mean that it may happen
to work for you with your browser during this phase of the moon, but
under other circumstances it may well not.
Something like that; but by gaining the benefits of the hash
representation, one also discards any supposed benefits there might
have been in the original ordering, so - as I say - it seems to me to
be the wrong approach anyway.
If you need to keep separate track of the different paramaters, give
them different names. Change whatever generates them to put a number
on the end, or something.
If you want to iterate through the name/value pairs that are present,
then just iterate through the keys of the hash. Write the code so
that the ordering doesn't matter. The resulting code is likely to be
simpler than trying to re-create the problem of positional parameters
all over again - would be my advice.