[Chicago-talk] Two Perl curiosities

Doug Bell madcityzen at gmail.com
Tue May 19 11:15:49 PDT 2015

> On May 19, 2015, at 1:08 PM, Alan Mead <amead2 at alanmead.org> wrote:
> Curiosity #1: I can't post the entire program code but I have a long script:
> #!/usr/bin/perl
> use strict;
> use warnings;
> [... about 150 lines omitted, including the beginning of a loop in a
> subroutine]
>          my $a = $$pars{'alpha'};
>          my $delta = $$pars{'delta'};
>          my $taus = $$pars{'taus'};
>          print $fh join("\t", ($a, >>$b<<, 0, @$taus)), "\n"  # >> <<
> is my emphasis
> I meant to type "delta" but I typed "b".  Shouldn't Perl throw an error
> because of the strict pragma or at least a warning? I get no warnings
> when I "perl -c" and when running, all the warning I get is that $b is
> uninitialized.  When I create a small test script with this kind of
> error, I get a warning: "Name "main::b" used only once: possible typo at
> ./test.pl line 11."

$b always exists as a global variable, because it's used by sort().
It's why people tend to avoid using $a and $b in examples or ever,
because they could be clobbered by a sort() at any time.

> Curiosity #2: Also, I converted a program from Pascal to Perl and I
> forgot one assignment operator (e.g., something like my $a := 1;) and
> Perl never complained.  What does := do in Perl? It's hard to google
> ops. In a test program, it looks like it's a valid assignment operator?
> $ cat test.pl
> #!/usr/bin/perl
> use strict;
> use warnings;
> my $a := 42;
> print "$a\n";
> $ ./test.pl
> 42

The : in variable declarations would allow you to set a variable's
attributes, which is rarely used. In that syntax, Perl just thinks
you've set no attributes.

More information about the Chicago-talk mailing list