What Is $this

Here are a few things you need to know about programming in Perl.
For a more in depth look at these and many more Perl features please see Beginner's Introduction to Perl.

Variables

All computer programming languages use variables. They are used as a temporary place to store information. If you have never programmed before you can think of a variable as like a peace of paper that you can write some information on. You can write it using a pencil and change it later or you can write it using a pen and make the information pertinent.

There are many types of variables. Some you create as you go and others are given to you when your program is run. Some hold numbers and some hold text or letters also known as strings. Lets look at some important things to know when working with variables.

this A very special variable it holds the identity or memory location of the object currently being talked about. If you think of the object as a person it would be like the object was saying I as in I am doing this or I have this. Every object has a this variable and uses it to refer to itself just like you use the word I to refer to your self.
$ Local variable identifier operator. Perl uses this symbol to maker an identifier as a local variable. In both Perl and C++ all variables have to be identified as being a variable. You tell the computer program that this special word is what you want to use as a variable. In C++ you tell the program it will use this special word when you very first use the word int A; In Perl you use a special operator like $ or @ or maybe ->{ } depending on the type of data you are keeping in the variable and you use these special operators every time. See also PerlQt vs Qt C++
@ Array of variables or a stack of variables. If you think of a variable as a page then an array is like a book of pages. Each variable in the array is same type, like a list of names or a list of phone numbers. When you use @ to create, append or read values from an array you may need to know how many items are in the array and this is done by using $#YOURVARNAME where YOURVARNAME is the name of your variable like so @Staff = ("Kim", "Tom", "Danny", "Linda", "Roger"); and to get the number of names in the array you would use $#Staff

Key words

In programming some words have special meaning. It can be hard to figure out what is going on if you don't know what these words mean. It is as if people that write computer code are talking in code. Lets look at decoding some of that jargon.

Terminology

function A group of instructions that are together in a block. They are processed in order.
object , class The words object and class are almost the same when talking about code. You can use them interchangeably. They basically mean a thing. In computer programming a class is a thing that has variables and functions that related to each other.
member A thing that is part of an object. It could be a variable or a function but it is a part of the object.
widget A graphical object like a button or text box.
variable A memory location you can put information in to be used later.
stack, array A bunch of memory that you use to put multiple variables in and the variables are all a like.
block A grouping of instructions contained inside a function.
sub block A grouping of instructions contained inside another block.

The Qt library has many objects and function members. These are the power behind PerlQt. You should look over the Qt objects documentation to see what objects your program needs and what function members you need to use.

Built in functions as operators

These are commands that are named and work like operators to do special things. These are reserved words and you cannot name variables to be the same as these.

sub Perl uses sub to identify the start of a new function.
shift Pulls the first variable off the top of a stack and gives it to you. For example $A = shift;
return In both Perl and C++ this causes a function to send back the data of a variable, send it back to the function that called this function. This is the last line and the function exits.
if Starts a comparison operation to test a value. If true the sub block flowing the if is executed.
while, for These are the start of loops that will repeatedly run a block of instructions until a condition is met that ends the loop.

Operators

All programming languages have sepcial built-in commands that do things for you to make programming easier. Perl and C++ share a lot of these operators in common as do many programming languages that came after the success of the C programming language. Most of the C++ operators work in Perl and you can find a more complete list here and there is more information about Perl operators on the Perl documentation site.

C++ style operators that also work in Perl

= Assignment: Sets a variable to a given value. For example A = 6 would make the variable A have a value of 6 stored inside it.
+ Assignment addition: Sets a variable to a given value by performing addition. For example A = 3 + 6 would make the variable A have a value of 9 stored in side it.
- Assignment subtraction: Sets a variable to a given value by performing subtraction. For example A = 21 - 9 would make the variable A have a value of 12 stored in side it.
* Assignment multiplication: Sets a variable to a given value by performing multiplication.
/ Assignment devision: Sets a variable to a given value by performing devision.
% Assignment modulus: Sets a variable to a given value by performing devision and then taking the remainder as the answer. For example A = 10 % 3 and we know that 10 devieded by 3 leaves a remainder of 1 so A have a value of 1 stored in side it.
++ Assignment Increment: Adds one the the number.
== Conditional equal: Asks if the value on the left is equal to the value on the right and returns a TRUE or FALSE result. In Perl this is used for numbers and variables that contain numbers.
> Conditional greater then: Asks if the value on the left is greater then the value on the right and returns a TRUE or FALSE result.
< Conditional less then: Asks if the value on the left is less then the value on the right and returns a TRUE or FALSE result.
>= Conditional greater then or equal to: Asks if the value on the left is greater then or equal to the value on the right and returns a TRUE or FALSE result.
< Conditional less then or equal to: Asks if the value on the left is less then or equal to the value on the right and returns a TRUE or FALSE result.
{ Start of a block.
} End of a block.

Perl operators that are also used in PerlQt

Some operators are used in Perl in a special way and do not work in programs written in C++. These special operators make it easy for you to do some very cool things in Perl that are normally much harder to do in C++ .

=~ Binding: Changes a string according to the next function. A common example is =~ s/A/B/ is when you have a string and you want to match some text represented by A and replace it by some other text represend by B
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License