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

Hello, I am trying to find the best way to incorporate a checkbox and a pulldown menu into this add_record subroutine.The checkbox name would be "status" and the pulldown name would be "shift".

Thanks for any help
Cal
@fields = ("first","last","position","email","telephone","presider"); $database = "data/pastoral_members.txt"; sub add_record { $key = time(); $record=$key; foreach $field (@fields){ ${$field} = $q->param($field); ${$field} = filter(${$field}); $record .= "\::${$field}"; } 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.

Replies are listed 'Best First'.
Re: trouble modifying add_record subroutine
by kabel (Chaplain) on Sep 10, 2002 at 17:11 UTC
    please tell us what the function filter does. with a short codechange for the checkbox the addition should reduce to merely add the names to @fields. and try to write subroutines in such a way that they do not rely on variables that are elsewhere defined. for example, you could just pass $database as actual argument to add_record, as with the array @fields.
      Thanks filter() just removes any instance of the delimiter in text.
      Your solution sounds simple. Can you please give me an example.
      sub filter{ $temp = $_[0]; $temp =~ s/\:://; # Remove :: delimiter symbols in text. return ($temp); }
        sorry, my thoughts were not _that_ far. in germany i would say "asche auf mein haupt" - again sorry i cannot translate that...
Re: trouble modifying add_record subroutine
by anithri (Beadle) on Sep 10, 2002 at 17:51 UTC
    foreach $field (@fields){ ${$field} = $q->param($field); ${$field} = filter(${$field}); $record .= "\::${$field}"; }

    First off, unless you really need to save the $field data for later, you don't need to setup the ${$field} variables. You could do...

    foreach $field (@fields) { $temp = $q->param($field); $temp = filter($temp); $record .= "\::$temp"; }

    Secondly, If you did need to save the data as such for some reason... Use a hash.

    foreach $field (@fields) { $data{$field} = $q->param($field); $data{$field} = filter($data{$field}); $record .= "\::$data{$field}"; }

    The person who helped me learn perl told me "If you're dealing with data, always ask How can I use a hash here?"

Re: trouble modifying add_record subroutine
by admiraln (Acolyte) on Sep 10, 2002 at 17:58 UTC
    I am assuming the data is comming from a web page. A few questions have to be asked before the question could be answered. Checkboxes imply a multiple valued result, is your result mulitvalued. If not the visual element you may want is a radio button. The obvious thing was mentioned in another comment that you should add the names to the list of fields
    @fields =("first","last","position","email", "telephone","presider","status","shift");
    This assumes that the method param of object $q will return the values for status and shift. There are a couple of other things to considier. The programs that read the database will have to be modified to handle the extra fields. Also the database will have to be updated to add the extra fields to the existing records.
      Thanks, Yes the data is coming from a web page. At this point I was only trying to use one checkbox. Using a checkbox just seemed better for the user interface. I have added these names "status","shift" to @fields as mentioned.

      I am new to perl, what is the method param of object $q and how do I know If it is correct?
        Another asumption: The object that is in $q is an object created by one of the many CGI modules that exist. A well known one that I have used is CGI.pm by Lincoln Stein. http://stein.cshl.org/WWW/software/CGI/

        The syntax that is used as well as the name of the method "param" suggest it is CGI.pm.

        This method returns several items of in about the parameters submitted from a form including its values. Depending on how the object is created all of the parameters that are submitted will have values.
Re: trouble modifying add_record subroutine
by mephit (Scribe) on Sep 10, 2002 at 20:20 UTC
    foreach $field (@fields){ ${$field} = $q->param($field); ${$field} = filter(${$field}); $record .= "\::${$field}";
    I'm surprised none of the previous posters mentioned that this is a use of symbolic references, and horks under 'use strict.' You are "use strict'-ing, aren't you? I agree with what anithri said here: use a hash instead. Oh, and use strict, too. -w couldn't hurt, either.

    --

    There are 10 kinds of people -- those that understand binary, and those that don't.