Enigmatic Perl

Birmingham -- 31st August 2006

Paul Johnson

www.pjcj.net

Enigmatic Perl

enigma.jpg

$|++

toilet.jpg

Wraparound

wraparound.jpg

Negative values

negative.jpg

Let's experiment!

experiment1.jpg

Let's experiment!

experiment2.jpg

Let's experiment!

experiment3.jpg

Let's experiment!

experiment4.jpg

Just a bit

masquerade.jpg

Select a select

select.jpg

Select a filehandle

filehandle2.jpg


my $oldh = select($h);
$| = 1;
select($oldh);

Select a filehandle

filehandle2.jpg


{
    my $oldh = select($h);
    $| = 1;
    select($oldh);
}

Select a filehandle

filehandle3.jpg




{ my $oldh = select($h); $| = 1; select($oldh) }

Select a filehandle

filehandle4.jpg




select((select($h), $| = 1)[0]);

Break it up

break_up.jpg

Heavy, yet graceful

sumo.jpg


use IO::Handle;
$h->autoflush(1);

Diamonds

diamonds2.jpg

List length

string.jpg

Temporary array

temporary.jpg


my @tmp = /.../g;
my $count = @tmp;

No temporary array

permanent.jpg


my $count = /.../g;

List context

match.jpg


my ($count) = /.../g;

If all else fails, read the docs

docs.jpg

Scalar context

scalar.jpg

Try again

ugly.jpg


my $count = my @tmp = /.../g;

Assign to a list

dummy.jpg


my $count = (my $dummy) = /.../g;

Assign to undef

undef.jpg


my $count = (undef) = /.../g;

Assign to empty list

empty.jpg


my $count = () = /.../g;

Kill whitespace

blank.jpg


my $count =()= /.../g;

Line endings

irs.jpg

Little lists

alias.jpg

Example

example.jpg $_ = 1;
my $x = "xyz";
for ($x)
{
    s/x/a/;
    s/y/--/;
    s/z/length/e;
}
print "$_:$x";

Command line

alchemy1.jpg

Example

alchemy2.jpg perl -pi.bak -e 's/Pb/Au/' *

...
{
    local $^I = ".bak";
    local @ARGV = glob "*";
    while (<>)
    {
        s/Pb/Au/;
        print;
    }
}
...

Or maybe something a little shorter

alchemy3.jpg perl -pi.bak -e 's/Pb/Au/' *

...
{
    local ($^I, @ARGV) = (".bak", glob "*");
    s/Pb/Au/, print while <>;
}
...

Select a second select

stopwatch.jpg

Conclusion

chameleon.jpg

Conclusion

sunset.jpg