source: perl/lib/BarnOwl/ModuleLoader.pm @ cb54527

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since cb54527 was 9729dba, checked in by Nelson Elhage <nelhage@mit.edu>, 17 years ago
Tighten up the reloaded regex a little.
  • Property mode set to 100644
File size: 1.8 KB
Line 
1use strict;
2use warnings;
3
4package BarnOwl::ModuleLoader;
5
6use lib (BarnOwl::get_data_dir() . "/modules/");
7use PAR (BarnOwl::get_data_dir() . "/modules/*.par");
8use PAR ($ENV{HOME} . "/.owl/modules/*.par");
9
10sub load_all {
11    my %modules;
12    my @modules;
13   
14    for my $dir ( BarnOwl::get_data_dir() . "/modules",
15                  $ENV{HOME} . "/.owl/modules" ) {
16        opendir(my $dh, $dir) or next;
17        while(defined(my $f = readdir($dh))) {
18            next if $f =~ /^\./;
19            if(-f "$dir/$f" && $f =~ /^(.+)\.par$/) {
20                $modules{$1} = 1;
21            } elsif(-d "$dir/$f" && -d "$dir/$f/lib") {
22                push @INC, "$dir/$f/lib" unless grep m{^$dir/$f/lib$}, @INC;
23                $modules{$f} = 1;
24            }
25        }
26        @modules = grep /\.par$/, readdir($dh);
27        closedir($dh);
28        for my $mod (@modules) {
29            my ($class) = ($mod =~ /^(.+)\.par$/);
30            $modules{$class} = 1;
31        }
32    }
33    for my $class (keys %modules) {
34        if(!defined eval "use BarnOwl::Module::$class") {
35            BarnOwl::error("Unable to load module $class: $!") if $!;
36            BarnOwl::error("Unable to load module $class: $@") if $@;
37        }
38    }
39}
40
41sub reload {
42    my $class = shift;
43    for my $m (keys %INC) {
44        delete $INC{$m} if $m =~ m{^BarnOwl/};
45    }
46    # Restore core modules from perlwrap.pm
47    $INC{$_} = 1 for (qw(BarnOwl.pm BarnOwl/Hooks.pm
48                         BarnOwl/Message.pm BarnOwl/Style.pm));
49
50    $BarnOwl::Hooks::startup->clear;
51    local $SIG{__WARN__} = \&squelch_redefine;
52    $class->load_all;
53    $BarnOwl::Hooks::startup->run(1);
54    BarnOwl::startup() if *BarnOwl::startup{CODE};
55}
56
57sub squelch_redefine {
58    my $warning = shift;
59    warn $warning unless $warning =~ /^Subroutine .+ redefined at/;
60}
61
621;
Note: See TracBrowser for help on using the repository browser.