Thursday 16 January 2014

Hello World! - writing and running my first print program

Hmmm what kind of program shall I write first? I wonder if there are any easy start up programs. You know what's coming up...

1.   #!/usr/local/bin/perl
2. use strict;
3. use warnings;
4.
5. print "Hello World!\n"

and that's it. Really, nothing more. I can now go and run it.

How do I run it?
Easy, there are two different ways you can run a perl script.

1. type "perl filename" into the command line, replacing "filename" with the name of your script including the extension.

2. make the file executable by typing "chmod +x filename" into the command line, again replacing filename with the name of your script including the extension. This command has changed the access permissions to the file and the "+x" means that we have added execution rights for all users. 
We can now run the file in the command line by typing "./filename".

This now appears on the terminal:

Hello World!

What have I actually achieved?
This is of course the infamous Hello World script, which is a rite of passage for every programmer learning a new language. This very small 4/5 line script (have you seen this, Java?) has allowed me to print the words in quotes to the console. Print takes a list so you can either give it one thing to print (some text in quotes) or a list of things to print (more than one set of text in quotes separated by a comma). It then stringifies (as it sounds, turns into a string)* the items passed to it, in this case "Hello World!\n". Then this string (or list of strings) is printed to the standard output channel, which I will explain below.

*not entirely sure how this works but I don't think this post is the place to delve into it. I will do a separate stringy post later on.

Output Channels
When we use the word "print" followed by whatever it is we want to print, it is actually as if we have written "print STDOUT "some text"". STDOUT means that the standard output channel is being used and since this is the default, we don't have to explicitly write it. The standard output channel is directed by default to the terminal screen but this can be changed to a file. There is also another output channel called STDERR, usually reserved for error information, which also is defaulted to go to the terminal screen. This time however, you have to use the whole term "print STDERR" if you want to print to this channel. You will not be able to tell the difference by looking at the output on the screen which text is from the standard output and which is from the standard error channel. To separate these, you can direct one or both of them to separate files.  

This is a link to the print section of the perldocs, if you want to read about it in more detail. I'm personally not advanced enough to understand it all yet.
http://perldoc.perl.org/functions/print.html

\n                                      
The \n is an indicator to start a new line so if I wrote another print statement in the script, it would appear underneath "Hello World!" in the console.
Note that double quotes must be used for the \n to be interpreted as a newline character. Single quotes would print the whole string literally and "Hello World!\n" would be printed to the console.

2 comments:

  1. You can also enable "say" in a variety of ways: Declare that you're writing for a recent version of Perl ( use v5.12; ) or specifically import it ( use feature 'say'; ) or use a module that will import it for you ( use Modern::Perl; ) etc, and then instead do:

    say 'Hello World!';

    and get the same output.

    If you use "perl -e" much, upper-casing the 'e' also gives you 'say', so:
    perl -E 'say "Hello World"'
    works. It's such a trivial feature but once you're used to it, it's hard to live without.

    ReplyDelete