Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

What is the point of this coding style?

by morgon (Curate)
on Nov 20, 2013 at 05:36 UTC ( #1063444=perlquestion: print w/replies, xml ) Need Help??
morgon has asked for the wisdom of the Perl Monks concerning the following question:


I have seen in many places a coding convention that looks like this:

package MooseX::Types::TypeDecorator; { $MooseX::Types::TypeDecorator::VERSION = '0.35'; } use strict; use warnings; # and so on

I assume that the author wants to have the version number in a prominent place and so is willing to put one line to trivial code outside of strict but what is the point of putting the assignment to a package variable into an extra block?

Why not simply

package MooseX::Types::TypeDecorator; use strict; use warnings; $MooseX::Types::TypeDecorator::VERSION = '0.35';
Is that just personal taste or is there something to it that I miss?

I just wonder...

Replies are listed 'Best First'.
Re: What is the point of this coding style?
by tobyink (Abbot) on Nov 20, 2013 at 08:10 UTC

    MooseX-Types is managed using Dist::Zilla which rewrites some of the source code when it gets published. In the MooseX-Types repository on Github, there is no $VERSION defined at all; this is added when it gets published. When Dist::Zilla adds $VERSION lines it always does so right underneath the package statement, thus it ends up above the use strict line.

    I'm not quite sure of the reason for the block. Possibly Dist::Zilla has a policy of wrapping any inserted code in a block to prevent lexical leakage; in this case there would not be any leakage anyway, but perhaps such a policy is automatically applied to all inserted code.

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: What is the point of this coding style?
by moritz (Cardinal) on Nov 20, 2013 at 06:27 UTC

    I can just speculate that people have a deeply ingrained habit of keeping scopes small, either for things like no warnings 'once';, or for variable declarations.

    Of course that would only apply if the code was

    { our $VERSION = '0.35'; }

    but old habits die hard.

Re: What is the point of this coding style? (pause indexer/dzil)
by Anonymous Monk on Nov 20, 2013 at 08:16 UTC

    Maybe its to make pause indexer job slightly easier ... or some fuzzy memory about doing that :) probably not though :)

    Or , more likely, maybe its generated/managed/synchronized/automagically by dzil (one of its plugins) or another such tool

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1063444]
Approved by GrandFather
Front-paged by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2017-06-27 23:27 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (617 votes). Check out past polls.