Turn off Perl 5.12 deprecation warnings, if you dare!

Perl 5.12 deprecates several features, for various reasons. Some of the features were always stupid, some need to make way for future development, and some are just too ornery to maintain. All of these are listed in the perldelta5120 documentation. The new thing, however, is that Perl 5.12 will warn you about these even if you don’t have warnings turned on. Consider this script full of Perl whoppers: Continue reading “Turn off Perl 5.12 deprecation warnings, if you dare!”

Match Unicode characters by property value

A Unicode character has properties; it knows things about itself. Perl v5.10 introduced a way to match a character that has certain properties that v5.10 supports. In some cases you can match a particular property value. Now v5.12 allows you can match any Unicode property by its value. The newly-supported ones include Numeric_Value and Age, for example:

\p{Numeric_Value: 1}
\p{Nv=7}
\p{Age: 3.0}

Continue reading “Match Unicode characters by property value”

Locate bugs with source control bisection

As you work in Perl you store each step in source control. When you finish a little bit of work, you commit your work. Ideally, every commit deals with one thing so you’re only introducing one logical change in each revision. Somewhere along the process, you might discover that something is not working correctly. You think that it used to work but you’re not sure where things went pear-shaped, perhaps because the bug seemingly deals with something that you weren’t working on. Continue reading “Locate bugs with source control bisection”

Perl Authors Night at Powell’s Technical Books

During OSCON, Joshua and I are taking part in the Perl Authors Night at Powell’s Technical Books on Tuesday, July 20 at 7 pm. Bring your copy of Effective Perl Programming for us to sign.

Other authors confirmed so far include:

  • chromatic (Modern Perl, Perl Testing: A Developer’s Notebook, Perl Hacks, Extreme Programming Pocket Guide)

  • brian d foy (Effective Perl Programming, Learning Perl, Intermediate Perl, Mastering Perl)

  • Joshua McAdams (Effective Perl Programming)

  • Curtis “Ovid” Poe (Perl Hacks)

  • Randal Schwartz (Programming perl (1st edition), Learning Perl, Intermediate Perl, Perls of Wisdom)

  • Peter Scott (Perl Medic, Perl Debugged, Perl Fundamentals (DVD))

Make sure that you’re going to Powell’s Technical Books is at 33 Northwest Park Avenue in Portland, and not one of their other Portland stores. You can take the MAX Green line (for free) from the Convention Center to NW 5th St and NW Couch St, then walk 4 blocks west to the store.

Keep your programmatic configuration DRY

A common mantra among programmers today is to keep your code DRY. This little acronym stands for “Don’t Repeat Yourself” and serves as a reminder that when you see a repetitive pattern in your code or are tempted to copy/paste some statements, you should think twice and consider extracting the common logic into a chunk of code that can be reused.

For many programmers, this practice begins to break down when “configuration” code is involved. When I talk about configuration code here, I’m not talking about the XML, YAML, INI, etc. bits of your project. I’m talking about the Perl code in your program that simply serves as data to feed some active portion of your code.

A common mantra among programmers today is to keep your code DRY. This little acronym stands for “Don’t Repeat Yourself” and serves as a reminder that when you see a repetitive pattern in your code or are tempted to copy/paste some statements, you should think twice and consider extracting the common logic into a chunk of code that can be reused. Continue reading “Keep your programmatic configuration DRY”

Effective Perl Programming is in PDF and ePub

I just found out that Effective Perl Programming is available in digital formats through eBooks.com. They have a PDF version and an ePub version, each for $US31.99, which, sadly, is more than the hard-copy price of $US26.39 on Amazon.com and the Kindle price of $US17.59. Continue reading “Effective Perl Programming is in PDF and ePub”

Watch out for side effects with `use VERSION`

Item 83: Limit your distributions to the right platforms mentioned that use might invoke side effects. We didn’t get into the details in that Item though. As of Perl 5.10, use imports some feature that you might not want.

Merely specifying a Perl version prior to 5.10 does nothing other than check the version you specify against the interpreter version. If the version you specify is equal to or greater than the interpreter version, your program continues. If not, it dies:

use 5.008;  # needs perl5.008000 or later

This works with require too:

require 5.008;  # needs perl5.008000 or later

However, use is a compile-time function and require is a run-time function. By the time you hit that require, perl has already compiled your program up to that point or died trying as it ran into unknown features. Code may have already run, despite using an inappropriate version of perl. You want to impose your version restriction as soon as possible, so use is more appropriate since it happens earlier.

You might think that you can fix this with a BEGIN block which compiles and immediately runs the code so you get the ordering right. This gets the version check at compile time even though it’s a runtime statement:

BEGIN { require v5.10; }

In early versions of v5.10, this still imported new features, but this bug has been fixed. See BEGIN {require 5.011} imports features.

You should use at least v5.10.1 because it fixes various issues with smart match. That version doesn’t automatically import the new features if you use require. Either of these specify that version:

use v5.10.1;
BEGIN { require v5.10.1; }

use 5.010

With Perl 5.10, you get three side effects with use v5.10. Starting with that version, use-ing the version also pulls in the new features for that version. Obstensibly, that keeps programs designed for earlier versions breaking as newer perls add keywords, but it also tries to enforce the current philosophy of good programming on you.

Perl 5.10 introduces say, state, and given-which, which you import implicitly when you say use v5.10.1:

use v5.10.1;
say 'I can use Switch!';  # imported say()

given ($ARGV[0]) {        # imported given()
	when( defined ) { some_sub() }
	};

sub some_sub {
	state $n = 0;         # imported state()
	say "$n: got a defined argument";
	}

If you want to insist on v5.10 without its new features, perhaps because your code uses some of the same keywords already, you can unimport the side effects immediately with the new feature pragma:

use v5.10.1;     # implicit imports
no feature;    # take it right back again

# you're own version of say()
sub say {
	# something that you want to do
	}

If you only want some of the new features, you can unimport the ones that you don’t want:

use v5.10.1;
no feature qw(say);   # leaves state() and given()

sub say {
	# something that you want to do
	}

use 5.012

Perl 5.12 includes two more side effects for use VERSION. The unicode_strings feature treats all strings outside of bytes and locale scopes as Unicode strings. Additionally, use v5.12 automatically turns on strict:

use v5.12;
# now strictures are on

$foo = 1;   # compile-time error!

If, for some odd and dangerous reason you don’t want strict on by default, you can turn it off yourself, even though unimporting it doesn’t give you the warning that you’ve left the paved roads, you’ve just violated your rental car contract, and there’s a chainsaw massacrer waiting for you:

use v5.12;
no feature;
no strict;

my $foo = 1;   

$fo0++;  # sure, go ahead and make that error

A workaround to restrict perl versions

You can restrict the version more tightly by checking the value of the $] variable, just like the various examples you saw in Item 83:

BEGIN {
	die "Unsupported version" 
		unless $] >= 5.010 and $] < 5.011
	}

This has the added benefit of restricting the upper acceptable perl version. It works on older Perls too.

Things to remember

  • use VERSION imports new features since Perl 5.9.5.
  • BEGIN { require VERSION } still imports new features (fixed in later versions of v5.10 and v5.12)
  • Use no feature or no strict to unimport unwanted features.
  • Restrict the perl version with $].