Esteemed Monks, I seek your wisdom and forbearance = this code happily loads CGI params into my namespace, and if desired can fill an array with 0, 1 or many elements:
package Begin_CGI; use CGI; sub import { # ====== my( $r_params ) = (@_) ; my $namespace = (caller)[0] ; use vars qw(@params) ; if ( defined($r_params ) ) { @params = @{$r_params || [] } ; foreach my $var ( '@params', @params ) { my $name = substr($var,1) ; *{"${namespace}::${name}"} = ( substr($var,0,1) eq '@' ) ? \@{$name} : \${$name} ; } } ; ETC,ETC #CGI script... use Begin_CGI ( [qw($scalarparam @arrayparam)]; print start_form(-name=>"input_form") . textfield(-name=>"arrayparam", -value=>"$array_param[0]" -force= +>1); #zero, one or more text fields if so desired print hidden(-name=>"arrayparam", -value=>"dummy", -force=>1) if is_IE +(); print end_form ; #HTML/Javascript <BODY onload="="window_loaded()"> <SCRIPT> function window_loaded { ensure_array(window.document.input_form, 'arrayparam'); } function ensure_array( this_form, name ){ // ------------ // Ensures that the specified array to hold form elements exists. // This function is needed because browser(s) only automaticaly create +s an array // when there is more than one form element with the same name. // This function works around the problem as follows (Netscape): // If there are no form elements with its name it creates an empty arr +ay. // If there is a single element with its name that is the first array +element. // If the array already existed it is returned. // This function works around the problem as follows (IE 6.0): // A dummy (hidden) form element is generated by the perl script // Dummy has disabled property set to true by this function // Perl script strips dummy elements prior to processing // Iterations over arrays that contain dummy elements use adj_len to r +educe length by one if IE var form_prop = "this_form." + name ; var obj = eval( form_prop ) ; if( browser_type() == 'isIE' ) { for ( i=obj.length-1; i >= 0; i-- ) { if( obj[i].value == 'dummy' ) { obj[i].disabled = true ; } } } else if( browser_type() == 'isNav' ) { if ( obj == undefined ) { eval("delete " + form_prop) ; eval(form_prop + " = new Array(0)") ; } else { select_obj = false; if( obj.type != undefined ) { select_obj = ( obj.type.indexOf( "select-" ) == 0 ); } if ( obj.length == undefined || select_obj ) { a = new Array(obj) ; eval("delete " + form_prop) ; eval(form_prop + " = a") ; } } } } </SCRIPT>
I seek your wisdom to help deliver me from the ugliness of this code: But... the main (only?) thing that this code has going for it is that it works with only minor tweaks to the existing Javascript. This is after a lot of experimentation - so please only propose improvements that actually work!!! Lastly, I pray for your forbearance - surely the ration of perl to js in this question is sullying the monastery. Please help me crush it back to a couple of lines!!!

In reply to CGI Ensure Array by SteveRoe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.