package lexical; use v5.22; use strict; use warnings; use Filter::Util::Call ; =head1 NAME lexical - Lexical use of exported functions =head1 VERSION Version 0.01 =cut our $VERSION = '0.01'; =head1 SYNOPSIS Pragma to import subs as lexical_subs into the current scope use lexical "Scalar::Util" => qw/looks_like_number/; print looks_like_number("a15"); =head1 EXPORT No classic exports, the namespace isn't polluted =head1 SUBROUTINES/METHODS =head2 import =cut sub import { my ($my_pkg, $module, @imports) = @_; eval "use $module"; # TODO quick & dirty my $code = q(use experimental 'lexical_subs','refaliasing';); $code .= << "__CODE__" for @imports; # TODO only subs ATM my sub $_; \\&$_ = \\&${module}::$_; __CODE__ upject($code); } sub upject { my $injection = shift; # --- exit if undef return unless defined $injection; # --- adjust line number to disguise injection my ($file,$line) = (caller(2))[1,2]; $line++; $injection .= qq{\n# line $line "$file"\n}; #warn $injection; # --- add source filter filter_add ( sub { my $status = filter_read_exact(1); # read one char into $_ if ( $status > 0) { $_ = $injection .";".$_; # prepend code once filter_del(); # delete source filter } $status ; } ); } =head1 AUTHOR Rolf Michael Langsdorf, C<< >> # yadda yadda ... 1; # End of lexical