Monday 13 January 2014

Use Statements and the Whole Shebang

See what I did with the title??.....
Nearly 100% I'm not the first to come up with that.
The shebang and use statements are things that we all  put at the top of our perl scripts but do we really know why? Maybe a lot of you do, but I've just been blindly copying and pasting them because I know that my scripts won't work properly without them and it's the right thing to do.
Now I'm actually going to explore what they are for.

Shebang
As far as I can tell shebang is actually a slang word, because why would any normal person come up with that as an official term?
It consists of the characters "#!" and then the path to the interpreter program so the complete shebang line will look similar to the line below:

#!/usr/local/bin/perl

In fact, in all of the perl code I've seen, which admittedly isn't very much, the shebang has had the exact format as above.

I've done a little digging to find out what all this actually means.
When a script with a shebang is run as a program, the program loader (a part of the operating system that is one of the essential stages in the process of starting a program) sees the "#!" characters and then parses the rest of the line as an interpreter directive. What does this mean? The interpreter is the thing that executes the script and is specific for each programming language, but the script could be written in any language so we need to know which interpreter is needed. The path points to the location of the interpreter, in this case, the perl interpreter. This means that the Shebang isn't only used in perl, it's used in python, ruby, PHP and other scripting languages.

Use Statements - also called "Pragmas"

The most common use statements I've seen are:

use strict;
use warnings;
use Modern::Perl 2011;

The last one actually implies use strict and use warnings so you can use it by itself and not have to go through all the bother of writing them separately. But what do they actually mean?

use strict;
This pragma activates three different pragmas:

  • use strict 'vars'; - this complains when you try to use a variable that you have not previously declared,
  • use strict 'refs'; - prevents you from using symbolic references*,
  • use strict 'subs'; - this stops you from using barewords inappropriately, these are words that appear on their own without quotes or other punctuation. For example, with use strict 'subs' activated, my $x = hello; will not be allowed because hello needs to be in quotes.
*I've been looking for ages and I have absolutely no idea what this means! I cannot figure it out. All the explanations I can find just confuse me and talk about concepts that I have no idea about. I guess when I get that far I can come back and fill in this bit.

You can deactivate any of these individual strict pragmas by using "no" rather than "use".

use warnings;
This gives all kinds of debug information, for example typos. It will stop the program from running and tell you the things that you would expect any IDE to. The only thing is that it will only give you one error at a time so you may need to run the script several times to be able to find all the bugs.

You can also activate warnings by typing "-w" in the command line when you run the script.


Both the strict and warning pragmas are very useful to have on your perl scripts so it's advisable to include them at the beginning of the script. Alternatively, you can just write "use Modern::Perl 2011;".

4 comments:

  1. "As far as I can tell shebang is actually a slang word, because why would any normal person come up with that as an official term?"

    It's a contraction of "hash bang". Because the it starts with a hash (#) followed by a bang (!). It comes from the US where they apparently get confused by the term "exclamation mark".

    "Symbolic reference" means "using a variable as a variables name". For example:

    $var_name = 'emmas_variable';
    $$var_name = 'Perl'; # Note the two dollar signs

    This has the effect of creating a variable called $emmas_variable and setting its value to 'Perl'.

    Beginner programmers often think that this is a cool feature. When really, it's horrible. Mark Dominus has a great explanation of the problems it causes.

    http://perl.plover.com/varvarname.html

    ReplyDelete
    Replies
    1. > It comes from the US where they apparently get confused by the term "exclamation mark".

      It's not confusion so much as laziness. Why say five syllables when you can skate by with only one? :-)

      Delete
  2. Have you considered signing this blog up to http://ironman.enlightenedperl.org/ ? It's a good way to get your posts seen by a wider audience of Perl types

    ReplyDelete
  3. My understanding is shebang is a contraction of "sharp bang" back when # was called sharp, as in music.

    ReplyDelete