Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2005
    Posts
    71
    Thanks
    9
    Thanked 3 Times in 3 Posts

    Need help understanding code

    I have this code that I found and would like to modify to meet my needs, however I am having trouble understanding a portion of the code and there was no explanation given on the site where I found it. Below is the code:

    Code:
    #!/usr/local/gnu/bin/perl
    use CGI qw(:standard);
    
    $regfile = '../public_html/perl/registrations.tsv';
    
    print header;
    
    if(param()) {
        $name = param('name');
        $email = param('email');
        $food = param('food');
        if(ok()) {
        open(REG,">>$regfile") or fail();
        print REG "$name\t$email\t$food\n";
        close(REG);
        print <<END;
    <title>Thank you!</title>
    <h1>Thank you!</h1>
    <p>Your fake registration to Virtual Nonsense Party
    has been recorded as follows:</p>
    <p>Name: $name</p>
    <p>E-mail: $email</p>
    <p>Food preference: $food</p>
    END
        exit; } }
    
    %labels = (
      '' => 'Food preference (select one):',
      'Fish sticks' => 'Fish sticks',
      'Falafel' => 'Falafel',
      'no food' => 'None (i.e., will not eat)' );
    
    print start_form, 'Fake registration to virtual party',br,
      'Name: ', textfield('name'), br,
      'E-mail: ', textfield('email'), br,
      radio_group(-name=>'food', -values=>\%labels, -linebreak=>'true',
                  -default=>''),
      submit, end_form;
    
    sub fail {
       print "<title>Error</title>",
       "<p>Error: cannot record your registration!</p>";
       exit; }
    
    sub ok() {
        $fine = 1;
        if(!$name) { print 'Your name is required!', br; $fine = 0; }
        if(!$email) { print 'Your E-mail address is required!', br; $fine = 0; }
        elsif(!($email =~ m/\@/))
           { print 'An E-mail address must contain the @ character!', br;
             $fine = 0; }
        if(!$food) { print 'A food preference (even if none) is required!',
           br; $fine = 0; }
        if(!$fine) { print 'Please fix the data and resubmit', hr; }
        return $fine; }
    What I can't seem to understand is the sub ok(). It is called with this line if(ok()) {, but I can't understand what is happening and why it works as far as checking input. I don't understand the variable $fine or how it affects the call. If anyone can give a brief explanation I would greatly appreciate it.
    Last edited by Budreaux; 04-06-2010 at 11:06 PM. Reason: Resolved
    It's OK to pee against the wind if your standing in a lake.

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    $fine is just a flag value. The sub is going to return the value of $fine (either zero or one only), depending upon conditions, which is the same as false or true in the if statement. Apparently, ok() is just checking for the presence of the params and whether the $email argument has a certain format.
    Are you a Help Vampire?

  • Users who have thanked tomws for this post:

    Budreaux (04-06-2010)

  • #3
    New Coder
    Join Date
    Aug 2005
    Posts
    71
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Thanks Tom
    It's OK to pee against the wind if your standing in a lake.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •