Friday, October 7, 2011

Scripting in Perl! [Linux/Windows]

So currently at school I'm taking a Scripting in Perl class, and I'm in absolute LOVE with this language. It's easy to understand, has very good English-like syntax, simple array and hash usage, built in BASH support (for all you Linux freaks!), easy GUI creation, and so many other things that we haven't even gotten into.
I'll be posting examples based upon things in my lab and lecture, including full programs, certain syntax, and other cool things. My teacher is very good and explains many things, so you have him at your disposal (meaning, ask me a question I don't know and I'll ask him, learn it, then explain it back to you!).

Lets get started with basic syntax then get into all the fun stuff.

Lets go over variables really quick. First of all, variables are declared or initialized with the "$" character. In C++ or Java we say "int variable = 0" or the such, but in Perl it's unique.
In Perl, we initialize numeric variables with the command:
my $variable = 0;
which would mean we create the variable "variable" and set it equal to zero. To just declare it it's as simple as just saying "my $variable"

Now you might be confused with the "my" command. This deals with the SCOPE of the variable. Many of you probably know what "scope" means, but if you don't, here's a quick definition.
The "scope" of a variable can be thought of as the lifetime of that variable, or where it can be referenced in your code. If you declare a variable in one block of code (say, a for loop), but try to change it outside of it, Perl won't allow it since it can't "see" the variable because it's "dead" or out of our scope!
I'm sure you'll all understand the "my" command soon enough, it's pretty simple and doesn't require much thought; just make sure you use it when declaring and initializing variables for the first time.

Now, remember how I said we initialize "numeric variables" with the above command? Well, you might be surprised to hear that numeric variables and strings are created the same way!
Here's how we create a simple string:
my $string = "this is some text to equal string";
and it's as simple as that.

Perl looks at the value of the variable rather than the type, so you don't have to say "double" or "int" or "string"!

Lets talk a but more about variables in Perl. Every PERL variable has three components: The name, address, and value.

Take this line of code for instance:
my $pet = "Noah";
The name is "pet", the address in memory is hidden right now, and the value is "Noah".

The address (called a reference as well) can be shown by a "scalar" and placed into another variable:
my $pet_ref = \$pet;

Note the \$...
Like in C++, this is the REFERENCE (shown in C++ by the asterisk *); it references the address in memory.

The following line of code prints the variable that we placed the reference of $pet into:
print $pet_ref;
...which would be the memory location!

Open for answer!:
Ignore this for now, it'll be used for asking questions and checking code I ask you to write!


  1. it's about time someone posted a easy to understand pearl tutorial

  2. Dude you are awesome, good luck in your studies and learn all you can kind sir.

  3. I've been wanting to learn perl.

  4. I had been involved in c++ programming classes many years ago, and I learned well then, but now how many years have I forgotten how much time does it take to train again?
    تابلو چلنيوم
    تابلو مغازه


  5. شركة نقل اثاث من الرياض الى الامارات
    لاننا افضل شركات شحن الاثاث فدائما خدماتنا هى الافضل
    شركة شحن عفش من الرياض الى الاردن

    اتصل بنا الان للحصول على افضل الخدمات لاننا الافضل دائما ونعمل على الاستعانة بافضل العمالة المدربة على نقل العفش و فك وتغليف الاثاث لاننا افضل و
    ارخص شركة نقل عفش بجدة

  6. A pesar de la alta probabilidad de encontrar algo que ames aquí, identificar esta combinación perfecta no es una tarea fácil,replicas de relojes y ahí es donde realmente brilla Watches of Switzerland. Identificar de manera rápida y precisa las preferencias personales de uno requiere una inmensa experiencia y talento, mientras que combinar dichas preferencias con una de las muchas piezas disponibles requiere una comprensión profunda y exhaustiva de un vasto inventario.replicas relojes Se requiere que cada miembro del personal de Watches of Switzerland posea estas habilidades raras y conocimiento académico, lo que constituye otra razón importante por la que los compradores de relojes confían en su asistencia a la hora de elegir uno.