public
Created

A basic use_from implementation

  • Download Gist
usex-declare.pm
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
package UseX::Declare;
 
use strict;
use warnings;
 
use Carp;
 
use Devel::Declare ();
 
my $verbose = 0;
 
sub import {
my $class = shift;
my $opts = shift;
 
$verbose = $opts->{verbose} || 0;
 
my $caller = caller;
 
Devel::Declare->setup_for(
$caller,
{ 'use_from' => { const => \&rewrite_use_from } }
);
no strict 'refs';
*{$caller.'::use_from'} = sub {};
 
}
 
sub rewrite_use_from {
no strict 'refs';
 
my $linestr = Devel::Declare::get_linestr;
print STDERR "Got: $linestr" if $verbose;
 
my $caller = Devel::Declare::get_curstash_name;
 
$linestr =~ s/use_from\s+\$(\w+)/
my $varname = $caller . '::' . $1;
my $module =
(defined ${$varname})
? ${$varname}
: croak "Cannot access variable \$$varname";
"use_from; use $module"
/e;
print STDERR "Rewritten: $linestr" if $verbose;
 
Devel::Declare::set_linestr($linestr);
}
 
1;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.