School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,213 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,552 people online right now. Registration is fast and FREE... Join Now!




Perl Basics

 
Reply to this topicStart new topic

> Perl Basics, Output and Variables

sharkbate24
Group Icon



post 22 Jul, 2009 - 11:38 AM
Post #1


I did originally accidentally post this in the Perl help forum, apologies about this.

Perl is said to be a very good language for beginners. The syntax is very easy for humans to understand, and best of all, it's easy to migrate to another programming language from Perl as the syntax is similar to those in C++, C# etc.

This tutorial is to teach the basics of Perl for people new to programming running the Windows Operating System.

Requirements:
- You will need a simple text editor such as notepad. Do not use a word processor such as Microsoft Word etc as these can add special characters to your script, making them un-runnable.

- You will also need the perl interpreter. Luckily, there are packages for Windows such as ActivePerl, Strawberry Perl etc. I recommend ActivePerl. You can download ActivePerl from the following location: http://www.activestate.com/activeperl/.

How to Run Perl Programs:
To run Perl programs, you need to open the command prompt and type in the following:

CODE

perl FILENAME.pl


You need to save Perl programs with the .PL extention.

Your First Program:
Open up your text editor, and type in the following code:

CODE

#!c:/perl/bin/perl.exe
print "Hello World!";


The first line is called the SHEBANG line. This tells the computer the location of the perl interpreter. It's not really needed to run programs in Windows, but when you run programs in linux, you must have it - especially for web applications. Although it's not needed on windows, it's always good practice to include it.

Notice the "#" in the start of the shebang line. This makes the line be known as a comment which is ignored by the interpreter, however, this isn't always the case, such as with the shebang line (otherwise the whole perl location would just be ignored).

Also notice how "Perl" means the language, and "perl" means the interpreter.

The second line outputs text to the screen. Anything inside a " " is known as a string (basically text). When you output numbers, you don't need to put them inside quotation marks:

CODE

#!c:/perl/bin/perl.exe
print 5;


The script above will output 5 to the user - notice how it works without the quotation marks because it's a number and not a string (text). But if you put text outside quotation marks, perl will think it's a function or command etc, and won't work.

Also note that semi colons go on the end of every line, but this is not always the case.

Variables:
What good is memory if there is nothing to remember? Variables are data, and are stored in the memory for future usage. Think of variables as algebra. You can also think of the variable as a box with a label, and the value of the variable is what is inside the box. You can store numbers, strings etc in a variable.

The main variable type in perl is called a scalar, which is declared in the following way:

CODE

# CREATE A SCALAR
$variable_name = VALUE;
# THIS IS A COMMENT AND IS IGNORED


With scalars, you don't need to tell perl the type of data it will store, so the 'box' is open to anything. You can stick text in there, numbers etc.

CODE

#!c:/perl/bin/perl.exe
$my_variable = 5;
print $my_variable;
# The code above will output 5
print "$my_variable";
# The code above will output 5
print '$my_variable';
# The code above will output $my_variable


Hope this tutorial helps,
Thanks and good luck with Perl!

Recommended to learn next:
- Conditional Logic,
- Arrays and Hashes
- Loops
- Input

Remember, practice makes perfect (especially when it comes to programming)!

This post has been edited by sharkbate24: 23 Jul, 2009 - 05:18 PM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

mattman059
Group Icon



post 8 Sep, 2009 - 09:56 AM
Post #2
I believe it's "SHABANG" tongue.gif getting a little william hung on us huh.
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 09:56PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month