Strict-Perl

 view release on metacpan or  search on metacpan

lib/Strict/Perl.pm  view on Meta::CPAN

package Strict::Perl;
######################################################################
#
# Strict::Perl - Perl module to restrict old/unsafe constructs
#
# http://search.cpan.org/dist/Strict-Perl/
#
# Copyright (c) 2014, 2015, 2017, 2018, 2019, 2023 INABA Hitoshi <ina@cpan.org> in a CPAN
######################################################################

$VERSION = '2023.03';
$VERSION = $VERSION;

use 5.00503;
use strict;
BEGIN { $INC{'warnings.pm'} = '' if $] < 5.006 }; use warnings; $^W=1;

# use strict;
sub _strict {
    require strict;
    if (exists $INC{'Fake/Our.pm'}) {
        # no strict qw(vars); on Fake::Our used
    }
    else {
        strict::->import(qw(vars));
    }
    strict::->import(qw(refs subs));
}

# use warnings;
sub _warnings {
    require warnings;
    warnings::->import;
}

# install Fatal CORE::* functions
sub _Fatal {
    my $package = (caller(1))[0];

    for my $function (
        qw(seek sysseek),                                                            # :io (excluded: read sysread syswrite)
        qw(dbmclose dbmopen),                                                        # :dbm
        qw(binmode close chmod chown fcntl flock ioctl truncate),                    # :file (excluded: fileno)
        qw(chdir closedir link mkdir readlink rename rmdir symlink),                 # :filesys (excluded: unlink)
        qw(msgctl msgget msgrcv msgsnd),                                             # :msg
        qw(semctl semget semop),                                                     # :semaphore
        qw(shmctl shmget shmread),                                                   # :shm
        qw(bind connect getsockopt listen recv send setsockopt shutdown socketpair), # :socket
        qw(fork),                                                                    # :threads
    ) {
        _install_fatal_function($function, $package);
    }

    # not on Modern::Open
    if (not exists $INC{'Modern/Open.pm'}) {
        for my $function (qw(open opendir sysopen pipe accept)) {
            _install_fatal_function($function, $package);
        }
    }
}

# make fatal invocation
sub _fatal_invocation {
    my($function, $proto) = @_;

    my $n = -1;
    local @_ = ();
    my @prototype = ();
    my $seen_semicolon = 0;

    $proto =~ s/_$/;\$/;
    $proto =~ s/_;/;\$/;
    while ($proto =~ /\S/) {
        $n++;
        if ($seen_semicolon) {
            push @prototype, [$n, @_];
        }
        if ($proto =~ s/^\s*\\([\@%\$\&])//) {
            push @_, $1 . "{\$_[$n]}";
            next;
        }
        if ($proto =~ s/^\s*([*\$&])//) {
            push @_, "\$_[$n]";
            next;
        }
        if ($proto =~ s/^\s*(;\s*)?\@//) {
            push @_, "\@_[$n..\$#_]";
            last;
        }
        if ($proto =~ s/^\s*;//) {
            $seen_semicolon = 1;
            $n--;
            next;
        }
        die "Unknown prototype letters: \"$proto\"";
    }
    push @prototype, [$n+1, @_];

    if (@prototype == 1) {
        my @argv = @{$prototype[0]};
        shift @argv;
        local $" = ', ';
        return qq{\tCORE::$function(@argv) || croak "Can't $function(\@_): \$!";};

lib/Strict/Perl.pm  view on Meta::CPAN

    my($self) = @_;

    # verify that we're called correctly so that strictures will work.
    if (__FILE__ !~ m{ \b Strict[/\\]Perl\.pm \z}x) {
        my($package,$filename,$line) = caller;
        die "Incorrect use of module '${\__PACKAGE__}' at $filename line $line.\n";
    }

    # must VERSION require
    unless ($VERSION_called) {
        my($package,$filename,$line) = caller;
        die "$self $Strict::Perl::VERSION version required like 'use $self $Strict::Perl::VERSION;', stopped at $filename line $line.\n";
    }

    # use strict;
    _strict();

    # use Fatal qw(...); --- compatible routine
    _Fatal();

    # use autodie qw(...);
    if ($] >= 5.010001) {
        _autodie();
    }
}

1;

__END__

=pod

=head1 NAME

Strict::Perl - Perl module to restrict old/unsafe constructs

=head1 SYNOPSIS

  use Strict::Perl 2023.03; # must version, must match

=head1 DESCRIPTION

Strict::Perl provides a restricted scripting environment excluding old/unsafe constructs, on both modern Perl and traditional Perl.

Strict::Perl works in concert with Modern::Open and Fake::Our if those are used in your script.

Version specify is required when use Strict::Perl, like;

  use Strict::Perl 2023.03;

It's die if specified version doesn't match Strict::Perl's version.

On Perl 5.010001 or later, Strict::Perl works as;

  use strict;
  use warnings qw(FATAL all); # not follow incompatible version-up of warnings.pm
  use Fatal # by compatible routine in Strict::Perl
  qw(
      seek sysseek
      dbmclose dbmopen
      binmode close chmod chown fcntl flock ioctl open sysopen truncate
      chdir closedir opendir link mkdir readlink rename rmdir symlink
      pipe
      msgctl msgget msgrcv msgsnd
      semctl semget semop
      shmctl shmget shmread
      accept bind connect getsockopt listen recv send setsockopt shutdown socketpair
      fork
  );
  use autodie qw(
      read sysread syswrite
      fileno
  );

On Perl 5.006 or later,

  use strict;
  use warnings qw(FATAL all); # not follow incompatible version-up of warnings.pm
  use Fatal # by compatible routine in Strict::Perl
  qw(
      seek sysseek
      dbmclose dbmopen
      binmode close chmod chown fcntl flock ioctl open sysopen truncate
      chdir closedir opendir link mkdir readlink rename rmdir symlink
      pipe
      msgctl msgget msgrcv msgsnd
      semctl semget semop
      shmctl shmget shmread
      accept bind connect getsockopt listen recv send setsockopt shutdown socketpair
      fork
  );

On Perl 5.00503 or later,

  use strict;
  $^W = 1;
  $SIG{__WARN__} = sub { die "$_[0]\n" }; # not follow incompatible version-up of warnings.pm
  use Fatal # by compatible routine in Strict::Perl
  qw(
      seek sysseek
      dbmclose dbmopen
      binmode close chmod chown fcntl flock ioctl open sysopen truncate
      chdir closedir opendir link mkdir readlink rename rmdir symlink
      pipe
      msgctl msgget msgrcv msgsnd
      semctl semget semop
      shmctl shmget shmread
      accept bind connect getsockopt listen recv send setsockopt shutdown socketpair
      fork
  );

Prohibited modules in script are;

  Thread  threads  encoding  Switch

Be useful software for you!

=head1 AUTHOR

INABA Hitoshi E<lt>ina@cpan.orgE<gt> in a CPAN

This project was originated by INABA Hitoshi.

=head1 LICENSE AND COPYRIGHT

This software is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L<perlartistic>.

This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

=head1 SEE ALSO

=over 2

=item *

L<ina|http://search.cpan.org/~ina/> - CPAN

=item *

L<A Complete History of CPAN|http://backpan.perl.org/authors/id/I/IN/INA/> - The BackPAN

=back

=cut



( run in 0.424 second using v1.01-cache-2.11-cpan-5511b514fd6 )