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
yourpackage.pm
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: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
.
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: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: