Perlito5

 view release on metacpan or  search on metacpan

src/Perlito5/Grammar/Use.pm  view on Meta::CPAN

            $Perlito5::FILE_NAME,      # current file name being compiled
            $Perlito5::LINE_NUMBER,    # current line number being compiled
        ],

        # "require" the module
        my $filename = modulename_to_filename($module_name);
        # warn "# require $filename\n";
        # require $filename;
        #   use the compiler "require" instead of native Perl
        #   because we need to add BEGIN-block instrumentation
        unshift @Perlito5::CALLER, [ $module_name ];
        Perlito5::Grammar::Use::require($filename);
        shift @Perlito5::CALLER;

        if (!$skip_import) {
            # call import/unimport
            if ($use_or_not eq 'use') {
                my $code = $module_name->can('import');
                if (defined($code)) {
                    # make sure that caller() points to the current module under compilation
                    unshift @Perlito5::CALLER, $caller;
                    eval {
                        # "package $current_module_name;\n"
                        $module_name->import(@$arguments);
                        1
                    }
                    or Perlito5::Compiler::error $@;
                    shift @Perlito5::CALLER;
                }
            }
            elsif ($use_or_not eq 'no') {
                my $code = $module_name->can('unimport');
                if (defined($code)) {
                    # make sure that caller() points to the current module under compilation
                    unshift @Perlito5::CALLER, $caller;
                    eval {
                        # "package $current_module_name;\n"
                        $module_name->unimport(@$arguments);
                        1
                    }
                    or Perlito5::Compiler::error $@;
                    shift @Perlito5::CALLER;
                }
            }
        }
    }
    else {
        # force "use" code to be inlined instead of eval-ed
        bootstrapping_use($ast);
        emit_time_eval($ast);
    }

    return Perlito5::Grammar::Block::ast_nop();
}

sub emit_time_eval {
    my $ast = shift;

    if ($ast->{mod} eq 'strict') {
        if ($ast->{code} eq 'use') {
            strict->import();
        }
        elsif ($ast->{code} eq 'no') {
            strict->unimport();
        }
    }
}

sub modulename_to_filename {
    my $s = shift;
    $s = $Perlito_internal_module{$s}
        if exists $Perlito_internal_module{$s};
    $s =~ s{::}{/}g;
    return $s . '.pm';
}

sub filename_lookup {
    my $filename = shift;

    if ( exists $INC{$filename} ) {
        return "done" if $INC{$filename};
        Perlito5::Compiler::error "Compilation failed in require";
    }

    if (substr($filename, 0, 2) eq './') {
        if (-f $filename) {
            $INC{$filename} = $filename;
            return "todo";
        }
    }
    for my $prefix (@INC) {
        for my $internal (@Perlito_internal_lib_directory) {
            my $realfilename = join( "/",
                $prefix,
                ($internal ? $internal : ()),
                $filename
            );
            if (-f $realfilename) {
                $INC{$filename} = $realfilename;
                return "todo";
            }
        }
    }
    Perlito5::Compiler::error "Can't locate $filename in \@INC ".'(@INC contains '.join(" ",@INC).').';
}

sub bootstrapping_use {
    # force "use" code to be inlined instead of eval-ed
    my $stmt = shift;

    my $module_name = $stmt->{mod};

    my $filename = modulename_to_filename($module_name);

    return 
        if filename_lookup($filename) eq "done";

    # say "  now use: ", $module_name;
     
    # TODO - look for a precompiled version



( run in 2.328 seconds using v1.01-cache-2.11-cpan-437f7b0c052 )