Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Beware of global! And bless the local!

by alexander_lunev (Pilgrim)
on Dec 12, 2019 at 13:26 UTC ( [id://11110010]=note: print w/replies, xml ) Need Help??


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

Hello, kcott!

I should adopt anonymous blocks as a every-day programming technique, thank you. As with the simple but powerful Truth of local, it's also always under my nose, and still I'm not using it - but I will!

As for unnecessary things like "= undef" and "close $fh" - I like it this way, it's more informative and illustrative this way. It takes more chars to write, but less mind time-clocks to comprehend the meaning. I like to write explicit "return $string;" instead of laconic "$string" in the end of the sub. I think that a programming language should stay language that we speak and understand, and I don't want to short-cut to the point in expense of explanation. In the end I'm speaking this language with myself-in-the-future, and it's for me I write those many words that could be omitted.

Replies are listed 'Best First'.
Re^3: Beware of global! And bless the local!
by stevieb (Canon) on Dec 12, 2019 at 15:58 UTC
    "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!

      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.

      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.

Re^3: Beware of global! And bless the local!
by kcott (Archbishop) on Dec 13, 2019 at 05:01 UTC
    "As for unnecessary things like "= undef" and "close $fh" ..."

    That's absolutely fine and your reasoning is sound. As I said: "I'm not saying don't use them: just advising that they're optional".

    "I think that a programming language should stay language that we speak and understand, ..."

    The first time I ever saw a Perl script — a very long time ago; I'm pretty sure it was Perl 3 — I was absolutely amazed that I was able to understand most of it at a glance.

    — Ken

Log In?
Username:
Password:

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

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

    No recent polls found