Page 1 of 1

Perl Basics Output and Variables Rate Topic: -----

#1 sharkbate24  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 14-April 09

Posted 22 July 2009 - 12:38 PM

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:

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:

#!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:

#!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:

# 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.

#!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 July 2009 - 06:18 PM


Is This A Good Question/Topic? 1
  • +

Replies To: Perl Basics

#2 mattman059  Icon User is offline

  • Epic Awesomeness
  • member icon

Reputation: 15
  • View blog
  • Posts: 538
  • Joined: 23-October 06

Posted 08 September 2009 - 10:56 AM

I believe it's "SHABANG" :P getting a little william hung on us huh.
Was This Post Helpful? 0
  • +
  • -

#3 auggiecc87  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 09-March 09

Posted 13 September 2010 - 08:09 AM

Although Perl is a little bit of a hack language in my opinion. My job has had me coding in Perl for about a year now. So I am working on posting some more tutorials as DIC should support the language with more tutorials.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1