CT320 IQ 08
Show Main.IQ08 as a slide show.
Sigils
In Perl, which sigil indicates an array?
@
$
%
#
&
- I don't know what the @$%#& a “sigil” is!
@ = array. Think of @ as the a in array.
Declaration
In Perl, how do I declare a scalar?
my zot;
int zot;
my $zot;
int $zot;
- You can’t.
We don’t give types to our variables, so int
is out.
A scalar always has a $
with it.
Arguments
In Perl, how are arguments passed to a function?
sub foo(alpha, beta, gamma)
sub foo($alpha, $beta, $gamma)
- Implicitly, in @_.
- Via some other agreed-upon global array.
Arguments are passed in @_
.
Named parameters ($alpha, $beta)
are possible, but they’re an
experimental feature that you have to explicitly enable.
$_
In Perl, what does $_
mean?
- It’s just a scalar with an odd name.
- It’s related to @_ somehow.
- Sometimes, implicitly set in a
for
loop.
- Sometimes, implicitly set in a
while
loop.
$_
is a scalar with an odd name, but not “just” that.
It’s implicitly set in for (1..3)
, and in while (<FH>)
loops.
Array
In Perl, how do I concatenate @a
and @b
into @c
?
@c = @a + @b;
@c = (@a, @b);
@c = @ab;
@c = @a, @b;
@c = @a; push @c, @b;
A does addition, in scalar context, of the sizes of @a
and
@b
. @ab
is a brand-new variable.
Either B or E will work, but B is easier to read.
What will this code display?
if ("My dog has fleas" =~ /(d*g)/) {
print "I matched “$1”\n";
}
else {
print "nope\n";
}
I matched “g”
I matched “nope”
I matched “dog”
I matched “g”
- It will throw an exception.
nope
Sycophancy
What is the best language ever?
- Perl
- Perl
- Perl
- Perl
- Perl
- Python