in reply to Passing on "use"s

For many of my projects, i use a script to update the standard pragmas in all files. A typical module might begin with:

package PageCamel::Web::ListAndEdit::Main; #---AUTOPRAGMASTART--- use v5.36; use strict; use diagnostics; use mro 'c3'; use English; use Carp qw[carp croak confess cluck longmess shortmess]; our $VERSION = 4.2; use autodie qw( close ); use Array::Contains; use utf8; use Data::Dumper; use Data::Printer; use builtin qw[true false is_bool]; no warnings qw(experimental::builtin); use PageCamel::Helpers::UTF; #---AUTOPRAGMAEND--- use base qw(PageCamel::Web::BaseModule); ...

Since things change over the years, i've written a script and that's the ONLY place in a project that needs manual adapting. Technically, i can even open a new, empty file and just paste in the AUTOPRAGMA lines and run the script. But for new files, ye olde copy&paste is usually faster.

Here's my script:

#!/usr/bin/env perl #---AUTOPRAGMASTART--- use v5.36; use strict; use diagnostics; use mro 'c3'; use English; use Carp qw[carp croak confess cluck longmess shortmess]; our $VERSION = 4.2; use autodie qw( close ); use Array::Contains; use utf8; use Data::Dumper; use Data::Printer; use builtin qw[true false is_bool]; no warnings qw(experimental::builtin); use PageCamel::Helpers::UTF; #---AUTOPRAGMAEND--- # PAGECAMEL (C) 2008-2020 Rene Schickbauer # Developed under Artistic license print "Searching files...\n"; my @files = (find_pm('lib'), find_pm('devscripts')); #my @files = find_pm('server'); print "Changing files:\n"; foreach my $file (@files) { my $inserted = 0; print "Editing $file...\n"; my @lines; open(my $ifh, "<", $file) or die($ERRNO); @lines = <$ifh>; close $ifh; open(my $ofh, ">", $file) or die($ERRNO); foreach my $line (@lines) { if($line =~ /^use\ +(.+)\;/) { my $pragma = $1; my $skip = 0; if($pragma =~ /(strict|warnings|English|mro|diagnostics|Ca +rp|Fatal|Array\:\:Contains|autodie|utf8|Encode|Data\:\:Dumper|Helpers +\:\:UTF|builtin|Data\:\:Printer)/ && $pragma !~ /Digest/) { # Remove this (old) lines $skip = 1; } if($pragma =~ /(5.\d+)/ && $pragma !~ /Digest/) { # Always update the required perl version $skip = 1; } if($file =~ /Helpers\/UTF\.pm$/ && $pragma =~ /Encode/) { # Don't skip this one instance, this is the only place + that loads the Encode module $skip = 0; } if($skip) { next; } } if($line =~ /^no\ if.*experimental\:\:smartmatch/) { next; } if($line =~ /^(our|my) \$VERSION/) { next; } # Handle sub "signatures" if($line =~ /^use\ feature\ \'signatures\'/ || $line =~ /^no\ +warnings\ .*experimental\:\:signatures/) { next; } # Handle the new "builtin" stuff if($line =~ /^no\ warnings\ .*experimental\:\:builtin/) { next; } if($line =~ /^\#\-\-\-AUTOPRAGMA/) { next; } print $ofh $line; if($inserted) { # Already inserted the pragmas next; } if($line =~ /^package\ / || $line =~ /^\#\!/) { print $ofh "#---AUTOPRAGMASTART---\n"; print $ofh "use v5.36;\n"; print $ofh "use strict;\n"; print $ofh "use diagnostics;\n"; print $ofh "use mro 'c3';\n"; print $ofh "use English;\n"; print $ofh "use Carp qw[carp croak confess cluck longmess +shortmess];\n"; print $ofh "our \$VERSION = 4.2;\n"; print $ofh "use autodie qw( close );\n"; print $ofh "use Array::Contains;\n"; print $ofh "use utf8;\n"; print $ofh "use Data::Dumper;\n"; print $ofh "use Data::Printer;\n"; print $ofh "use builtin qw[true false is_bool];\n"; print $ofh "no warnings qw(experimental::builtin);\n"; if($file !~ /Helpers\/UTF\.pm$/) { print $ofh "use PageCamel::Helpers::UTF;\n"; } print $ofh "#---AUTOPRAGMAEND---\n"; $inserted = 1; } } close $ofh; } print "Done.\n"; exit(0); sub find_pm($workDir) { my @files; opendir(my $dfh, $workDir) or die($ERRNO); while((my $fname = readdir($dfh))) { next if($fname eq "." || $fname eq ".." || $fname eq ".hg"); $fname = $workDir . "/" . $fname; if(-d $fname) { push @files, find_pm($fname); } elsif($fname =~ /\.p[lm]$/i && -f $fname) { push @files, $fname; } } closedir($dfh); return @files; }

PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP

Replies are listed 'Best First'.
Re^2: Passing on "use"s
by choroba (Cardinal) on Nov 02, 2023 at 15:30 UTC
    What irritates me is the duplicity duplication. Wouldn't it be nicer to copy the autopragma part from the script itself instead of repeating it in prints? ;-)

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Hmm, the script also edits itself during update, using the logic defined in the function. But your solution would work, too.

      PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
Re^2: Passing on "use"s
by NERDVANA (Priest) on Nov 02, 2023 at 22:50 UTC
    Why the explicit 'use strict' when you're already loading 'v5.36' ?
      Why the explicit 'use strict' when you're already loading 'v5.36' ?

      Having both allows to later change your mind and support older Perl versions (perhaps because someone paid for that). Removing use v5.36 would implicitly disable strict otherwise.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        G'day Alexander,

        "Having both allows to later change your mind and support older Perl versions ... Removing use v5.36 would implicitly disable strict otherwise."

        In the dim, distant past I made a similar comment regarding code I'd posted which included some equivalent redundancy (although I don't recall details now); on that basis, I've upvoted your post. However, I changed my thinking on that and, accordingly, would argue against what you have here.

        I'll just talk about this generally as a number of related scenarios apply; for example, removing any use VERSION statement, changing to various earlier VERSIONs, changing to a VERSION earlier than 5.12, and so on. These comments would also apply, to a certain degree, when starting with a VERSION later than 5.36.

        See "perl5360delta: use v5.36" for the features enabled by that VERSION.

        Here's just some of the things you'd have to deal with:

        • strict is enabled for all versions since (and including) 5.12; so you wouldn't necessarily lose that.
        • warnings was first enabled in 5.36; so you would definitely lose that.
        • signatures were introduced in 5.20, so you might lose that and need to rewrite "sub find_pm($workDir) {...}" in the code presented (and potentially rewrite similar code elsewhere).
        • signatures were experimental prior to 5.36. Local policy may mean not using them or including use experimental signatures (or similar).
        • builtin was introduced in 5.36; so you would definitely lose that and need to rewrite any code using it.

        Having to remember to explicitly "use strict;" may not be necessary and, even it was, it pales into insignificance against all of the other things you'd have to remember to do.

        — Ken

      Yes, it's technically not required. But after nearly two decades of me preaching to other people to "use strict", a Perl script without that line just freaks me out. The force of habit is strong in this one...

      PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP