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

Trying to understand how to write the 'checked'value of an input type checkbox name="presider" while inside of add_record sub. Thanks
##################################### @fields = ("first","last","position","email","telephone","presider"); sub add_record { $key = time(); $record=$key; foreach $field (@fields){ ${$field} = $q->param($field); ${$field} = filter(${$field}); $record .= "\::${$field}"; } $record .= "\::${$presider}"; print"were inside of add_record sub\n"; unless (-e $database){ open (DB, ">$database") || die "Error creating database. $!\n"; } else { open (DB, ">>$database") || die "Error opening database. $!\n"; } flock DB, $EXCLUSIVE; seek DB, 0, 2; print DB "$record\n"; flock DB, $UNLOCK; close(DB); } # End of add_record subroutine. ########################################3 sub print_add_screen{ print<<HTML; <HTML><HEAD><TITLE>Add a Record</TITLE></HEAD> <BODY BGCOLOR="#FFFFFF"> <CENTER><FONT SIZE=5 FACE="ARIAL"> Add a Record </FONT></CENTER> <P> <FORM ACTION="database.cgi" METHOD=POST> <CENTER> <input type=checkbox name="presider" value="" $myvar> Check here <B> +only</B> if individual is currently a presider <TABLE BORDER=1 CELLSPACING=0> HTML foreach $field (@fields){ print<<HTML; <TR> <TD BGCOLOR="e0e0e0"><B>\u$field:</B></TD> <TD><INPUT TYPE=TEXT NAME="$field"></TD> </TR> HTML } # End of foreach. print<<HTML; <TR> <TD COLSPAN=2 BGCOLOR="e0e0e0"> <CENTER> <INPUT TYPE=SUBMIT NAME=action VALUE="Add Record"> </CENTER> </TD> </TR> </TABLE></CENTER> <P> </FONT> </BODY></HTML> HTML } # End of print_add_screen subroutine.

Replies are listed 'Best First'.
Re: Need Help with writing to flat file from add_record sub
by Zaxo (Archbishop) on Sep 12, 2002 at 16:36 UTC

    Where $field is one of ('foo', 'bar', 'baz'), ${$field} is not anything you want. That expression is appropriate to dereference \'foo', say. Your $field is not a reference (though it could be a symbolic reference, a dreary game). Were you trying to populate a hash there? If so, insert an identifier between the initial dollar and the braces.

    Your conditional for opening on existence of $database is unnecessary and dangerous. Just use ">>" open always. The danger comes when another instance races to create the database. Calling sysopen instead would let you handle locking and exotic modes all at once. (Added) As fglock says, don't seek if opened to append.

    After Compline,
    Zaxo

Re: Need Help with writing to flat file from add_record sub
by fglock (Vicar) on Sep 12, 2002 at 16:26 UTC

    I think you don't need  seek DB, 0, 2.

    (you are normally supposed to append to the end of a file, and >> takes care of that)