Changeset f2f5815 for perlwrap.pm


Ignore:
Timestamp:
Nov 2, 2006, 10:19:15 PM (14 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, barnowl_perlaim, debian, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
0ff8d110
Parents:
591ea418
Message:
Refactoring hook code
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perlwrap.pm

    r591ea418 rf2f5815  
    284284package owl;
    285285
    286 # Arrays of function pointers to be called at specific times.
     286# Arrays of subrefs to be called at specific times.
    287287our @onStartSubs = ();
    288 our @onReceiveMsg = undef;
    289 our @onMainLoop = undef;
    290 our @onGetBuddyList = undef;
     288our @onReceiveMsg = ();
     289our @onMainLoop = ();
     290our @onGetBuddyList = ();
    291291
    292292################################################################################
     
    306306$reload = 0;
    307307
    308 sub owl::mainloop_hook
    309 {
    310     foreach (@onMainLoop)
    311     {
    312         &$_();
    313     }
    314     return;
     308# Functions to call hook lists
     309sub runHook($@)
     310{
     311    my $hook = shift;
     312    my @args = @_;
     313    $_->(@args) for (@$hook);
     314}
     315
     316sub runHook_accumulate($@)
     317{
     318    my $hook = shift;
     319    use Data::Dumper;
     320    my @args = @_;
     321    return join("\n", map {$_->(@args)} @$hook);
     322}
     323
     324sub mainloop_hook
     325{
     326    runHook(\@onMainLoop);
    315327}
    316328
     
    318330# Startup and Shutdown code
    319331################################################################################
    320 sub owl::startup
     332sub startup
    321333{
    322334# Modern versions of owl provides a great place to have startup stuff.
     
    325337}
    326338
    327 sub owl::shutdown
     339sub shutdown
    328340{
    329341# Modern versions of owl provides a great place to have shutdown stuff.
     
    333345# should terminate.
    334346    $shutdown = 1;
    335     owl::mainloop_hook();
     347    mainloop_hook();
    336348}
    337349
     
    347359
    348360    loadModules();
    349     foreach (@onStartSubs)
    350     {
    351         &$_();
    352     }
     361    runHook(\@onStartSubs);
    353362}
    354363################################################################################
     
    395404################################################################################
    396405
    397 sub loadModules ()
    398 {
    399 my @modules;
    400 foreach my $dir (owl::get_data_dir()."/owl/modules", $ENV{HOME}."/.owl/modules") {
    401   opendir(MODULES, $dir);
    402   # source ./modules/*.pl
    403   @modules  =  grep(/\.pl$/, readdir(MODULES));
    404 
    405 foreach my $mod (@modules) {
    406   do "$dir/$mod";
    407 }
    408 closedir(MODULES);
    409 }
    410 }
     406sub loadModules () {
     407    my @modules;
     408    foreach my $dir ( owl::get_data_dir() . "/owl/modules",
     409        $ENV{HOME} . "/.owl/modules" )
     410    {
     411        opendir( MODULES, $dir );
     412
     413        # source ./modules/*.pl
     414        @modules = grep( /\.pl$/, readdir(MODULES) );
     415
     416        foreach my $mod (@modules) {
     417            do "$dir/$mod";
     418        }
     419        closedir(MODULES);
     420    }
     421
     422}
     423
    411424
    412425
     
    415428################################################################################
    416429
    417 sub owl::receive_msg
     430sub receive_msg
    418431{
    419432    my $m = shift;
    420     foreach (@onReceiveMsg)
    421     {
    422         &$_($m);
    423     }
     433    runHook(\@onReceiveMsg, $m);
    424434}
    425435
     
    428438################################################################################
    429439
    430 sub owl::get_blist
     440sub get_blist
    431441{
    432442    my $m = shift;
    433     foreach (@onGetBuddyList)
    434     {
    435         &$_($m);
    436     }
     443    return runHook_accumulate(\@onGetBuddyList, $m);
    437444}
    438445
Note: See TracChangeset for help on using the changeset viewer.