Cool lil PERL feature i learned recently with cgi.pm

  • Borrow -A- Geek
  • Professor
  • Professor
  • User avatar
  • Posts: 763
  • Loc: Dallas/Ft Worth, Texas

Post 3+ Months Ago


in PERL 5.0+ it comes with a module of cgi.pm.
i have started studying this module and it is VERY powerful in the things it can do for you here is the tip of the iceburg, below.. ill post more as i learn.
Code: [ Select ]
use CGI::Carp qw( fatalsToBrowser );

put that little line of code right below the shebang an thats it

that little teeny weenie line does wonders for troubleshooting scripts.. now i dont have to run the script in a bash shell to find the errors.

here is an example of a result from it, rather than the unhelpful 500 error message.


http://www.ferrisdesign.net/cgi-bin/test/raffle.cgi

(i purposely placed an error in the script to get it to show in the example)
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Borrow -A- Geek
  • Professor
  • Professor
  • User avatar
  • Posts: 763
  • Loc: Dallas/Ft Worth, Texas

Post 3+ Months Ago

Here is a cool little sample script that demonstrated the shortcuts and power of the cgi.pm... notice the difference even at a glance from straight perl...

Code: [ Select ]
 
#!/usr/bin/perl
# CGI script that creates a fill-out form
# and echoes back its values.

 use CGI qw/:standard/;
 print header,
    start_html('A Simple Example'),
    h1('A Simple Example'),
    start_form,
    "What's your name? ",textfield('name'),p,
    "What's the combination?", p,
    checkbox_group(-name=>'words',
            -values=>['eenie','meenie','minie','moe'],
            -defaults=>['eenie','minie']), p,
    "What's your favorite color? ",
    popup_menu(-name=>'color',
          -values=>['red','green','blue','chartreuse']),p,
    submit,
    end_form,
    hr;



  if (param()) {
    print "Your name is",em(param('name')),p,
       "The keywords are: ",em(join(", ",param('words'))),p,
       "Your favorite color is ",em(param('color')),
       hr;
  }
  1.  
  2. #!/usr/bin/perl
  3. # CGI script that creates a fill-out form
  4. # and echoes back its values.
  5.  use CGI qw/:standard/;
  6.  print header,
  7.     start_html('A Simple Example'),
  8.     h1('A Simple Example'),
  9.     start_form,
  10.     "What's your name? ",textfield('name'),p,
  11.     "What's the combination?", p,
  12.     checkbox_group(-name=>'words',
  13.             -values=>['eenie','meenie','minie','moe'],
  14.             -defaults=>['eenie','minie']), p,
  15.     "What's your favorite color? ",
  16.     popup_menu(-name=>'color',
  17.           -values=>['red','green','blue','chartreuse']),p,
  18.     submit,
  19.     end_form,
  20.     hr;
  21.   if (param()) {
  22.     print "Your name is",em(param('name')),p,
  23.        "The keywords are: ",em(join(", ",param('words'))),p,
  24.        "Your favorite color is ",em(param('color')),
  25.        hr;
  26.   }
  • Borrow -A- Geek
  • Professor
  • Professor
  • User avatar
  • Posts: 763
  • Loc: Dallas/Ft Worth, Texas

Post 3+ Months Ago

PROGRAMMING STYLE
There are two styles of programming with CGI.pm, an object-oriented style and a function-oriented style. In the object-oriented style you create one or more CGI objects and then use object methods to create the various elements of the page. Each CGI object starts out with the list of named parameters that were passed to your CGI script by the server. You can modify the objects, save them to a file or database and recreate them. Because each object corresponds to the ``state'' of the CGI script, and because each object's parameter list is independent of the others, this allows you to save the state of the script and restore it later.
For example, using the object oriented style, here is how you create a simple ``Hello World'' HTML page:


Code: [ Select ]
  #!/usr/local/bin/perl -w
  use CGI;               # load CGI routines
  $q = new CGI;            # create new CGI object
  print $q->header,          # create the HTTP header
     $q->start_html('hello world'), # start the HTML
     $q->h1('hello world'),     # level 1 header
     $q->end_html;         # end the HTML
  1.   #!/usr/local/bin/perl -w
  2.   use CGI;               # load CGI routines
  3.   $q = new CGI;            # create new CGI object
  4.   print $q->header,          # create the HTTP header
  5.      $q->start_html('hello world'), # start the HTML
  6.      $q->h1('hello world'),     # level 1 header
  7.      $q->end_html;         # end the HTML



In the function-oriented style, there is one default CGI object that you rarely deal with directly. Instead you just call functions to retrieve CGI parameters, create HTML tags, manage cookies, and so on. This provides you with a cleaner programming interface, but limits you to using one CGI object at a time. The following example prints the same page, but uses the function-oriented interface. The main differences are that we now need to import a set of functions into our name space (usually the ``standard'' functions), and we don't need to create the CGI object.

Code: [ Select ]
  #!/usr/local/bin/perl
  use CGI qw/:standard/;      # load standard CGI routines
  print header,          # create the HTTP header
     start_html('hello world'), # start the HTML
     h1('hello world'),     # level 1 header
     end_html;         # end the HTML
  1.   #!/usr/local/bin/perl
  2.   use CGI qw/:standard/;      # load standard CGI routines
  3.   print header,          # create the HTTP header
  4.      start_html('hello world'), # start the HTML
  5.      h1('hello world'),     # level 1 header
  6.      end_html;         # end the HTML
  • dreamer7
  • Student
  • Student
  • dreamer7
  • Posts: 95
  • Loc: UK

Post 3+ Months Ago

yup that line is v useful but what i havent figured out yet is what the -> actually means in reference to scalars cud ya give me a definition please

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 144 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.