diff --git a/.aspelldict b/.aspelldict index 00d57f1..ab3e84a 100644 --- a/.aspelldict +++ b/.aspelldict @@ -1,4 +1,4 @@ -personal_ws-1.1 en 119 +personal_ws-1.1 en 126 AliasVar CMD CPAN @@ -37,12 +37,15 @@ TODO TRE UTF Util +acos arg args argv ascii +basename binmode boolean +canonpath chdir cmd cmp @@ -52,6 +55,7 @@ cpanm cpanminus csv dir +dirname dirs dq ds @@ -63,6 +67,7 @@ eval exitval expressivity fh +fileparse filepath fn gcc @@ -106,11 +111,13 @@ subprocess subst substr sudo +tabstop tac tempfile tsv un undef +unexpand uniq uniqstr unshift diff --git a/Changes b/Changes index 418e6e3..03e2d8a 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,7 @@ Revision history for Perl distribution App-PTP 1.16 - ?? - Add a "cheat sheet" POD to the distribution. + - Load a set of modules by default in the Perl environment. 1.15 - 2024-09-29 - Remove a remaining smartmatch usage from our benchmarks. diff --git a/lib/App/PTP/Cheat_Sheet.pod b/lib/App/PTP/Cheat_Sheet.pod index 9ffd628..68d7aee 100644 --- a/lib/App/PTP/Cheat_Sheet.pod +++ b/lib/App/PTP/Cheat_Sheet.pod @@ -171,7 +171,7 @@ C<">, or C<$> in all I arguments =item B<--re> I, B<--regex-engine>: use the specified regex engine (e.g. I, I, I, I, etc.) if installed -=item B<-X> (B<--fatal-error>), B<--ignore-error>: dies on error in B<--perl>, +=item B<-X> (B<--fatal-error>), B<--ignore-error>: die on error in B<--perl>, B<-n> and B<--filter> =back @@ -183,6 +183,11 @@ I, I, and I ones (unless B<-Q> has been passed). =over 8 +=item B, B, B, B, B, B, +B, B, B, B, B, B, B, +B, B, B, B, B, B, B<$tabstop>, B, +B, B<$columns>, ... + =item B<$_>: current line content (I) =item B<$f>, B<$F>: current file name, current absolute file name (I) diff --git a/lib/App/PTP/Commands.pm b/lib/App/PTP/Commands.pm index 47e0576..082bcfb 100644 --- a/lib/App/PTP/Commands.pm +++ b/lib/App/PTP/Commands.pm @@ -43,6 +43,23 @@ our $I_setter = tie $App::PTP::PerlEnv::I, 'App::PTP::Util::ReadOnlyVar'; # execution provided by the user. my $safe; # Sets to Safe->new() before each new file processed; +my %modules_to_load = ( + 'Data::Dumper' => [], + 'File::Basename' => [], + 'File::Copy' => [], + 'File::Path' => [qw(make_path remove_tree mkpath rmtree)], + 'File::Spec::Functions' => [':ALL'], + 'List::Util' => [@List::Util::EXPORT_OK], + 'Math::Trig' => [], + 'Text::Tabs' => [], + 'Text::Wrap' => [qw(wrap fill $columns $break $huge)], +); + +sub list_to_string { + return join(', ', map { "'$_'" } @_) if @_; + return ''; +} + # Prepare the $safe variable so that it can execute code with access to our # PerlEnv. sub new_safe { @@ -55,6 +72,8 @@ sub new_safe { } print "Creating a new safe.\n" if $options->{debug_mode}; $safe = Safe->new(); + # Loading arbitrary modules in the Safe does not work well so we don’t do it + # here. $safe->share_from('App::PTP::PerlEnv', \@App::PTP::PerlEnv::EXPORT_OK); if ($options->{use_safe} > 1) { $safe->permit_only( @@ -96,6 +115,9 @@ sub reset_safe_env { chomp($@); die "INTERNAL ERROR: cannot prepare the SafeEnv package: ${@}\n"; } + while (my ($m, $i) = each %modules_to_load) { + eval("package App::PTP::SafeEnv; use $m ".list_to_string(@{$i})); ## no critic (Eval) + } } # Delete the PerlEnv (both the safe and the eval based one). This method is only diff --git a/script/ptp b/script/ptp index 42cca4c..8c11227 100755 --- a/script/ptp +++ b/script/ptp @@ -859,6 +859,17 @@ Note that the Perl environment is entirely reset between each file (and when encountering the B<--merge> command) unless the B<--preserve-perl-env> option was passed. +When using the default B<--safe 0> mode, the following modules are loaded in the +environment, with most of their methods imported by default, in addition to the +variables and functions listed below : +L (C, with C set to true by default), +L (C, C, C), L +(C, C), L (C, C), +L (C, ...), L (C, C, +C, C, C, C, ...), L (C, C, ...), +L (C, C, C<$tabstop>), L (C, +C, C<$columns>, ...). + =head3 B<$_> This variable is set to the current line being processed. In most context (but diff --git a/t/300-perlenv.t b/t/300-perlenv.t index e18bb91..b3c8bd4 100644 --- a/t/300-perlenv.t +++ b/t/300-perlenv.t @@ -7,7 +7,7 @@ use FindBin; use lib "$FindBin::Bin/lib"; use AppPtpTest; -use Test::More tests => 37; +use Test::More tests => 38; for my $use_safe (0..2) { @@ -66,5 +66,7 @@ for my $use_safe (0..2) { ok($@ =~ /Perl code failed.*trapped by operation mask/, 'tie disallowed with --safe 2'); }{ - is(ptp(['-M', 'File::Basename', '--pivot', '-n', 'dirname($f)', 'src/fake.h']), "src\n", 'load module'); + is(ptp(['--pivot', '-M', 'File::Spec', '-n', 'File::Spec->catfile("foo", "bar")', 'src/fake.h']), "foo/bar\n", 'load module'); +}{ + is(ptp(['--pivot', '-n', 'dirname($f)', 'src/fake.h']), "src\n", 'default module'); }