Jump to content




Recent Status Updates

  • Photo
      30 Sep
    rhossis

    laptop hard disk seated beneath motherboard but with no access panel. 7 hours to replace :(

    Show comments (3)
  • Photo
      19 Sep
    Chall

    I love it when you go to write a help thread, then while writing, you reach an enlightenment, and figure it out yourself.

    Show comments (3)
View All Updates

Developed by Kemal Taskin
Photo

The basics of Perl

case sensitive

  • Please log in to reply
10 replies to this topic

#1 sfoulk526

sfoulk526

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 21 March 2010 - 09:59 PM

***Let’s Get Started***

Hello and welcome. Today we’re going to learn some basics of Perl programming. I was going to treat you to the standard “Hello World” program, as is the Standard Operating Procedure for most programming languages, but I just couldn’t do it without pain. Instead, we’ll get to the programming part as quickly as possible.
I want to set some ground rules first, then get you set up with Perl on your computer, then begin coding.

***Ground Rules***
Rule I.
Assumptions as to your skill levels and navigability around your native operating system.
A. You should be comfortable with your operating systems version of a command shell.
B. You should be able to change directories.
C. You should be able to create directories, and directory structures.
D. You should have at a minimum ONE text editor on your system, and know how to use it. If you are not sure, then you’d better ask a friend. EVERY OS has at least 1 text editor. Examples are: Windows (notepad), Linux / Unix (pico). Better yet, if you have an IDE (Interactive Development Environment) and know how to use it for Perl, excellent!
E. You should be able to install software from the Internet, because there are waaaay too many cool tools for Perl out there. Yeah! (We’ll get to those later)

Rule II.
If you need help, ask for help.
Just do it. I’m not writing for Computer Science Ph.D.s, I’m writing for any people out there who want to learn Perl. A college education is not a prerequisite to being able to program. (It is usually required by employers, however.)

Rule III.
If you see a typo, or anything that looks wrong, omitted, misstated, or seems like it’s generally just not right, let me know. There’s an email address below, and I read my emails at least once a day.

Rule IV.
This is a Perl community. Our Perl community. Please participate and interact freely. You don’t even need to raise your hand!

***Get Perl***

You will need a directory where you keep your perl programs that we will practice with. Preferably, you should create a new directory. Do that now, and make sure you can remember how to get there. When you save a Perl program, save it in this directory unless otherwise directed.

Next, let’s set up your programming environment. If you are setup already, go to the ***Let’s Code!*** section below. Otherwise, Mac and Linux users, you probably already have Perl installed. Go to a command prompt and type:

perl -v

If you get a message that starts with ‘This is perl’, you have Perl installed. Jump to the ***Let’s Code!*** section.

If you do not have Perl, Linux users can go to their software updaters included with their version of the OS and get the latest Perl software.

Mac users can go to the following link:

ActivePerl Downloads

You should have Mac OSX v10.4 to use Active Perl. By the way, Active Perl is a commercially supported version of Perl, so if you’re willing to pay, this may be key for you. I would wait until you’re ready for some professional development, which probably won’t be this week.

For Windows users, open your browser, and go to this URL:

Strawberry Perl for Windows

See the largish picture of the strawberry? Good - you’re there. Now click on the DOWNLOAD link on the bottom right corner and begin the download of Perl. Install with all the defaults.

Now you’re ready to code. And instead of leaving you with a simple “Hello World” program until our next regularly scheduled program, I’ll be giving you a couple of small, but different varieties of useful, simple combinations of commands.

***Let’s Code!***

In programming, the best way to learn anything is the same as in your life:
(1) Practice - lots!
and
(2) Learn from your mistakes - you will make them. Treat them with respect, and they’ll turn you into a great programmer.

Let's begin with our first set of programs.

Start your editor. Btw, Perl is case sensitive. print does not equal PRINT or Print or pRInt. Type exactly what you see. Computers do not know what you meant to say. They know only what you did say.

#!c:/strawberry/perl/bin/perl.exe
# Start here
print “Here we go on our journey ”;
print “to become a decent Perl programmer.\n”;
print “You will learn not only to be a good Perl programmer…”;
print “but how to be a good programmer no matter what language you code in.\n”;
# Some important points…
print “Practice…tons\n. “;
print “And feel free to experiment…try any variations you want.  Predict “;
print “what will happen, and see what actually does happen.\n”;
# And what is this line?
print “\tDid you notice anything happen in this line?\n\n\n\n”;
print “Is it \tsubtle? \Uno, maybe you did not.  Well\t lets try\E “;
print “to make it clearer.  What are the slashes doing?”;
print “I will leave it to you.\n”;


Save this program as:

perl1.pl

Let’s look over our first program.

The first line begins with the # character. We’ll call it the ‘pound sign,’ cause that's what everyone calls it. Usually, the pound sign denotes that anything after it will be ignored by the Perl compiler. This means that all the text until the end of the line is not compiled, and is called a COMMENT. COMMENT’s are used to make notes in your program, and tell the reader/programmer exactly what the program is doing. COMMENTs are notes for you or your replacement to come back to in a few minutes, months or years. COMMENTs are important for the maintenance and upkeep of programs. They can make someone’s, or your, life much easier, and I would highly recommend you COMMENT your code a great deal. Styles of COMMENTs are up to you – feel free to be verbose and accurate in your descriptions.

Back to line 1. Normally this ‘would be’ a COMMENT, but it is actually a directive that tells the Perl interpreter the path to Perl. In Windows, this is not necessary, but in UNIX/Linux, it’s a must. The UNIX/Linux/Macintosh version of this first line might be:

#!/usr/bin/perl

This location varies by operating system. Try whereis on Linux systems to find your Perl interpreter path.

Line 2 is a full-fledged COMMENT. It probably could use more detail in YOUR program, but for here and now it’s an example. The interpreter will bypass everything else on that line from the pound sign onward.

Line 3 is a print statement. print writes everything between the double quotes (“this text is between the double quotes”) to the output device, in this case, the terminal window.

Take a look at all the print statements – notice anything that they all have? They all end with a *semicolon*. (The ‘;’ thingy) The interpreter will flag you with an error if you forget it. The semicolon is the terminator, or the delimiter for the Perl language.

You may have tried another language – Java, C or PHP; all of these delimit using a semicolon. JavaScript optionally uses a semicolon as a delimiter, and many programmers do not use them in JavaScript. But they should.
Semicolons define the end of the line in a program statement, thus making the code easier to read, and that will count a lot when your programs grow larger and more complex.

Line 4 and onward…

Go to your command line in the directory where perl1.pl now resides.

Type:

perl perl1.pl

Then you’ll compare the output with the program we typed in. If there are errors, compare the code I’ve given you with what you typed. Many times Perl will give you a line number where the problem resides. Not always, but often enough to be helpful. make your changes and try again.

The gist of this lesson is that you learned the COMMENT and the print statement, plus the rule for the first line of EVERY Perl program. You’ve also learned that every line that is not a COMMENT must be terminated with the semicolon delimiter (‘;’). If I’m lucky you figured out what the ‘\n’ does. Better yet, what did the ‘\t’ do? Continue on for the answers to these exciting questions!


Let’s enter our second program. It answers some questions from the first program...


#!C:\strawberry\perl\bin
# The slashes denote what is called an escape character
# The escape characters I used are \n, \t, \U, and \E
# Let’s see what they do.
##################################
#  Slash-n 
##################################
print "This is print line one with only a slash n at the end of it.\n";
print "This is print line two with two slash n at the end.\n\n";
print "This is print line three with no slash n at the end.";
print "\n\n\n\nThis is print line four with 4 slash n at the front.";
print "\n\nSlash n (pronounced escape n) performs a carriage feed-line return,";
print "\ntaking your print output to the next line.  Just call it newline!";
print "\n**************************\n\n";
##################################
#  Slash-t
##################################
print "This\tline\tis\tseparated\tquite\ta\tbit.\n";
print "It\tuses\tslash\tt - t\tis\tfor\ttab.\nI know you got it...\nGood job.\n\n";
##################################
#  Slash-U and slash-E
##################################
print "Next, we'll use the slash uppercase U and slash uppercase E to illustrate their functions.\nPay attention!\n";
print "\UWhat does the slash uppercase U do?\E Look at this line in your program.\n";
print "What hap\Upene\Ed?\n";
print "Got it?  Good. Slash uppercase U converts the text that follows it to uppercase.\n";
print "Slash uppercase E then ends the run of capitalization.  That means no more capitalizing unless another slash uppercase U appears.\n";
print "Just call them uppercase and end.\n";
print "Its what they do...\n";
##################################
#  Slash-a – ‘\a’
##################################
print "\a\a\a\a\a\a";
print "So, what did the slash-a do?";



Save this program as:

perl2.pl

Go to your command line in the directory where perl1.pl and perl2.pl now reside.

Type:

perl perl2.pl

Let’s go through some of the major points of this code. The most difficult part to understand is the ‘\U’ / ‘\E’ combination. Notice that I did not use them separately or by themselves. When ‘\U’ occurs in a print statement, every character that is a lowercase letter is made into an UPPERCASE letter. That is until the Perl interpreter encounters the ‘\E’. ‘\E’ tells the interpreter to STOP or END the capitalization that it began when it encountered ‘\U’.

There is another escape (slash) character that I did not mention - ‘\l’. ‘\l’ may appear to be a one digit, but is actually lowercase L (pronounced ‘el’). ‘\l’ tells the Perl interpreter to make every UPPERCASE letter into a lowercase letter, until of course, it meets the ‘\E’. Then all this funny business stops! Or ends. Since it’s E, we’ll go with ‘ends’.

What about the ‘\a’? Did you figure it out? Here’s a hint: beep – 6 times, one for each ‘\a’. You may not have been able to hear this on some Linux systems, but Windows and Mac users will. This is an old, though rarely used BELL feature, but for illustrative purpose (and sentimental value) I have included it.

***The Wrap Up***

Here are some regular and *Error Analysis* challenges to keep you busy:

(1) Read through the code and determine what happens BEFORE running the Perl interpreter, as if YOU are the interpreter?

(2) Type in your own programs and try to leave off the semicolon on a line. What happens? Did the Perl interpreter give you the correct line number?

(3) Try leaving the semicolon off of the very last line of the program. Did it give you an error message?

(4) Capitalize one of the print statements? Check out the error message.

Error analysis is an invaluable skill for a programmer. Understanding the types of error messages generated by the interpreter will be helpful in your future projects because even the simplest errors can cause projects to lose a great deal of time (and money). Do not be dismayed by this 'adversity'. Facing adversity will only make you stronger!

Next time, we’ll begin variables and operations. Thanks for coming by.

See you next time...


Oh yes, my email address: sfoulk526@gmail.com

Edited by Jaan, 23 March 2010 - 06:07 PM.
Fixed title

  • 0

#2 phpforfun

phpforfun

    Speaks fluent binary

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,056 posts

Posted 24 March 2010 - 11:20 AM

So this entire article can be summed up by the "print" function and basic formatting strings?.. Humm....
  • 0

#3 Red_Shadow

Red_Shadow

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 50 posts

Posted 26 March 2010 - 02:26 AM

This is not a **** tutorial. Anyone wanting to actually learn Perl should just go invest in Learning Perl/Intermediate Perl/Advanced Perl (Or just get Programming Perl. :P)
  • 0

#4 sfoulk526

sfoulk526

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 26 March 2010 - 10:42 AM

This is part one. But you're right, they should go buy the books. :cool:
  • 0

#5 James.H

James.H

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 569 posts

Posted 26 March 2010 - 11:15 AM

Looking forward to part 2, will follow this series!
  • 0

#6 phpforfun

phpforfun

    Speaks fluent binary

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,056 posts

Posted 27 March 2010 - 01:12 PM

Im dreading it, Hes going to learn his next function, "if" and "elsif", and write like 30 paragraphs on it, lol
  • 0

#7 James.H

James.H

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 569 posts

Posted 27 March 2010 - 02:54 PM

Well this is the essential basics lol

Do you know enough to write an advanced tut ? I'm a beginner in Perl.
  • 0

#8 phpforfun

phpforfun

    Speaks fluent binary

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,056 posts

Posted 27 March 2010 - 04:02 PM

Are you asking me? or the thread starter
  • 0

#9 James.H

James.H

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 569 posts

Posted 29 March 2010 - 12:05 PM

I was asking you, you sound like you perl quite well.
  • 0

#10 phpforfun

phpforfun

    Speaks fluent binary

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,056 posts

Posted 30 March 2010 - 01:46 PM

Ive only been doing it for like a month, but ive been doing php for such a long time, that it came pretty fast.

Only thing I really had to learn, was using specific objects, like now im learning WMI, I spend more time learning how to use WMI, run WQL statements, and calling to certain namespaces, than I do actually learning perl
  • 0

#11 sfoulk526

sfoulk526

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 08 April 2010 - 04:05 PM

Part 2 is coming soon - I just got a new job and have been very busy. Thanks for the nice and 'other' comments.
  • 0





Also tagged with one or more of these keywords: case sensitive