Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Do you ever forget to use the strictures?

by tphyahoo (Vicar)
on Aug 30, 2005 at 13:58 UTC ( #487760=perlmeditation: print w/replies, xml ) Need Help??

I do. Lots of pls and pms and .ts out there and sometimes I just... forget. And ugly things happen.

So, here's a sanity check I will be running every now and then when weirdness creeps into my life. Put it into the top of any directory, and it spits out the perl files where you forgot to use strict or use warnings.

use strict; use warnings; use File::Find; use File::Slurp; my @starters = @ARGV ? @ARGV : qw(.); my @files; @ARGV = (); find sub { push @files, $File::Find::name if /\.(pl|pm)$/; # or whatever cond +ition here }, @starters; print "Perl files that don't use the stricutres\n"; foreach my $file (@files) { my $text = read_file( $file ) ; print "$file\n" unless ( $text =~ /use warnings;/ && $text =~ /use + strict;/ ) ; }
C:\thomasdata\thomasprojects\fixHtml\fix_fonts>perl sanitycheck.t Perl files that don't use the stricutres ./modules/Htmlripper/ ./modules/Htmlripper/ ./modules/Htmlripper/ ./modules/Htmlripper/ ./modules/Htmlripper/Regex/ ./modules/Htmlripper/Regex/ ./modules/Htmlripper/Regex/ ./modules/Htmlripper/Regex/ ./modules/Htmlripper/Regex/ C:\thomasdata\thomasprojects\fixHtml\fix_fonts>
UPDATE: turned off the i flags, as ysth suggested.

Replies are listed 'Best First'.
Re: Do you ever forget to use the strictures?
by inman (Curate) on Aug 30, 2005 at 14:11 UTC

      Exactly: ++ to inman. My module templates have them automatically. And I use an editor macro to easily add them to any quick and dirty code I write. E.g., for vim:

      map ,pl ggI#!/usr/bin/perl<CR>use strict;<CR>use warnings;<CR><ESC>


      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      My own boilerplate template is similar:
      #!/usr/bin/env perl # $Header$ use strict; use vars qw( $VERSION ); $VERSION = sprintf('%d.%02d', q$Revision: 1.0 $ =~ /(\d+)\.(\d+)/); 1; __END__


      perl -le "print unpack'N', pack'B32', '00000000000000000000001000000000'"

Re: Do you ever forget to use the strictures?
by VSarkiss (Monsignor) on Aug 30, 2005 at 16:12 UTC
Re: Do you ever forget to use the strictures?
by Ovid (Cardinal) on Aug 30, 2005 at 18:53 UTC

    Oddly enough, though I'm fairly dogmatic about warning against the evils of coding without strictures, I forget to use strict now more than I used to. This is because I test so thoroughly that I catch bugs early. Then when I'm sitting there scratching my head over a code failure, I check and see that I forgot to use strict.

    So if you're testing thoroughly, strict isn't as much of the big deal some make it out to be. However, if I come along later and add strictures to your code and it fails, I'd be mighty unhappy.


    New address of my CGI Course.

Re: Do you ever forget to use the strictures?
by ysth (Canon) on Aug 30, 2005 at 23:50 UTC
        print "$file\n" unless ( $text =~ /use warnings;/i && $text =~ /use strict;/i ) ;
    You really don't want those //i flags. On case-preserving filesystems, use Warnings; and use Strict; will apparently succeed but have no effect because of the lack of Warnings::import() and Strict::import().
      yep, took them out. old habit I guess, I just turn on i "by default."
Re: Do you ever forget to use the strictures?
by Steve_p (Priest) on Aug 30, 2005 at 16:08 UTC
Re: Do you ever forget to use the strictures?
by Ven'Tatsu (Deacon) on Aug 30, 2005 at 18:44 UTC

    I start any file based programs with strict and warnings. But I rarely -Mstrict with -e. Often that means that when a -e program begins to grow to the point that my shell's (bash or cmd.exe depending on platform) readline is more cumbersome than moving the code into a file and bouncing to a shell to execute (or keybinding to execute for editors that handle it well) I move it to a file, usualy adding strict and warnings before I even copy the code from the command line, and then forgetting before the first time I run the file that I still need a few my's sprinkled around.

    I'm fairly good at judging before hand what will be a one off with maybe an edit or two and a program that will require editing and debugging, but ever couple of months I'm not so lucky.

Re: Do you ever forget to use the strictures?
by tinita (Parson) on Aug 31, 2005 at 23:27 UTC
    i have a vim macro for starting a new script/module, so i can't forget it.
    unfortunately this doesn't help if you have more packages in one module file (mostly during development) and then, if the code has grown and you source the packages out (hmm, outsourcing gets a different meaning here =) you'll forget to add strictures. so i'll try out your script i guess.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://487760]
Approved by ktross
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2022-09-24 16:04 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (114 votes). Check out past polls.