Perl Programming

CSE3395




nooq script

0 comments

All the script has to do is get the ‘notes’ folder onto the server. Plonk.


Lecture: Subroutines

0 comments

It's hard to make a topic like subroutines interesting.

You have, of course, sigh, the option of sending parameters to a subroutine.

many Unic protrams can act as filters
<> diamond operator. Returns as much input as there is.
If there are no comad line options then it acts like

sub name
{
#body goes here
}

You almost never need to use the & character, only when it's ambiguous. But we're encouraged to use the ampersand and brackets.

@_ is the list that has been passed into me.

Remember the value of pi? It's a good party trick anyway.

returning
Last value evaluated
Can use the return keyword
Return value can be a scalar or list


Lecture 3: Lists and Arrays

0 comments

Wow imagine having to wait for your computer to turn on before you could use it!

You can pretty much chuck any scalar into an array in Perl.
Arrays grow in size depending on what you access.

$#array returns the highest index -1.

Lists
A list is an expression containing an ordered sequence of scalars

THIS IS PERL.

Arrays and Lists.
A list literal is an expression containing an ordered sequence of scalars.

An array is best used when initialised by a list, so that you can index it and step through it and do all kinds of nifty things like that.

There are various ways to assign a list of words to an array. You can reference the entire array with the @ sign. The $ sign is for individual indexed elements.

Now you can do @animal = qw(Dog cat wildebeest);
And that's it :-D

(This just from Mum: "Did you go to Mardi Gras?"
Me: "No! I'm in Clayton learning about Perl Programming hehehe").

Arrays and scalars occupy different name-spaces. @x and $x[...] refers to array var @x
$x refers to scalar var $x

Arrays cannot contain arrays.
Lots of handy things you can now do.

@items = sort @items;
print sort qw(c a t); # Prints act

qw creates a list containing all the elements. It's the same as print sort ("c", "a", "t");

Array Functions
push pop
unshift shift


Scalar Values

0 comments

In perl, you don't need to initialise and even declare variables.

Typing is relaxed and late bound.
There is a special value 'undefined' undef

Scalar variables must start with a $
$apples
$text2
$_ <- a variable you don't have to assign it to anything, but you can always find the result of the last thing that happened.
One of the most elegant features of the language?

Perl doesn't usually care if a scalar contains a number or a string.

"cat" . "fish" produces "catfish"

> and gt test for 'greater than' in scalars. One for numbers, one for strings.

String literals
Double quotes "blah" - delimited. Usual C backslash rules apply.
In double quotes "hello $name", $name is a variable.

single 'blah'
variables do not interpolate.

Turns out this is useful

print "The sum is $total\n";
subs the current value for total.

print "today is the ${day}th\n";

undef is neither number nor string.

$line = ;
chomp $line;

and now it's in the $_ variable.

False
undef
""
0

True
everything else

if(){}
elsif(){}
else{}

Standard way of processing a text screen

while (defined ($input = ))
{
chomp $input;
push @lines, $input;
}

unless and untill IS THE SAME AS
if(!condition) and while(!condition)

last
next
redo


Last posts

Archives

Links