October 2012 archive

More hashes and sorting

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 …

Continue reading

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 …

Continue reading

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 …

Continue reading

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 …

Continue reading

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 …

Continue reading

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 …

Continue reading

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 …

Continue reading

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 …

Continue reading

Educated guesswork

Enterobacter W1 [CC-BY-SA-3.0 Steve Cook]; cylindrical cells about a micron long

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 …

Continue reading

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 …

Continue reading

Load more