Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Conditional and opt-in breaking change: is this design viable and my use of 'import' OK?

by Anonymous Monk
on Oct 05, 2024 at 09:30 UTC ( [id://11162068]=note: print w/replies, xml ) Need Help??


in reply to Re: Conditional and opt-in breaking change: is this design viable and my use of 'import' OK?
in thread Conditional and opt-in breaking change: is this design viable and my use of 'import' OK?

Thank you very much for detailed answer, I appreciate. Never seen __PACKAGE__ for "true" at the end of module, learning something new each day.

Constants go to dedicated module and "modern" user is told to "use" it. Gratefully accepted.

To answer your other comment: yes, the OOP, shared code/behaviour and separate data/properties, I have some vague idea. If "import" redefines the subroutines, it's once and for all. Though I didn't expect such mixed usage scenario, I planned to eventually do at least something about it, like (assuming $MODE undefined initially):

sub import { my ( $self, $flag ) = @_; if ( defined $flag and $flag eq ':modern' ) { if ( defined $MODE ) { if ( $MODE == 0 ) { warn "Note: Foo is in classic mode\n" } } else { $MODE = 1; Foo-> export_to_level( 1, 'Foo', keys %const ); no warnings 'redefine'; *Foo::m2 = \&Foo::Modern::m2; # etc. # longer list } } else { if ( defined $MODE ) { if ( $MODE == 1 ) { warn "Note: Foo is in modern mode\n" } } else { $MODE = 0 } } }

If someone does just (or mixes with other uses) "require Foo;" or "use Foo();" it'll be considered an act of direct sabotage ;-) But in the end I'll probably abandon the idea to abuse "import", therefore doesn't matter. I don't understand, however, what's the benefit of "Builder". The "script.pl" is written by the end user, all she wants is simply "use Foo;". If asked instead from this day on to:

use Foo::Builder; my $builder = 'Foo::Builder'->new; my $object = $builder->build('classic');

then I'm afraid to be pointed at the door. Or if in less grumpy mood/mode, she'll agree to hit a few more keys to "use Foo::Classic;". And if "modern" user happens nearby, he'll say "Aha, I'll do use Foo::Modern; use Foo::Constants; then!"

I was hoping to use a single ternary to check a flag in, say, 60 lines method (and there are several such methods) so that 2 of these lines are executed in this or that mode; instead of keeping almost exact duplicates in two packages. I'll think more about it and ask further. Thank you.

  • Comment on Re^2: Conditional and opt-in breaking change: is this design viable and my use of 'import' OK?
  • Select or Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2025-01-24 16:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (68 votes). Check out past polls.