Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##### main.pl
- #! /usr/bin/env perl
- use lib q<.>;
- use warnings;
- use strict;
- use utf8;
- use feature qw<say>;
- use MyFuncs qw<add minus multi divide>;
- say add(12, 34);
- say minus(12, 34);
- say multi(12, 34);
- say divide(12, 34);
- # say "\n";
- # for my $key (sort keys %::) {
- # say "${key}=>", $::{$key};
- # }
- exit(0);
- ##### MyFuncs.pm
- package MyFuncs;
- use 5.006;
- use strict;
- use warnings;
- use MyExporter qw<import>;
- our @EXPORT_OK = qw<add minus multi divide>;
- sub add {
- my ($a, $b) = @_;
- $a + $b;
- }
- sub minus {
- my ($a, $b) = @_;
- $a - $b;
- }
- sub multi {
- my ($a, $b) = @_;
- $a * $b;
- }
- sub divide {
- my ($a, $b) = @_;
- die "Can't divide by $b" if $b == 0;
- $a / $b;
- }
- 1; # End of MyFuncs
- ##### MyExporter.pm
- package MyExporter;
- use 5.006;
- use strict;
- use warnings;
- use feature qw<say>;
- use List::Util qw<any>;
- sub import {
- my ($pack, @funcs) = @_;#MyExporter @funcs
- my ($c_pack) = caller;#MyFuncs
- if (any{$_ eq 'import'} @funcs) {
- no strict qw<refs>;
- *{"${c_pack}::import"} = \&{"${pack}::myImport"};
- }
- }
- sub myImport {
- my ($pack, @funcs) = @_;#MyFuncs @funcs
- my ($c_pack) = caller;#main
- {
- no strict qw<refs>;
- if (@{"${pack}::EXPORT_OK"}) {
- for my $func (@funcs) {
- if (any{$_ eq $func} @{"${pack}::EXPORT_OK"}) {
- *{"${c_pack}::${func}"} = \&{"${pack}::${func}"};
- }else {
- die "Can't find \"$func\" in the export list";
- }
- }
- }
- }
- }
- 1; # End of MyExporter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement