Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Fixed a year later...

by tobyink (Canon)
on Mar 26, 2013 at 09:54 UTC ( [id://1025480]=note: print w/replies, xml ) Need Help??


in reply to Fixed a year later...
in thread can't import using exporter

Personally I'd do this:

use strict; use warnings; BEGIN { package MyUtils; no thanks; use base "Exporter"; our @EXPORT = "shout"; sub shout { print @_, "!!!\n"; } 1; }; use MyUtils; shout "it works";

I don't have any problem with the BEGIN block. I'd always wrap inline packages in braces anyway, so it's just five extra letters before the opening brace.

If I want to factor out MyUtils into a separate file, it's just a matter of cutting everything inside the braces and pasting it into MyUtils.pm; then replace the now empty BEGIN {} block with use MyUtils;. Easy. (And if I was being a perfectionist, I could remove no thanks; from MyUtils.pm as it becomes superfluous.)

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Replies are listed 'Best First'.
Re^2: Fixed a year later...
by perl-diddler (Chaplain) on Mar 27, 2013 at 23:24 UTC
    To each their own.

    When I see a BEGIN block, it interrupts the structure of the code.

    Also I find a need to re-use strict & warnings inside of different packages.

    I tend to use alot of packages in writing programs as a means to create typed-classes. I.e. if I need a structure, I want a type for it and that usually means a class/package.

    But for most data types/structures, I wouldn't see them getting large enough or important enough to split into separate files. Most of my code writing was in C -- and I liked typedefs and structures for grouping data. But one virtually never thinks of putting each typedef and structure into a separate file.

    One also doesn't like seeing lots of #if/#define/#else#endif's in C-code as they disrupt the normal indentation and the *visual* flow of the code. I like to see the look of the code reflect its function and having an ALLCAPS standoff block just doesn't look attractive. It highlights something unimportant -- that you need to make sure your defines are executed in phase1 of the perl interpreter, in order that they work in phase2. To me, users shouldn't have to think about those things -- they should just 'work'. So rather than having my attention drawn to quirks specific to perl, I can focus my attention on the underlying algorithm. Using BEGIN or referencing $INC{xxx}, takes my attention away from the underlying algorithm and makes focusing on it more difficult. But that's a quirk of my programming! ;-)

    Someone on a list recently said they WANTED their usage of quirks to stand out... *ouch*... to me, those are things to gloss over, not emphasize.

    Oh well, as I said, to each their own...

Log In?
Username:
Password:

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

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

    No recent polls found