Using strict is hard, let's just use Moose;

I've been running into more and more code which uses Moose, and doesn't use strict. The logic being that since Moose uses strict, your code doesn't need to.

Is there something about Moose's use of strict that differs from other modules that use strict? Say for instance that if I use Carp in my code (which uses strict), is it still ok not to use strict?

What worries me as someone who works on legacy systems where code doesn't use strict, is that best practices are de-evolving. New Perl programmers will see code that uses Moose but not strict, and go on to write programs that don't use Moose and also don't use strict. It makes me wonder if the practices that are being implemented in Modern Perl for the benefit of experienced users will set bad precedents for inexperienced users.

Hey 5.16 developers, can you enable strictures and warnings by default? Now that makes sense to me.

8 Comments

The reason people who use Moose do not also use strict/warning is because Moose->import (which is called when you use Moose) will import strict and warning into your namespace, thus you do not need to manually use them. This will demonstrate the effect:

package stuff;
use strict; use warnings;
sub import { strict->import; warnings->import; }

package main;
no strict; no warnings;
BEGIN { stuff->import; } # Emulation of "use stuff"
print $c;

As you can see, strict is in effect during the print even though strict was turned off. You can see this with Moose as well:

package main;
no strict; no warnings;
use Moose; # Turns them on
print $c;

But I do agree with you that the lack of seeing "use strict" in Moose-using code may perhaps lead people to not use strict when they are not using a package that imports strict into your namespace. Personally even when I use Moose, I also add the strict and warnings lines as well.

> Hey 5.16 developers, can you enable strictures and warnings by default? Now that makes sense to me.

That's been already the case since 5.12, but only when you declare 'use v5.12' (or v5.14 etc.).

% perl -e 'use v5.12; $x'
Global symbol "$x" requires explicit package name at -e line 1.

If the upgraded perl enables strict *everywhere* then all the existing program that has no 'use strict' will break, and that's terrible.

You may feel better if you've seen Jesse Vincent's talk about the future of Perl 5. The basic gist is if you declare a version, Perl will try it's hardest to keep to the semantics of that version. The defaults without declaring a version should be (if all goes according to plan) what you currently get with Perl 5.14.x when you don't declare a version.

As for the people writing new code without strict. Only education can help. (well, of course unless you want to shoot them but that's messy). So every time they come to you treat it as an opportunity to to show them how use strict will help them. You can mention saved time, or saved sanity or safety belts. Whatever might work for them.


Maybe eve try to start a code review process where they will show their code to you even before they bump into a problem.

Leave a comment

About Phred

user-pic I blog about Perl.