Undef a scalar to release its memory

When you store a large string in a scalar, perl allocates the memory to store that string and associate it with the scalar. It uses the same memory even if you assign a much shorter value to the same scalar. Use the functional form of undef to let perl reuse that memory for something else. This is important when you want to reuse the variable or the lifetime of the variable is very long.

Continue reading “Undef a scalar to release its memory”

Mix assignment and reference aliasing with declared_refs

Perl v5.26 adds the experimental declared_refs feature that expands on the experimental refaliasing feature from v5.22. As with all experimental features, this may change or disappear according to perlpolicy.

Continue reading “Mix assignment and reference aliasing with declared_refs”

Use Unicode 10 in Perl v5.28

Perl v5.28 updates to Unicode 10. There are 8,518 new characters, 7,473 which are in the CJK extension. There are 56 new emojis. And, the Bitcoin symbol, ₿. It adds a T. rex, 🦖, but we’re still waiting for a raptor. To Perl they are just characters like any other so you don’t need anything new to deal with them.

Continue reading “Use Unicode 10 in Perl v5.28”

Find the new emojis in Perl’s Unicode support

Perl v5.26 updates itself to Unicode 9. That’s not normally exciting news but people have been pretty enthusiastic about the 72 new emojis that come. As far as Perl cares, they are just valid code points like all of the other ones.

Continue reading “Find the new emojis in Perl’s Unicode support”

Avoid perl housekeeping for hot loop optimization

David Golden gave a talk at The Perl Conference 2017 where he showed Real World Optimization for the MongoDB Perl driver. He spoke about many big performance gains and you can watch the talk for that, but at the end he talked about various micro-optimizations.

Small gains in “hot loops” (code that executes many, many times) can add up to significant savings. David was able to cut off 20% of the runtime with some of these micro-optimizations. All of these are his ideas but they are the very thing the Effective Perl programmer is curious about.

Continue reading “Avoid perl housekeeping for hot loop optimization”

Initialize array and hash variables with state

Perl v5.28 allows you to initialize array and hash variables that you declare with state. This is a feature a long time coming and that I’m quite happy as finally arrived.

Since v5.10 and up to v5.26 you could only initialize a state variable if it was a scalar. You could declare a hash or array variable but you couldn’t give it an initial value at the same time. You could do this:

Continue reading “Initialize array and hash variables with state”

Perl v5.26 now recognizes version control conflict markers

Perl v5.26 can now detect and warn you about a version control conflict markers in your code. In prior versions, the compiler would try to interpret those as code and would complain about a syntax error. You program still fails to compile but you get a better error message. Maybe some future Perl will bifurcate the program, run both versions, and compare the results (don’t hold your breath):

Continue reading “Perl v5.26 now recognizes version control conflict markers”

Unquoted empty heredoc terminators are now fatal

Continuing its quest to clean up long deprecated features, v5.28 takes care of another feature deprecated since v5.0. You can no longer neglect to specify a heredoc separator. This was a warning in v5.26 and is now fatal. You probably weren’t doing this anyway (I’ve never seen it in the wild), but it’s nice to know the edge cases are disappearing.

Continue reading “Unquoted empty heredoc terminators are now fatal”

In-place editing gets safer in v5.28

In-place editing is getting much safer in v5.28. Before that, in rare circumstances it could lose data. You may have never noticed the problem and even with all the times I’ve explained it in a Perl class I haven’t really thought about it. This was first reported as early as December 2002 and after we get v5.28 it won’t be a problem anymore. Continue reading “In-place editing gets safer in v5.28”