TheMatrix
June 18th, 2011, 02:32 PM
Table of contents:
Your very first script! (http://virtualteen.org/forums/showpost.php?p=1309827&postcount=1)
Perl Tutorial: Variables, arrays, hashes, subroutines, and simple logic. (http://www.virtualteen.org/forums/showpost.php?p=1310887&postcount=2)
I have decided to teach all who care some Perl! In this post, I will be going over using Perl and a basic "Hello World" script.
Step 1: Prerequisites
This is by far the most important step. Nothing here will work without it.
Perl
If you use Linux, Macintosh(System 9 or newer), FreeBSD, Solaris, or any other *nix system, you probably already have Perl installed.
If you use Windows NT(or above), you will need to install it. You may use ActivePerl (http://www.activestate.com/activeperl/downloads) or any other Perl interpreter. Heck, you could even compile Perl yourself if you really wanted to.
Text editour
Of course you must have a method of actually saving your scripts! Hence, you need a text editour.
Linux
You most likely already have several. Try gEdit or KWrite. Or if you are a command-line person, use vim.
Macintosh(System 10 or later)
I think you use TextEdit or vim.
Windows
There are many. ActivePerl has one, I think. I reccomend that one. Or, use Notepad(when saving, do not forget to save as "script.pl" and set the File type to "All Files").
In addition, on DOS 3.1 or later, you can type "EDIT C:\DOCUME~1\[Your username]\script.pl" in a CMD.EXE window.
Step 2: Starting out your scripts
Once you've got Perl installed, you will need to know WHERE it is installed.
On *nix systems, type the following into a Terminal:
which perl
Remember the output of that, you will need it every time you make a script.
Usually it is /usr/bin/perl, but it can also be in/usr/local/bin/perl, or anywhere else.
On Windows, however, it is somewhere else. I do not know where it would be installed, but I think it will be in C:\Perl\bin\perl, but I could be wrong...
In my sample scripts, I will assume /usr/bin/perl. Adjust your path accordingly.
Step 3: Learning the syntax
Perl has a syntax that is relatively similar to that of PHP, if any of you know PHP. Some basic things to remember are:
A comment is started with a "#"
All commands must end with a semicolon!
Although some things may seem challenging, you'll learn it, eventually.
Also, the style I use in my scripts may not be what you are comfortable with. There is no "correct" style of programming. Use what you think is best.
Step 4: Make your very first script.
So once you've figured out where Perl is, let's use that in a script.
Here is the code:
#!/usr/bin/perl -wT
#This is our very first script!
print "Hello, World!\n";
Save as "hello.pl", to your desktop(you can save it elsewhere, but this tutorial is geared to that path).
Before running it, read the following sections.
Step 5: Some things to remember
There are some conventions I use throughout my tutorials. Some include that I will always add the "-wT" to the end of my pound-bang(#!) line. Those are
some options to send to the Perl interpreter. The "w" stands for "warnings" and the "T" for taint mode. The warnings are useful for debugging your code. The "taint" is for making sure that other data cannot interfere with your script. Although not needed for a "Hello World" script, it can be useful later.
Now, if you look at the sample script, you will see a "\n" at the end of the print statement. This signifies a new line. It is different on most systems.
Windows use "\r\n" for new line
Macintosh System 9 or earlier use "\r" for a new line.
*nix and Macintosh System 10 and newer use "\n" (I use this one in all scripts. Adjust yours accordingly.)
Step 6: Running the script
Now comes the fun part!
Windows
Go to a CMD.EXE window and type CHDIR "C:\DOCUME~1\[Your Username]\DESKTOP\"
Then simply type: hello.pl
Macintosh(System 10 and later), *nix
Open a terminal(On Macintosh, go to "Spotlight" and type "Terminal").
Then type the following commands. Your terminal should then look like this:
[email protected]:/> cd ~/Desktop/
[email protected]:~/Desktop> perl -wT hello.pl
Macintosh(System 9)
I think the same as above, but I don't know where the Terminal would be.
If you get an output of:
Hello, World!
you have done it right. Congratulations!
Step 7: Conclusion
That was all. Quite simple, no?
You are now well on your way to becoming a great programmer: ;)
If you have any questions, comments, or ideas, please post them!
Your very first script! (http://virtualteen.org/forums/showpost.php?p=1309827&postcount=1)
Perl Tutorial: Variables, arrays, hashes, subroutines, and simple logic. (http://www.virtualteen.org/forums/showpost.php?p=1310887&postcount=2)
I have decided to teach all who care some Perl! In this post, I will be going over using Perl and a basic "Hello World" script.
Step 1: Prerequisites
This is by far the most important step. Nothing here will work without it.
Perl
If you use Linux, Macintosh(System 9 or newer), FreeBSD, Solaris, or any other *nix system, you probably already have Perl installed.
If you use Windows NT(or above), you will need to install it. You may use ActivePerl (http://www.activestate.com/activeperl/downloads) or any other Perl interpreter. Heck, you could even compile Perl yourself if you really wanted to.
Text editour
Of course you must have a method of actually saving your scripts! Hence, you need a text editour.
Linux
You most likely already have several. Try gEdit or KWrite. Or if you are a command-line person, use vim.
Macintosh(System 10 or later)
I think you use TextEdit or vim.
Windows
There are many. ActivePerl has one, I think. I reccomend that one. Or, use Notepad(when saving, do not forget to save as "script.pl" and set the File type to "All Files").
In addition, on DOS 3.1 or later, you can type "EDIT C:\DOCUME~1\[Your username]\script.pl" in a CMD.EXE window.
Step 2: Starting out your scripts
Once you've got Perl installed, you will need to know WHERE it is installed.
On *nix systems, type the following into a Terminal:
which perl
Remember the output of that, you will need it every time you make a script.
Usually it is /usr/bin/perl, but it can also be in/usr/local/bin/perl, or anywhere else.
On Windows, however, it is somewhere else. I do not know where it would be installed, but I think it will be in C:\Perl\bin\perl, but I could be wrong...
In my sample scripts, I will assume /usr/bin/perl. Adjust your path accordingly.
Step 3: Learning the syntax
Perl has a syntax that is relatively similar to that of PHP, if any of you know PHP. Some basic things to remember are:
A comment is started with a "#"
All commands must end with a semicolon!
Although some things may seem challenging, you'll learn it, eventually.
Also, the style I use in my scripts may not be what you are comfortable with. There is no "correct" style of programming. Use what you think is best.
Step 4: Make your very first script.
So once you've figured out where Perl is, let's use that in a script.
Here is the code:
#!/usr/bin/perl -wT
#This is our very first script!
print "Hello, World!\n";
Save as "hello.pl", to your desktop(you can save it elsewhere, but this tutorial is geared to that path).
Before running it, read the following sections.
Step 5: Some things to remember
There are some conventions I use throughout my tutorials. Some include that I will always add the "-wT" to the end of my pound-bang(#!) line. Those are
some options to send to the Perl interpreter. The "w" stands for "warnings" and the "T" for taint mode. The warnings are useful for debugging your code. The "taint" is for making sure that other data cannot interfere with your script. Although not needed for a "Hello World" script, it can be useful later.
Now, if you look at the sample script, you will see a "\n" at the end of the print statement. This signifies a new line. It is different on most systems.
Windows use "\r\n" for new line
Macintosh System 9 or earlier use "\r" for a new line.
*nix and Macintosh System 10 and newer use "\n" (I use this one in all scripts. Adjust yours accordingly.)
Step 6: Running the script
Now comes the fun part!
Windows
Go to a CMD.EXE window and type CHDIR "C:\DOCUME~1\[Your Username]\DESKTOP\"
Then simply type: hello.pl
Macintosh(System 10 and later), *nix
Open a terminal(On Macintosh, go to "Spotlight" and type "Terminal").
Then type the following commands. Your terminal should then look like this:
[email protected]:/> cd ~/Desktop/
[email protected]:~/Desktop> perl -wT hello.pl
Macintosh(System 9)
I think the same as above, but I don't know where the Terminal would be.
If you get an output of:
Hello, World!
you have done it right. Congratulations!
Step 7: Conclusion
That was all. Quite simple, no?
You are now well on your way to becoming a great programmer: ;)
If you have any questions, comments, or ideas, please post them!