[ABE.pm] perl 5.12! - the little things

Ricardo Signes perl.abe at rjbs.manxome.org
Tue May 4 07:45:39 PDT 2010


Perl 5.12 has been out for about a month.  Later this summer, I'll be giving a
talk at YAPC about what's new in it, so it's time for me to draft my
explanations.  You get to tell me whether they make any sense or not!

I'll start with some of the very small, but nice, changes.

First off, you should be more likely, now, to use strict.  Why?  Because this:

  use 5.12.0;

Means "die unless I'm on perl 5.12.0 or later" just like it always has, and
"turn on all incompat language features introduced in 5.12" just like it meant
in 5.10, but now it also means "and turn on strict."

Since you'll be writing "use 5.12.0" anyway to get things like "say" and
Unicode fixes, now you get strictures without any stupid boilerplate.

===

A few versions ago, `man` started to try to be really clever.  It converts
quotes into smart quotes and hyphens into endashes.  This is really annoying,
and breaks code samples.  You try to copy a code example from man into your
editor, and perl barfs because −> is not ->.  You know how much time I've lost
to this?  A lot.

The error we got before was okay:

  ~$ perl5.10.1 -MCGI -e'my $q = CGI−>new;'
  Unrecognized character \xE2 in column 12 at -e line 1.

Now it's even better:

  ~$ perl -MCGI -e'my $q = CGI−>new;'
  Unrecognized character \xE2; marked by <-- HERE after y $q = CGI<-- HERE near column 12 at -e line 1.

This is one of those stupid places where a small improvement to an error
message can save us a pile of time.  Looking at the error message it's obvious
what we did:  after the CGI there's going to be some bizarre character, and we
can just go look at what it is.  No column counting needed.

===

Finally, my favorite.  Maybe I shouldn't admit that this is my favorite change
in 5.12, but it is.  One of my favorite changes in 5.10 was the // operator,
because I could write:

  my $x = $given // default;

instead of

  my $x = defined $given ? $given : default;

This change is similar:  length($x) now returns undef if $x is undef.  It used
to return 0, but it would *also* warn that you'd called length on undef.  This
led to a LOT of code like this:

  if (defined $str and length $str) { ... }

The new change is brilliant.  Not only can we just check (length $str) in
boolean context (because undef is as false as 0) but we still get that warning
if we do the actually problematic thing and use the length of undef as a
number, later.

  my $total = $length + length($x); # warns if $x is undef

That's it!  Tomorrow or so I'll mumble about some other cool changes.

-- 
rjbs


More information about the ABE-pm mailing list