+ Reply to Thread
Results 1 to 8 of 8

Thread: Perl Introduction/Explained

  1. #1
    vol7ron's Avatar
    vol7ron is offline x10 Lieutenant vol7ron is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    DC
    Posts
    434

    Perl Introduction/Explained

    My website will talk about this a little more, but I just wanted to introduce a basic example to show you how to build your own Perl programs.

    Many people thanks it's hard to write their own programs and modules, but it's not. Here's a basic 'helloworld' program and module.

    yourprogram.cgi
    Code:
    #!/usr/bin/perl
    
    use strict;
    use CGI ':standard';
    use yourpackage;
    
    
    print qq(Content-type: text/html\n\n);
    
    print "hello world";
    print pkg::output("hello world");
    yourpackage.pm
    Code:
    package pkg;
    
    use strict;
    
    sub output {
       my ($var) = @_;       #takes in all the function inputs
       $var = "Output from module: $var";
    
       return ($var);
    }
    
    1;
    These should be self explanable.

    yourprogram.cgi
    This is the file that you would call from your browser: http://www.yourwebsite.com/cgi-bin/yourprogram.cgi
    The first line (#!/../..) is the path to the perl interpreter so that your program can run as a standalone. "Use <...>;" is the same as using "include <...>" in C/C++ or basically, how to include functions defined in other programs. Notice the "yourpackage" is actually the file name of the module we create.

    The qq() is a function that wraps all the text inside it in quotes, with the benefit of not having to escape inner quotes. So that line would be the same as print "Content-type: text/html\n\n"; We need this line strictly to output text to the web. It says every subsequent print statement will be in the text/html format. Notice that the program will output two things to the browser, that's 'hello world' and the return from function we call, which we will come to find is 'Output from module: hello world'

    yourpackage.pm
    If we look in the yourpackage.pm, you'll notice the package pkg;, which is the line that defines the scope. This is so that the server knows how to manage functions with the same name, that way in your program you would call the function in the format, scope::functionname(), or using the above example pkg::output.

    The 'use yourpackage;' written in your cgi file will actually point to the yourpackage.pm and to use the functions in it, we use the package name (pkg) defined in the module. I also want to add that if you wish to store your module in a subdirectory of cgi-bin, for example your module is located in ./cgi-bin/modules/yourpackage.pm, then in your .cgi file, you would have "use modules::yourpackage;" instead of "use yourpackage;".

    Notice the one function declared: sub output {...}. Inside it we store all the parameters sent in (@_) into every variable listed in the parentheses. So if there was more than one parameter passed, we would have multiple variables. Example: my ($var1, $var2, $var3) = @_; All the function does is add more text to the parameter passed in and then returns it. That's because this is just a basic tutorial. The "1;" at the bottom is a necessary return to make sure your module ran okay. The cgi will not run without this one.

    There is still more we can talk about even with this simple example; we can get into the special variables $_ (not mentioned) and @_ and other more advanced techniques to do the same thing, but I believe this is a good starting place.



    Anyhow, this should be a nice basic introduction on how to set things up. You can use the basic structure you see here to write your own programs. So the end result of this post you should know how to define a cgi, a module, a variable, and a function. Syntax won't be too different from that of c/c++ and JavaScript, but I am just showing you this because its the modules that people don't usually understand how to write and/or call.

    If you've found it useful or need some help, please let me know,
    vol7ron




    .
    Last edited by vol7ron; 12-05-2008 at 11:43 PM.
    If you find my post useful please add to my reputation by clicking the +Rep button
    You may also use the Donate link to donate credits - this is appreciated too Thanks to those whom have donated so far!


  2. #2
    compwhizii is offline Banned compwhizii is an unknown quantity at this point
    Join Date
    May 2008
    Location
    The Moon
    Posts
    779

    Re: Perl Introduction/Explained

    *Moved to tutorials*

    I've been wanting to get into Perl myself, thanks for the guide.

  3. #3
    vol7ron's Avatar
    vol7ron is offline x10 Lieutenant vol7ron is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    DC
    Posts
    434

    Re: Perl Introduction/Explained

    There is a learning curve in Perl, but it's pretty small and once you've learned it, it's a really fun language to use, especially once you become advanced with regular expressions.

    I would say I'm not a Perl expert and have plenty of room to learn, but I do have a lot of experience with it.
    If you find my post useful please add to my reputation by clicking the +Rep button
    You may also use the Donate link to donate credits - this is appreciated too Thanks to those whom have donated so far!


  4. #4
    sunils's Avatar
    sunils is offline x10 Spammer sunils is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Chennai ,India
    Posts
    2,264

    Re: Perl Introduction/Explained

    A good introduction tutorial. Good job.
    [LEFT][B]Sunil Sankar
    -------------------------------------------------------------------------

  5. #5
    khaledphp is offline Guest khaledphp is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    29

    Re: Perl Introduction/Explained

    great thank yuou

  6. #6
    lch-x10hosting is offline x10Hosting Member lch-x10hosting is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    8

    Re: Perl Introduction/Explained

    Thanks, that script works. But unfortunately I can't seem to get much else to work. Where can I see what Perl modules are installed? Could you have a system-wide installation of these scripts, maybe, or install their dependencies:

    http://www.dfservice.com/soft.htm.en.htm

    or

    http://support.moonpoint.com/languag...ist-modules.pl

    Thanks!

  7. #7
    incahouse is offline x10Hosting Member incahouse is an unknown quantity at this point
    Join Date
    Feb 2010
    Location
    London
    Posts
    3

    Question Re: Perl Introduction/Explained

    Hi can you tell me which version of perl and what interpreter I should use on my pc to perform error
    correction. As I often have to solve an internal server 500 error when my script works fine and pass the perl -wc check on my computer.

    This error comes up on the server side x10hosting, and I cannot resolv it. I have used strict and warnings aswell as CARP.

    Any clues would be grateful.

    Regards,

    S

    ps I use ActiveState

  8. #8
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Perl Introduction/Explained

    The version should not matter.

    Any script I write starts:

    Code:
    #! /usr/bin/perl
    use strict;
    use CGI::Carp qw(fatalsToBrowser);
    
    print "Content-type: text/html\n\n";
    500 errors often are caused by uploading the file in binary mode from a Windows machine.
    Another problem is not putting out the headers before anything else, even errors.
    Also double check permissions and have the script in the cgi-bin. You can execute perl scripts outside of cgi-bin, but you have to adjust .htaccess
    Lastly, you can check the Error Log via cPanel. Depending on the nature of the error, it might get reported there.
    Last edited by descalzo; 02-12-2010 at 11:55 AM.
    Nothing is always absolutely so.

+ Reply to Thread

Similar Threads

  1. Some very usfull links for php & perl.
    By DefecTalisman in forum Scripts & 3rd Party Apps
    Replies: 3
    Last Post: 12-11-2008, 07:49 PM
  2. Internal Server Error FOR Perl script
    By radofeya in forum Free Hosting
    Replies: 0
    Last Post: 04-14-2008, 01:06 PM
  3. Help with PERL code
    By oiwio in forum Programming Help
    Replies: 8
    Last Post: 12-12-2007, 07:38 AM
  4. To all those using Perl and CGI
    By DefecTalisman in forum Free Hosting
    Replies: 0
    Last Post: 09-16-2007, 03:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers