Perl Introduction/Explained

vol7ron

New Member
Messages
434
Reaction score
0
Points
0
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::eek:utput.

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:

compwhizii

Banned
Messages
779
Reaction score
0
Points
0
*Moved to tutorials*

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

vol7ron

New Member
Messages
434
Reaction score
0
Points
0
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.
 

incahouse

New Member
Messages
3
Reaction score
0
Points
0
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
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
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:
Top