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