1: Introduction to Perl 2: Perl Basics/Your first program 3: Search Patterns 4: Modules 5: Where to get more info. ------------------------------------------------------- Plus remember that this is an on-going series, check back soon for the next issue where we will hone some of the skills learned here and teach you some more. And is this seems a little basic don't worry they will get harder and more complicated. ------------------------------------------------------- 1: First, we need to understand what Perl is and how to use it. Perl (Practical Extraction and Report Language) is a very versatile language that can be used on pretty much every OS. I will be using and coding things for Unix machines but they can be easily modified to run on Win 32 and MacOS systems. Perl is like a combination of C and Shell scripting. The syntax is very much like C but you use it like a shell script. One interesting thing about Perl is that it does not compile into a binary executable. Every time you run the program it compiles the source. This means you must have Perl installed on your machine, or on a machine that you have access to (ie. Most shells). Know that you know a tiny bit about it lets get on with the learning... 2: If you don't have Perl installed then you can get a copy FREE from www.perl.com/pace/pub/perldocs/ latest.html. But since you are reading this I will start assuming you already have it installed. The "#" sign means comment. Anything proceding a # will not be compiled by perl. This is true everywhere except in the first line. So when you write your scripts from reading this text you can leave out all the # lines except the first one. The first line of EVERY program must be #!/usr/bin/perl (or where ever your version of Perl is located). When you are writing/testing your programs you should but #!/usr/bin/perl -w This will describe bugs in your programs pretty detailed. Now you say "OK but how do I write a Perl program?" Well you write it in your favorite text editor, or even WP just save it as ascii text. Now lets start with some basics. VARIABLES: In every language you need to have variable, special things that hold a value. Also, after you write a line that does something you must put a semi-colon. For example: #!/usr/bin/perl -w $name = "bob"; In this example the variable $name equals bob. So now when ever I write $name in the program it really means bob. For example: #!/usr/bin/perl -w $name = "bob"; print $name; In this example the word "bob" will appear on your screen. This also shows the very basic "print" command. To make this run you now need to tpye at the command prompt: Chmod +x filename This makes the file executable. Now type ./filename the response should be bob[user@host dir] But lets say that you want "bob" and your prompt on different line, then change the code to: #!/usr/bin/perl -w $name = "bob\n"; print $name; The "\n" means newline. It breaks the line after printing "bob". But now lets say you don't know what the guys name is. You need him to input his name. To do that you use the <STDIN> construct. This is the most basic form of input. For example: #!/usr/bin/perl -w Print "Please enter your name\n"; #make the variable $name equal what the guy types $name = <STDIN>; #<STDIN> automatically adds a new line so the chomp #function chomps off the newline Chomp ($name); #This will print "thank you "his name" (then new line)" Print "Thank you $name\n"; See this is real easy. It gets harder but not much, perl is a very easy to learn and use language but is still very powerful. Now lets say you want to save a file with the guys name in it. You need to create a file and them save the text entered. #!/usr/bin/perl print "Please enter your name\n"; $name = <STDIN>; #create a filehandle called NAMES to use like a variable for #the file in the same directory called ".names" the ">" #before .names creates the file. The 2 pipes(||) then the #die command tells it what to do if there is an error. open (NAMES, ">.names") || die "Can't open .names"; #prints $name into the file now called NAMES print NAMES $name; close (NAMES); Now each time you run this it will make a file and save your name to it. But lets say you wanted it to keep adding names. So you would know who had used it? Now simply change the open line to: Open (NAMES, ">>.names") || die "Can't open .names"; The >> adds to instead of overwrites. See this is too easy! 3: Search Patterns: No lets say you wanted to be able to search the file and see if joe has entered his name. Then you need to write a program to do something like this...(latter we will combine them) #!/usr/bin/perl -w #you don't need a < or << here because you just want to read #from the file. open (WHO, ".names") || die "Couldn't open .names"; # the while part basically means while inside the file #WHO(which now equals .names) While ( #this is a basic if statement. It says if anything #inside(the slashes mean anything) equal joe than do #the next thing. This means they guy could have put in #joey or joe and it would show up. If you just wanted #it to look for "joe" then you would omit the "//'s" if (/joe/) { print "\nJoe has used the other program\n"; } } close (WHO); Now lets say you wanted to see if there was a name in the file called "joe.bi Change the if code to If (/joe\.bi\ Whoa! That looks weird...I'll tell you what's going on real slow. First the "if" then the "/" to indicate anywhere not exact, then "joe" then "\." because the "." has a special meaning in Perl (I'll tell you later) so you have to put a "\" before it so it will interpret "." as a period and nota command. Then you have "bi" then "\>" for the same reason..."<" is a special Perl character so "\" is needed before it. Then "kle" then "/" to end the pattern. It seams really complicated but once broken down it is really easy. If you want more info on patterns its coming in the next series. Patterns are at the heart of Perl and I want to make sure you totally understand this stuff before moving on. 4: Modules: Modules are a Perl programmers best friend! They are basically scripts that automate many of the things you want to do in Perl. You must load them on your comp for them to work. For example, it would be really hard(or at least long) to use Perl to automatically FTP into a server, login in with username and password, then do stuff. But with the Net::FTP module it becomes a real easy task. Just watch: Use Net::FTP; $ftp = Net::FTP->new("host.com"); $ftp->login("username", password"); $ftp->put("file", "path/file"); $ftp->quit; This section logs onto the host, enters name and password, then uploads a given file onto the server, then disconnects. We will use this later in the series but you get the idea now on what modules are and how to use them. I will go into way more detail later, I just wanted you to get a feel of what they were. 5: Where to get more info: The most in depth information can be found right on your computer. Use the man pages. The man pages have info on every topic. Also check out comp.lang.perl.misc but don't post. It is for fairly advanced users and if you post a poorly written question you will only get flamed. These pages provide a little more detialed information that what you have read so far. Check out www.perl.org www.perl.com http://language.perl.com/info/documentation.html http://reference.perl.com/query.cgi?tutorials http://www.hut.fi/~jkorpela/perl/intro.html These sites have good info plus a ton of links so you can now spend years reading about Perl. If you have any questions or comments about this document please send mail to: bxtc@forfree.at Have fun, (BXTC) ICQ# 23289202 |