[Chicago-talk] Two Perl curiosities
joel.a.berger at gmail.com
Tue May 19 11:16:39 PDT 2015
On Tue, May 19, 2015 at 1:08 PM Alan Mead <amead2 at alanmead.org> wrote:
> 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
> use strict;
> use warnings;
> my $a := 42;
> print "$a\n";
> $ ./test.pl
To investigate, I ran it through B::Deparse and got this interesting nugget:
perl -MO=Deparse -E 'my $x := 42; say $x'
Use of := for an empty attribute list is not allowed at -e line 1.
So what it is trying to do is apply an empty attribute list. Using
attributes on variables is a very rare thing, so perhaps few people know it
exists, read more here: https://metacpan.org/pod/attributes
A possibly useful example is that this fails:
perl -E 'my $x :Int = 42.2; say $x'
but generally, I think variable attributes are best avoided.
> Alan D. Mead, Ph.D.
> President, Talent Algorithms Inc.
> science + technology = better workers
> +815.588.3846 (Office)
> +267.334.4143 (Mobile)
> Announcing the Journal of Computerized Adaptive Testing (JCAT), a
> peer-reviewed electronic journal designed to advance the science and
> practice of computerized adaptive testing: http://www.iacat.org/jcat
> Chicago-talk mailing list
> Chicago-talk at pm.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Chicago-talk