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”

Find a module’s release managers

The CPAN ecosystem is more than just a way to share your code with other people. It’s also a way to let other people collaborate on the code and to help you release it. In Item 70. Contribute to CPAN, you saw how to upload your work to the Perl Authors Upload Server (PAUSE). There’s a lot more that you can do through PAUSE, though. Even if you aren’t a CPAN author, you can use PAUSE to find out more about a module’s authors and comaintainers. Continue reading “Find a module’s release managers”

Count the number of times a character occurs in a string

This Item isn’t really about counting characters in a string, but we thought we’d expand on an Item in the original Effective Perl blog that Joseph set up to support the first edition of Effective Perl Programming. He had an Item titled “Counting the Number of Times a Character Occurs in a String”. We won’t reproduce it here, so you should read his version too. Continue reading “Count the number of times a character occurs in a string”

Make exclusive flip-flop operators

In Respect the global state of the flip flop operator, you saw some examples of the .. scalar operator. The flip-flop operator returns false until its lefthand side is true. Once the lefthand side is true, the flip-flop operator returns true until its righthand side is true. Once the righthand side is true, the flip flop operator returns false: Continue reading “Make exclusive flip-flop operators”

Use CORE when you need the real thing

Perl’s a dynamic language, which means you get to change the definition of almost anything while the programming is running. You can even change the defintions of Perl’s built-in subroutine. Once you (or the evil doer who wrote the module you need) change the definition, you might want to get back to the original, and Perl provides a way for you to do that. Continue reading “Use CORE when you need the real thing”

Avoid modifying scalars connected to string filehandles

Since Perl 5.8, you can treat a string as a file (Item 54. Open filehandles to and from strings). You can open a filehandle, read from the string, write to the string, and most of the other things that you can do with a file. There are some gotchas though, when you deal with that string as a normal string and a filehandle at the same time. We’ve filed this as RT 78980: Odd behavior when string filehandles and scalar assignment collide. Continue reading “Avoid modifying scalars connected to string filehandles”

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”