SteveRoe has asked for the wisdom of the Perl Monks concerning the following question:
I seek your wisdom to help deliver me from the ugliness of this code: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>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: CGI Ensure Array
by vladb (Vicar) on May 30, 2002 at 16:19 UTC | |
|
Re: CGI Ensure Array
by thpfft (Chaplain) on May 30, 2002 at 17:45 UTC |