Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: restrict use of regex module in script

by pmpmmpmp (Novice)
on Apr 07, 2017 at 22:07 UTC ( [id://1187435]=note: print w/replies, xml ) Need Help??


in reply to Re: restrict use of regex module in script
in thread restrict use of regex module in script

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".

Replies are listed 'Best First'.
Re^3: restrict use of regex module in script
by stevieb (Canon) on Apr 07, 2017 at 23:08 UTC
    "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.

      { local $/; # slurp in a file my $contents = <$fh> ... } # now we go back to reading a file line-by-line again while (<$fh>){ ... }

      Parenthetic note: in the quoted example code, the  $fh file handle would become "exhausted" after the "slurp" read to  $contents and the subsequent while-loop read of the handle would produce nothing. A seek operation is needed to reset the logical file position back to the beginning of the file (update: or at least to somewhere other than the end) so that subsequent reads may be successful. Try the following code without the seek statement:

      c:\@Work\Perl\monks\pmpmmpmp>perl -wMstrict -le "open my $fh, '<', 'junk.txt' or die $!; ;; my $contents = do { local $/; <$fh>; }; print qq{[[$contents]]}; ;; seek $fh, 0, 0; print 'now we go back to reading a file line-by-line again'; while (<$fh>){ print qq{<<$_>>}; } " [[line 1 fee fie foe line 2 wibble wobble line 3 blah yada ]] now we go back to reading a file line-by-line again <<line 1 fee fie foe >> <<line 2 wibble wobble >> <<line 3 blah yada >>


      Give a man a fish:  <%-{-{-{-<

        Indeed. I was going to create a second handle for the latter example, but forgot :)

        update: I've updated my original reply to the OP to point to your update, which technically is extremely informative./update

      When a special variable is intended to apply to only one operation, I prefer to make that explicit by limiting the scope even more.
      my $contents = do{local $/; <$fh>};
      Bill

        Thank you stevieb and Bill for your examples and explanation. They have been very helpful. ( I like the seek function ! )

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1187435]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-24 10:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found