Gabor has his listicle for 7 of the most useful Perl command line options, although it’s really five after giving separate items for the -v
/ -V
and -e
/ -E
pairs. The others are -p
, -n
, and -i
. I have my own list that I’d like to share. You might not like my list; go through perlrun to find your own. There’s also the book Perl One-Liners.
Category: advanced topics
The locale’s thousands separator
Perl can use the thousands separator appropriate for your locale, as well as the appropriate decimal separator. The Number::Format from CPAN can do all sorts of interesting localizations, but POSIX can do it.
I debated offering an example in Learning Perl (7th Edition), but POSIX‘s localeconv
function returns a hash reference. And, although I’ve added an appendix covering experimental features, I didn’t want to go through enough Perl to explain slices and postfix dereferencing. Continue reading “The locale’s thousands separator”
when(), Try::Tiny, and autodie
I’m working on Chapter 17, which is the catch-all chapter for topics we think that segue into the other books in the Learning Perl series. Although Mastering Perl has an entire chapter on catching and reporting errors, we want to at least survey the topic in Learning Perl.
The first edition of Learning Perl noted that eval
existed and gave a couple of examples, and in each subsequent edition the discussion became more involved.
Starting with the fourth edition, we devoted a chapter to using Perl modules, acknowledging the fact that Perl’s greatest feature is CPAN. In that edition, it was fairly late in the book. In the fifth edition, we moved that chapter toward the middle of the book. In each case, this means that we can then use Perl modules, whether from the Standard Library or CPAN, for the rest of the book since we’ve covered the idea of using modules. Our goal is always to cover any topic before we use it.
Since the sixth edition also covers modules, we can use it when we talk about catching errors. That means that we can talk about autodie, the pragma that became part of the Perl core in 5.10.1, and Try::Tiny, which is not a core module. We might have covered autodie
in the fifth edition, but we only covered up to Perl 5.10.0. Paul Fenwick just barely missed the cut-off.
So, while working on the eval
section, I was playing with some examples. I covered eval
, Try::Tiny, and autodie separately, but I was wondering what would happen if I combined them. Could try
and autodie cooperate?
I started with the example from the autodie documentation:
eval { use autodie; open(my $fh, '<', $some_file); my @records = <$fh>; # Do things with @records... close($fh); }; given ($@) { when (undef) { say "No error"; } when ('open') { say "Error from open"; } when (':io') { say "Non-open, IO error."; } when (':all') { say "All other autodie errors." } default { say "Not an autodie error at all." } }
Then I added Try::Tiny and started playing around with it:
use 5.010; use autodie; use Try::Tiny; my $filename = '/does/not/exist'; try { open my $fh, '>', $filename; # still dies on error } catch { when( 'open' ) { say 'Got an open error'; continue; } when( 'close' ) { say 'Got an open error'; continue; } when( ':io' ) { say 'Got an io error'; continue; } };
The output is not what I expected. It does what it looks like it should do:
Got an open error Got an io error
I was surprised that this worked, and that it worked without a warning. That when
is outside an official topicalizer (something that sets $_
, such as given
or foreach
).
That’s one of the interesting parts of writing a book. To properly research something, we think about the different ways we might combine things and especially how things might break. When we’re teaching, we’re going to run into all sorts of crazy syntheses of the topics. With a bit of experience, we can anticipate some of that, and when we do we discover some interesting things like this.
Updates to chapter 17, “Advanced Topics”
[This post notes differences between the fifth and sixth editions.]
Chapter 17 of Learning Perl is a catch-all chapter at the end of the book. We cover some “advanced” items to pique the interest of the reader and to segue into Intermediate Perl. Mostly, this covers grep
, map
, and eval
.
Since we also cover Perl modules, we expanded this chapter a bit. The list operators are nice, and since list operations are most of many programs, so we should at least mention List::Util and List::MoreUtils.
The eval
is the minimum for dealing with errors, and it also provides quite a bit of trouble as it handles $@
. So, we also introduce Try::Tiny. We don’t say much about it, but anyone who makes it this far at least finds out that it exists. While we are at it, we might as well throw in autodie, especially since it’s part of the Standard Library.