Tuesday, 21 January 2014

my $scalar;

In most of the books I'm using, the first thing after all the set up and hello world stuff is scalars. This seems to be sensible to me because they're quite simple for a beginner to understand and seemingly fundamental to the Perl language.

From what I understand, scalars make up any singular piece of data - it's when Perl has one of something and often this will be a number or a string but not an array or hash or other complex data type. You can identify a scalar variable because it's prefixed by a $ sign so if you see a variable named:
"$something" 
you know that it contains a singular piece of data.


Unlike other programming languages (Java), Perl is not strongly typed and is intelligent enough to know that when you have typed a string in quotes, you want it to behave like a string and when you type a number, you want it to behave like a number.

This means there is no faffing around telling the interpreter what kind of variable you want when it can work it out for itself. All you need to do is tell it what you want the variable to be called and what you want in that variable. Simple

There are ways of giving the variable a number but wanting it to behave like a string and vice versa but these have to explicitly be done. The default is to treat whatever it is as what it looks like - which sounds very sensible to me but I think there could be some potential problems with this so I guess it needs to be kept in mind.



Declaring and assigning a scalar

There are two ways to declare and assign a scalar variable; you can either do it all in one go:

my $name = "Emma";

or declare and assign separately:

my $name;                    #This is declaring the scalar
$name = "Emma;"        #This is assigning it

This has basically said that there is a scalar variable called "name" and the value of this scalar is the string "Emma". The quotes just denote the beginning and the end of the string and won't actually be printed out. If you want to assign a number to a variable, you don't need to put the quotes but more on that later.


If we leave 'my' out and we have use strict turned on (which you should have!!), the program will fall to bits when you try to run it and complain at you with something that looks like this:

Global symbol "$name" requires explicit package name at declaration.pl line 5.
Execution of declaration.pl aborted due to compilation errors.

Not the friendliest of error messages, but I have seen worse. This error message could also mean that you've made a typo when writing a variable name. This will mean having different spellings between the declaration and any usage of it, which will make the interpreter cry.


So, now we know how to declare a scalar, the next question is what exactly can we store in these scalars? Posts to follow on numbers and strings...



5 comments:

  1. Interesting blog. It's good to see a newbie's viewpoint on perl. I'll point students learning perl to it. Keep up the good work!

    ReplyDelete
  2. "The default is to treat whatever it is as what it looks like - which sounds very sensible to me but I think there could be some potential problems with this so I guess it needs to be kept in mind."

    Perl doesn't so much treat the scalar as what it looks like, but rather as you use it. The difference between Perl and other languages, it is often said, is that Perl is more concerned with verbs than nouns. If you join two scalar with + it does so as numbers, if you join them with . it does so as strings.

    This is also a major reason that warnings is so important; it will warn when it thinks you have used an incompatible operation, like adding two strings that do not look like numbers.

    Happy Perling!

    ReplyDelete
  3. This is a good start. Please note that there are other data types in Perl that could be prefixed with a '$'. For instance references:

    my $person = {
    name => 'fred',
    pet => 'dino',
    };

    This declares a reference to a hash (aka hashref).

    ReplyDelete
    Replies
    1. Hm, that part also caught my eye, but the way Emma described it (purposefully?) can also support refs - you could say a hashref contains/is a singular piece of information: a single reference to a whatever-it-is-referencing. :)

      Delete