App-PTP

 view release on metacpan or  search on metacpan

lib/App/PTP/Commands.pm  view on Meta::CPAN

}

# prepare_re('re', \%options)
# Applies the modal option on the given regex.
# This function is not exported.
sub prepare_re {
  my ($re, $modes) = @_;
  $re = base_prepare_re($re, $modes);
  my $r;
  if ($modes->{regex_engine} ne 'perl') {
    # Some play to correctly escape whatever special characters might be in the
    # regex while preserving its semantics. This relies on the fact that the
    # 'Terse' option of Data::Dumper is set in the main program.
    # The regex-engine variable has been validated in the Args module.
    my $str_re = Dumper($re);
    my $engine = 're::engine::'.$modes->{regex_engine};
    # We could the safe to avoid this perlcritic warning but we are not actually
    # executing code here, so the safe is not needed (and we would need to find
    # variable names that are guaranteed not to collide with user code).
    $r = eval "use ${engine}; \$re = $str_re; qr/\$re/s";  ## no critic (ProhibitStringyEval)
    if ($@) {

script/ptp  view on Meta::CPAN


Filter each input to keep only the lines that match the given regular
expression. That expression cannot have delimiters (e.g. /foo/) so, if you
want to pass options to the regex, you need to use the group syntax (e.g.
(?i)foo).

If you use parenthesis, you probably want to enclose the expression in single
quotes, to prevent the shell from interpreting them.

This command is much faster then manually giving a match operation to the
B<--filter> command, because the code does not need to be escaped.

This operation can be made case-insensitive with the B<-I> flag, inverted with
B<-V> and the regex can be interpreted as an exact string with B<-Q>.

=item B<-s> I<regex> I<string>, B<--substitute>

Replace all matches of the given regular expression by the given substitution
text on each line of the input. The substitution string is evaluated like a
Perl string, so it can contain references to capture group in the regular
expression using the B<$1>, B<$2>, etc. syntax.

t/201-command-substitute.t  view on Meta::CPAN

  my $data = ptp([qw(-s oo o'o)], 'default_small.txt');
  is($data, "fo'obar\nfo'obaz\n\nlast\n", 'add quote');
}{
  my $data = ptp([qw(-s oo o"o\")], 'default_small.txt');
  is($data, "fo\"o\"bar\nfo\"o\"baz\n\nlast\n", 'add double quote');
}{
  my $data = ptp([qw(-Q -s oo o"o\")], 'default_small.txt');
  is($data, "fo\"o\\\"bar\nfo\"o\\\"baz\n\nlast\n", 'add double quote quoted');
}{
  # A '\\' in the string is evaluated like a single 'real' \ when building the
  # string, which then become an escape character when the regex is evaluated.
  my ($data, $err) = ptp(['-s', 'oo(.)', 'oo\\\\$1'], 'default_small.txt');
  is($data, "foo\\bar\nfoo\\baz\n\nlast\n", 'add backslash') or diag $err;
}{
  my $data = ptp([qw(-s oo(.) oo/$1)], 'default_small.txt');
  is($data, "foo/bar\nfoo/baz\n\nlast\n", 'add slash');
}{
  # Same here. Funily (...), /o, \/o, \\/o an \\\/o are all equivalent, but then
  # \\\\/o starts becoming something else...
  my $data = ptp([qw(-s oo(.) o\\\\/o/$1)], 'default_small.txt');
  is($data, "fo\\/o/bar\nfo\\/o/baz\n\nlast\n", 'add slashes');

t/208-command-shell.t  view on Meta::CPAN

  is($out, "", 'eat');
}{
  my $temp = File::Temp->new();
  my $f = $temp->filename;
  my $out = ptp(['--shell', "echo \"foo\" > $f"], \"foo\nbar\nbaz\n");
  is(slurp($f), "foo\n", 'command with quote');
}{
  my $temp = File::Temp->new();
  my $f = $temp->filename;
  my $out = ptp(['-Q', '--shell', "echo \"foo\" > $f"], \"foo\nbar\nbaz\n");
  is(slurp($f), "foo\n", 'escape command with quote');
}{
  my $temp = File::Temp->new();
  my $f = $temp->filename;
  my $out = ptp(['-e', '$a = "abc"', '--shell', "echo \"\$a\" > $f"], \"foo\nbar\nbaz\n");
  is(slurp($f), "abc\n", 'interpolate variable');
}

done_testing();



( run in 0.373 second using v1.01-cache-2.11-cpan-c21f80fb71c )