Hash manipulation: each, delete, exists Before we got side-tracked with subroutines and conditionals, we covered arrays in some detail, and saw the various functions, like push and pop that you can torture them with. The functions for torturing hashes are our next port of call. Perl stores the key => value pairs of a hash …
October 2012 archive
Oct 03
Conditionals
Conditionals: if and unless We’re on the home-straight in understanding the code from the last post now: #!/usr/bin/perl use strict; use warnings; my @peas = qw/chick mushy split/; while ( my $type = pop @peas ) { print “$type peas are “, flavour( $type ), “.\n”; } sub flavour { my $query = shift @_; my …
Oct 03
Bondage, discipline and subroutines
Lexical my variables and use strict; You may have noticed a little thing I slipped in the last script: the keyword my in the chomp. my is a very important keyword, although you’ll note that it doesn’t seem to make any difference if you delete it and run the program. What my does is pin …
Oct 03
Prettier loops and nicer code
Idiomatic Perl The original code we were studying is very ugly, and Perl makes writing short, diabetogenic code easy. It also makes writing shoddy, abominable code easy too, but I wouldn’t recommend this as a course of action. A much more attractive way of writing the horrible code from the last page is: #!/usr/bin/perl print …
Oct 03
Simple loops
Loops Hopefully you’re now happy with the fundamental data types in Perl: scalars, arrays and hashes, so most of the script below shouldn’t be too mysterious: #!/usr/bin/perl print “What is your name?\n”; $name = <STDIN>; chomp $name; @beans = ( “adzuki”, “haricot”, “mung” ); print( “\@beans contains “, scalar @beans, ” members: @beans\n” ); for …
Oct 03
Hashes
Hashes Perl actually has two sorts of array. Simple arrays like those we’ve already discussed are simply called arrays. The second sort of array is called an associative array, or hash. They’re very similar to dictionaries, if you’re a Python programmer. Hashes are created with a % for %we_ran_out_of_sensible_symbols, and contain pairs of data (ah, maybe …
Oct 03
Arrays and slices
Types You should now be able to create very simple scripts that can take input from the keyboard, assign it to scalar variables, and echo it back to the screen with print. Time for some technicalities. Perl is often (and wrongly!) termed a weakly typed language. For those of you coming from bondage and discipline …
Oct 03
Input and output
Input The “not a hello world script” script is a little dull. Let’s try something a more useful: #!/usr/bin/perl print “What is your name?\n”; $name = <STDIN>; chomp( $name ); print “$name likes beans.\n”; Don’t forget the semicolons at the end of each statement. The first line of this program should be obvious now, but …
Oct 03
Educated guesswork
Someone recently asked me, “How many cells are there in the human body?” I have quite a lot of correspondence of this kind, just to be clear. I have to confess my first thought was “Not a bloody clue”. My second thought was “I bet the Internet knows”; but my third was “The Internet is full of …
Oct 03
Not a hello world script
Rather than employing the usual tactic of showing you how to write the accursed ‘hello world’ program, so beloved of all computer programming introductions, let’s start with something entirely different: print “This is most definitely not a hello world script.\n”; If you save this as the file thing.pl, or similar, then you can run it …
- 1
- 2