Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Exporting use strict/warnings into main::

by nysus (Parson)
on May 15, 2020 at 14:43 UTC ( #11116817=perlquestion: print w/replies, xml ) Need Help??

nysus has asked for the wisdom of the Perl Monks concerning the following question:

I have a module using exporter:

package JSON::Karabiner::Manipulator ; use strict; use warnings; use Carp; use Exporter 'import'; our @EXPORT = qw'new_manipulator add_action add_description add_condit +ion add_parameter add_key_code add_key_code add_any add_optional_modifiers add_mandatory_modifiers +add_simultaneous add_simultaneous_options add_consumer_key_code add_p +ointing_button add_shell_command add_select_input_source add_set_vari +able add_mouse_key add_modifiers add_identifier add_description add_value add_bundle_identifiers add_ +file_path add_input_source add_keyboard_types add_variable add_descri +ption _dump_json';

When a user adds this package with use use, I'd like it to automatically inject/export the use strict; use warnings pragmas so they don't have to add it to their script. I've seen other packages do this but I can't figure out how. Thanks.

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Exporting use strict/warnings into main::
by haj (Curate) on May 15, 2020 at 16:01 UTC
    So you want to export your own symbols, using Exporter's import capabilities and strict/warnings... In that case, you should not import Exporter's import method, but roll out your own - and call the original afterwards, like this:
    use Exporter; sub import { strict->import; warnings->import; goto &Exporter::import; }

      Ah, very smooth. Thanks!

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

Re: Exporting use strict/warnings into main::
by clueless newbie (Curate) on May 15, 2020 at 17:27 UTC

      Yes ++

      updated: minidb dumper, thx marioroy

      From a bundle I use a lot:

      use strict; use warnings; package My::Bundle { use Data::Dumper::AutoEncode (); use Data::Rmap 'rmap_ref'; use Import::Into; use parent 'Exporter'; our @EXPORT = 'Dumper'; sub import { shift->export_to_level(1); strict ->import::into(1); warnings ->import::into(1); strictures ->import::into(1, 2 => 1); feature ->import::into(1, 'say', 'state'); utf8 ->import::into(1); autodie ->import::into(1); Encode->import::into(1); List::Util->import::into(1, qw/ any all first max maxstr min m +instr sum sum0 pairkeys pairvalues pairgrep pairmap shuffle uniq /); Method::Signatures->import::into(1); Path::Tiny->import::into(1); Time::Piece->import::into(1); Time::Seconds->import::into(1); Try::Tiny->import::into(1); } # These are global vars; localize them unless # you want *everything* to be changed $Data::Dumper::Deepcopy = 1; $Data::Dumper::Indent = 1; $Data::Dumper::Purity = 1; $Data::Dumper::Quotekeys = 0; $Data::Dumper::Sortkeys = 1; $Data::Dumper::Terse = 1; sub Dumper { rmap_ref { if ( my $class = ref($_) ) { $class = $_->blessed if $class eq 'MCE::Shared::Object +'; if ( $class->isa('MCE::Shared::Array') ) { $_ = [ $_->can('export') ? @{ $_->export } : $_->v +alues ]; } elsif ( $class->isa('MCE::Shared::Hash') ) { $_ = { $_->can('export') ? %{ $_->export } : $_->p +airs }; } elsif ( $class->isa('MCE::Shared::Minidb') ) { my $db = $_; $_ = $db->lkeys ? [ { $db->hpairs }, { $db->lpairs + } ] : [ { $db->hpairs } ]; } elsif ( $class->isa('MCE::Shared::Ordhash') ) { $_ = $_->clone; } elsif ( $class->isa('MCE::Shared::Scalar') ) { $_ = $_->get; } } } @_; return Data::Dumper::AutoEncode::eDumper(@_); } }; 1; # return true

      Hope this helps!


      The way forward always starts with a minimal test.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11116817]
Approved by marto
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2021-09-25 17:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?