in reply to Re^2: Beware of global! And bless the local!
in thread Beware of global! And bless the local!

"I should adopt anonymous blocks as a every-day programming technique"

Anonyblocks are just another tool in the toolset. I use them primarily in unit test files to separate out tests of a feature or method where I need to instantiate a new object for a test sequence.

For situations such as yours, I'd probably opt for a function instead of a block that's inline with the code. A subroutine provides the same scoping as the block does:

sub slurp_file { my ($fname) = @_; local $/; open my $fh, '<', $fname or die "Can't open damned '$fname' file: +$!"; my $data = <$fh>; close $fh or die $!; return $data; }

To each their own, there's more than one way to do it!

Replies are listed 'Best First'.
Re^4: Beware of global! And bless the local!
by Your Mother (Archbishop) on Dec 12, 2019 at 17:10 UTC
    I use [Anonyblocks] primarily in unit test files to separate out tests of a feature or method where I need to instantiate a new object for a test sequence

    This is also my primary use case. So I can have the same variable names repeated and don’t end up with $mech1 .. $mech10 or $json_116.

      Exactly. Here's an example.

      I also use blocks in many of my test files where I'm performing tests against multiple functions or methods all within the same test file to make the sections more easily visible, and provide me with the ability to fold the sections in my IDE. Here's an example of that.

Re^4: Beware of global! And bless the local!
by Fletch (Bishop) on Dec 12, 2019 at 17:59 UTC

    I have the following yasnippets defined in my emacs.

    $ cat ~/.emacs.d/snippets/cperl-mode/slurp # -*- snippet -*- #key: slurp #group: slurp #name: Slurp file-handle # -- my $${1:variable} = do { local $/; <$${2:fh}>; };
    $ cat ~/.emacs.d/snippets/cperl-mode/slurp.open # -*- snippet -*- #key: slurp #group: slurp #name: Open and slurp # -- my $${1:variable} = do {$> local $/;$> open( my $${2:fh}, q{<}, ${3:$WHAT} )$> or die qq{Can't open "$3": $!\n}; <$$2>;$> };$>

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.