Here is where it starts getting interesting. One thing I overlooked that turned out to be a problem was teaching them how to use the command prompt. The IT teachers didn't know either. Seems Windows has prevailed in restricting command prompt knowledge to us IT pros :(
Remember I'm trying to keep this simple, some of the kids will only be 13. Although saying that I'm currently working on a follow on course. So notes on all the things/changes you think should be in this version and all the things you think I should teach in the next would be ideal ;) The formatting used to make this easier to read is of course lost in plain text. Each Chapter from herein roughly fits into an hour of teaching. I got them typing things in for each section after hello world. Next time I'll be more diligent to make sure they are actually understanding and playing with the code rather than solitaire (why is that still on the school pc's????). Some of the kids were treating it as quickly copy what I've put on the board then start messing about, where they should have been messing with the code and trying to understand it. Chapter 3: Programming in Perl This course is short so it's structured to be as hands on as possible. But before we can write any code, you need to understand a few basics. Comments To make the examples clearer we use comment tags. Anything on a line after a #, is a comment and ignored by Perl (this isn't always true). #This is a comment Outputting data At this point we'll cover basic data output using print, so that you can get hands on a writing some working code. The print statement is used in many programming languages as the means of outputting data to a variety of devices, such as your HD in the form of a file, or text onto your screen. print 'Hello world!'; Line endings So that long lines of code can be easily broken over several lines, or short lines of code can be put on the same line, lines of Perl code must end with a semi colon ';'. As with comments there are of course exceptions. Running Perl scripts Perl scripts are just text files that contain Perl code. They are most commonly saved with the file extension “.pl”. These scripts can be ran along the command line, using the Perl interpreter. Let's use the above print example as our first program. Open a text editor such as notepad, word processors such as Word and WordPad are not suitable as they try to format the text. Perl scripts should always start with the path to Perl. This is known as the shebang line. In these exercises we'll be invoking Perl directly so it's not technically needed, but it is good practice to put it in anyway. In your text editor type:- #!c:/perl/bin/perl.exe print 'Hello world!'; Save this as hello.pl within a suitable folder (such as “c:\perlscripts”). You'll see that the path to Perl uses forward slashes / as opposed to backslashes \. That is because Perl was originally developed for Unix based systems which use the forward slash for directory paths. Now open a command prompt (usually Start->Accessories->Command Prompt), navigate to the folder where you saved the hello.pl script (“cd \perlscripts”) and run the script with the Perl interpreter (“perl hello.pl”). You should get the output “Hello World!”. Variables Data that you are currently working with is stored in variables. Think of variables as labelled containers. There are different kinds of variables for storing different kinds of data. For example to define a variable in C you need to declare what type it is e.g. Number = int Letter = char Word = varchar Luckily Perl's variables aren't as strictly typed. Scalars In Perl integers, strings, etc, do not have to be defined separately and are more simply defined as Scalars e.g. $number = 10; $char = 'c'; $huge = 'pages of information'; Scalars are denoted by the $ sigil. Perl has variables that can be global; allowing all parts of the program to access them, or local; allowing only certain blocks of code access them. A global variable is defined as our $name = 'xyx'; Whereas a local variable is my $name = 'xyz'; It's good practice to have all or most your variables as local variables. Scalars that have been initialized but not assigned a value have a special value known as NULL. Perl calls NULL undef, for undefined. undef is not the same as an empty string. my $scalar; # $scalar will contain undef my $empty = ''; # $empty will contain an empty string my $undef = undef; # $undef will contain undef † The 'our' syntax for defining global variables was introduced with Perl v5.6. Some people still prefer the old method of declaring globals with: use vars qw( $LIST @OF %GLOBALS ); Using Scalars Let's update our hello.pl script to use a scalar to contain the message:- #!c:/perl/bin/perl.exe my $message = 'Hello world!'; print $message; Working with Scalars There are different ways of working with Scalars. Perl is well known for TIMTOWTDO (There is more than one way to do it). Here are some examples of common things you'll want to do with Scalars. Interpolation Our previous hello.pl example uses single quotes '. Single quotes do not interpolate Scalars. Such that the code my $name = 'Lyle'; print 'Hello $name'; Would output the text “Hello $name”. Double quotes do interpolate, such that the same code with double quotes my $name = 'Lyle'; print “Hello $name”; Would give you “Hello Lyle”. Joining strings Strings can be joined with double quotes, the join function, or by using the dot '.' operator. my $start = 'Hello'; my $end = 'World'; my $joined = “$start $end”; # Same $joined = join( ' ', $start, $end ); # Same $joined = $start . ' ' . $end; # Same Repeating strings You can also repeat strings with the x operator. print 'Hi!' x 5; Would display “Hi!Hi!Hi!Hi!Hi!”. Working with numbers You have all the normal math operators. Examples:- print 1 + 1; # Displays 2 my $multi = 5 * 5; # $multi contains 25 my $div = 5 / 5; # $div contains 1 Lyle _______________________________________________ BristolBathPM mailing list [email protected] http://mailman.bristolbath.org/mailman/listinfo/bristolbathpm
