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

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

Replies are listed 'Best First'.
Re^4: restrict use of regex module in script
by AnomalousMonk (Archbishop) on Apr 08, 2017 at 00:02 UTC
    { 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

Re^4: restrict use of regex module in script
by BillKSmith (Monsignor) on Apr 08, 2017 at 12:20 UTC
    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 ! )