Skip to content

Commit

Permalink
Load a set of modules by default in the Perl environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkende committed Sep 30, 2024
1 parent 163cf64 commit 6091488
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
9 changes: 8 additions & 1 deletion .aspelldict
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
personal_ws-1.1 en 119
personal_ws-1.1 en 126
AliasVar
CMD
CPAN
Expand Down Expand Up @@ -37,12 +37,15 @@ TODO
TRE
UTF
Util
acos
arg
args
argv
ascii
basename
binmode
boolean
canonpath
chdir
cmd
cmp
Expand All @@ -52,6 +55,7 @@ cpanm
cpanminus
csv
dir
dirname
dirs
dq
ds
Expand All @@ -63,6 +67,7 @@ eval
exitval
expressivity
fh
fileparse
filepath
fn
gcc
Expand Down Expand Up @@ -106,11 +111,13 @@ subprocess
subst
substr
sudo
tabstop
tac
tempfile
tsv
un
undef
unexpand
uniq
uniqstr
unshift
Expand Down
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion lib/App/PTP/Cheat_Sheet.pod
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ C<">, or C<$> in all I<code> arguments
=item B<--re> I<engine>, B<--regex-engine>: use the specified regex engine (e.g.
I<RE2>, I<PCRE>, I<TRE>, I<GNU>, 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
Expand All @@ -183,6 +183,11 @@ I<string>, I<filename>, and I<command> ones (unless B<-Q> has been passed).

=over 8

=item B<Dumper>, B<fileparse>, B<basename>, B<dirname>, B<copy>, B<move>,
B<make_path>, B<remove_tree>, B<canonpath>, B<any>, B<all>, B<none>, B<min>,
B<max>, B<sum>, B<pi>, B<acos>, B<expand>, B<unexpand>, B<$tabstop>, B<wrap>,
B<fill>, B<$columns>, ...

=item B<$_>: current line content (I<RW>)

=item B<$f>, B<$F>: current file name, current absolute file name (I<RO>)
Expand Down
22 changes: 22 additions & 0 deletions lib/App/PTP/Commands.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions script/ptp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Data::Dumper> (C<Dumper>, with C<Terse> set to true by default),
L<File::Basename> (C<fileparse>, C<basename>, C<dirname>), L<File::Copy>
(C<copy>, C<move>), L<File::Path> (C<make_path>, C<remove_tree>),
L<File::Spec::Functions> (C<canonpath>, ...), L<List::Util> (C<any>, C<all>,
C<none>, C<min>, C<max>, C<sum>, ...), L<Math::Trig> (C<pi>, C<acos>, ...),
L<Text::Tabs> (C<expand>, C<unexpand>, C<$tabstop>), L<Text::Wrap> (C<wrap>,
C<fill>, C<$columns>, ...).

=head3 B<$_>

This variable is set to the current line being processed. In most context (but
Expand Down
6 changes: 4 additions & 2 deletions t/300-perlenv.t
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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');
}

0 comments on commit 6091488

Please sign in to comment.