[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] More help



Title: Re: [MacPerl] More help
Hi Amitava,

Both errors occur in the same region of code:

====================================================================
# Type of arg 1 to keys must be hash (not subtraction), near "data)"
File 'LC 2:MacPerl ƒ: formregistration.pl'; Line 22
       ====================================================================
foreach $var(keys(%registration-data)){
...

The %registration-data hash should be %registration_data (with an underscore) so that perl doesn't try to interpret %registration-data as a subtraction function.  I'm not sure if it's a general rule or just a stylistic preference, but you shouldn't (as far as I know) use hyphens in variable names, for this reason.


foreach $var(keys(%registration-data)){
    if (length($registration-data{"$var"})==0){
      print"<li>Your<em>$var</em>will be used to help confirm your order
      Please fill in the <em>$var</em>field";}
}
print "</ul>";
}
=====================================================================
# Unmatched right bracket, at end of line
File 'LC 2:MacPerl ƒ: formregistration.pl'; Line 28
# syntax error, near "}"
====================================================================


If you delete the final right bracket so that the above snippet reads (with the change to %registration_data included)

foreach $var(keys(%registration_data)){
    if (length($registration_data{"$var"})==0) {
      print"<li>Your <em>$var</em> will be used to help confirm your order
      Please fill in the <em>$var</em> field";
  }

}
print "</ul>";

then you'll be set.

One important note: MacPerl complains for good reason, and usually offers some decent explanations for what went wrong in the script.  Reading perl's warnings is usually the quickest way to debug your code.

Also, you may want to change that foreach loop to something like this to avoid redundancy:

print "Please fill in the following field", ($num_blank > 1 ? "s" : ""), ":\n"
  if $num_blank = grep { $_ eq "" } values %registration_data;

foreach $key(keys %registration_data) {
  print "\t$key\n" if $registration_data{$key} eq "";
}

(Ignore this if you want, of course!)

Hope that all helps!

Regards,
David