Results 1 to 4 of 4
  1. #1
    WTF Groupie Array
    Join Date
    Oct 2010
    Posts
    51
    Thanks
    0
    Thanked 26 Times in 20 Posts

    Perl Lesson 5: Reading Input

    Reading input into your CGI program isn’t rocket science, but the code to do it is a little more complex than what we’ve dealt with so far. The good news is that you don’t need to think about it because it’s already written for you!

    Most Perl installs will come with a module called the CGI.pm module. It’s very handy, and allows you to read input to your program easily (as well as simplifying other tasks).

    What you need to know in order to read input from your users is how to use CGI.pm in order to read input from your forms. The first thing you need to do is put the following line near the beginning of your program:

    Code:
    use CGI;
    I’d put it either right before, or right after my use STRICT; line. This tells the Perl interpreter that you want to use a module, and will make all the code from that module available for your use.

    Modules comply with what is known as Object Oriented programming. That means that in order to use the code supplied by a module, you first have to create an object, or “instance” of that module. This will be a variable that will be used to access all of the functions available by that module. This is how you’d declare a new instance of the CGI.pm module:

    Code:
    my $q = new CGI;
    Don’t ask me why, but the most common variable name used for creating an instance of CGI is $q. Of course, you can make the variable name whatever you want.

    So, let’s say you had a textbox named country in a form that's being submitted to your Perl CGI script. You’d access the information from that text box using your $q variable. For easy access later, it’s common to assign the value to another variable early in the program:

    Code:
    my $country = $q->param(“country”);
    And that’s how you access each of the values that would be set up in your input forms. From then on after this line you can access the value that the user has typed in through the variable $country.

    When you’re dealing with checkboxes, the checkbox will show as being activated by having “on” as it’s value. Radio buttons will work exactly the same way as other input types, it’s value will be set to the value of the option that was selected.

    There are lots of other handy features that you can use with the CGI.pm module, we’ll be discussing those in future lessons. But these are the solid basics that you'll need to know in order to access variables in Perl and create CGI scripts that actually interact with the user.

  2. The Following User Says Thank You to Mongwell For This Useful Post:

    Sandy (10-31-2010)

  3. #2
    WTF Senior Array
    Join Date
    Nov 2009
    Posts
    1,369
    Thanks
    26
    Thanked 41 Times in 30 Posts


    Great tutorial! I never really used Perl. What advantages does it have over PHP?

  4. #3
    WTF Groupie Array
    Join Date
    Oct 2010
    Posts
    51
    Thanks
    0
    Thanked 26 Times in 20 Posts


    PHP has certainly become more popular than Perl these days. Perl was the big programming language for the web before PHP came along. PHP was actually designed to imitate Perl in many ways.

    One advantage of Perl is that it's a more mature programming language than PHP, and it's features may be more stable than PHP which is growing and changing in new and untested ways all the time. There are also still a lot of web applications out there written in Perl, so if you learn Perl you can draw on a large amount of pre written code.

    As a more stable and mature programming language, Perl has more advanced features that you would expect from a programming language. PHP was made specifically for the web so it's been kind of playing catch up.

    Mainly, though, it's a matter of personal preference. I personally like Perl's structure, it seems more like you're working with the programming language primarily, rather than with PHP where it seems kind of like you're embedding PHP in an HTML document.

  5. #4
    WTF Lurker Array
    Join Date
    Apr 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts


    If you want to improve your outcomes, you have to improve your actions.


 

Bookmarks

Posting Permissions

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