Perl v5.10 adds a regular expression shortcut \R
that matches anything the Unicode specification thinks is a line ending. It looks similar to a character class shortcut but it’s not. It can match the sequence of carriage-return line-feed, but character classes don’t match sequence. Continue reading “The \R generic line ending”
Category: perl
Use for() instead of given()
[Lexical $_
was removed in v5.24]
Perl 5.10 introduced the given-when
feature, a fancier version of the C switch
feature. However, it was poorly designed and tested and depended on two other dubious features, the lexical $_
and smart-matching. Parts of this feature are salvageable, but you should avoid the literal given
(and probably the lexical $_
and the smart matching, but I’ll skip those for this Item). Continue reading “Use for() instead of given()”
Use the C3 method resolution order in multiple inheritance
Perl 5.10 introduced a flexible method resolution order mechanism. Instead of Perl’s default order (see Understand Perl’s default inheritance model), you can try something less stupid by using the mro pragma to specify which order perl
. Continue reading “Use the C3 method resolution order in multiple inheritance”
Use the > and < pack modifiers to specify the architecture
Byte-order modifiers are one of the Perl 5.10 features farther along in perl5100delta, after the really big features. To any pack
format, you can append a <
or a >
to specify that the format is little-endian or big-endian, respectively. This allows you to handle endianness in the formats that don’t have specify versions for each architecture already, as well as apply endianness to groups. Continue reading “Use the > and < pack modifiers to specify the architecture"
Use Test::More as you experiment
When you’re trying something new, write small programs to test the idea or the new feature. This way, you isolate what you’re doing from the rest of the big application where you might want to use the idea. Some people try to insert the new features directly into the middle of their large programs, but then have problems separating the bugs from the rest of the application with the new thing they want to add. Continue reading “Use Test::More as you experiment”
Use the \N regex character class to get “not a newline”
Perl 5.12 introduced an experimental regex character class to stand in for every character except one, the newline. The \N
character class is everything but the newline.
In prior versions of Perl, this is the same thing as the .
meta character. That is, it’s the same as long as someone doesn’t add the /s
to the match or substitution operator or the regex quoting operator, or doesn’t insert the option inside the pattern: Continue reading “Use the \N regex character class to get “not a newline””
Use a smart match to match several patterns at once
The smart match operator (Item 23. Make work easier with smart matching) reduces many common comparisons to a few keystrokes, keeping with Perl’s goal of making the common things easy. You can use the smart match operator to make even less common tasks, such as matching many regular expressions at the same time, just as easy. This Item shows you how to use the smart match to see if at least one of a series of regexes matches a string. Continue reading “Use a smart match to match several patterns at once”
Perl 5.14 adds non-destructive transliteration
[This is a mid-week bonus item since it’s so short]
In Perl 5.13.2, you got a non-destructive version of the substitution operator (Use the /r substitution flag to work on a copy). Instead of changing it’s target, the non-destructive version returns a new string that has the substitution. Continue reading “Perl 5.14 adds non-destructive transliteration”
Use array references with the array operators
[Update: Perl v5.24 removes this experimental feature]
There’s a significant change in syntax showing up in Perl 5.14. The array operators push, pop, shift, and unshift previously only worked on named arrays or dereferenced references. Now, thanks to David Golden, they’ll work on array references. Not only that, they’ll work on references that you’ve stored in variables or that come as the return values from subroutine calls. This feature will show up in Perl 5.13.7, so you need to compile a development version to try this: Continue reading “Use array references with the array operators”
Use the return value from srand
[This is another bonus, mid-week item since it’s so short and probably mostly useless as a tweak to what you already do.]
Perl 5.14 changes srand to return the seed that it used to start the pseudorandom number generator that gives you numbers through rand. There are plenty of interwebs that will explain the difference between real randomness and the sort that you get from computers, but for this item, suffice it to say that the numbers you get from perl are completely deterministic. If you start with the same seed, you get the same sequence. Continue reading “Use the return value from srand”