pmpmmpmp has asked for the wisdom of the Perl Monks concerning the following question:

I want to use re::engine::TRE for some of the regular expressions in my script. However, there are other regular expressions in the same script in which I want to use Perl's default regex engine. What is the best way to go about doing this ? How do I restrict the use of re::engine::TRE to particular regex's ?

Replies are listed 'Best First'.
Re: restrict use of regex module in script
by LanX (Saint) on Apr 07, 2017 at 19:45 UTC

      Thank you for your answers. OK, so this was very simple once I tried it out on a very small piece of code instead of what I was working on. I did not realize that you could just stick {} pretty much anywhere you want. Example below, in case anyone is interested.

      #!/usr/bin/perl use strict; use warnings; use re::engine::TRE max_cost => 5; my $seq1 = "TTTTACGAGAGAATATGTTAGGTGAAGGAACCTCTATCTGAGAGAAAAA"; my $seq2 = "TTTTGAGCTCGTTGTCGATCCGAGGTACTTTTGAATCCGCAGTTTCTTG"; if ("A pearl is a hard object produced ..." =~ /\(Perl\)/i) { print "$1\n"; } if (($seq2 =~ /GTTGTTCGATCCAGGTAC/) && ($seq1 =~ /ACGAGAGATAGATGA/)) { print "Have a match\n"; }

      This will print 'pearl' and "Have a match".

      #!/usr/bin/perl use strict; use warnings; my $seq1 = "TTTTACGAGAGAATATGTTAGGTGAAGGAACCTCTATCTGAGAGAAAAA"; my $seq2 = "TTTTGAGCTCGTTGTCGATCCGAGGTACTTTTGAATCCGCAGTTTCTTG"; if ("A pearl is a hard object produced ..." =~ /\(Perl\)/i) { print "$1\n"; } { use re::engine::TRE max_cost => 5; if (($seq2 =~ /GTTGTTCGATCCAGGTAC/) && ($seq1 =~ /ACGAGAGATAGATGA/)) { print "Have a match\n"; } }

      This will only print out "Have a match".

        "I did not realize that you could just stick {} pretty much anywhere you want."

        That's called a "block", and they can't quite go anywhere, but pretty close.

        Blocks are how we limit the "scope" of something. Anything defined within that block is limited to that scope. This is where my and local for example come in to play. Here's a couple of examples:

        use warnings; use strict; my $thing = 123; { my $thing = 456; } print $thing; # 123 open my $fh, '<', 'file.txt' or die $!; { local $/; # slurp in a file my $contents = <$fh> ... } # now we go back to reading a file line-by-line again while (<$fh>){ ... }

        Subroutines (functions/methods) are blocks as well, and some functions even take blocks as parameters (or operate in "block mode" so it appears to take a block as a param):

        my @list = map {$_ => 1} @other_list; my @filtered_list = grep {$_ =~ /blah/} @some_list;

        ...etc.

        Please see AnomalousMonk's post here for how my above example is actually quite broken if taken literally. Haste took rank, but we have people who correct hasty things thankfully. I should have added an "untested" warning or better.

Re: restrict use of regex module in script
by Laurent_R (Canon) on Apr 07, 2017 at 20:38 UTC
    Cross posted on Perl Gurus: http://perlguru.com/gforum.cgi?post=83925;#83925

    There is nothing wrong with cross-posting questions, but it is considered polite to inform the reader and provide links, to avoid duplication of work in various parts of the Internet.