From 43424b9438dff43a056e4563b47e1f334dd94a25 Mon Sep 17 00:00:00 2001 From: Tom Feist Date: Sun, 10 Oct 2010 22:56:19 +0100 Subject: added a brief proof-of-concept for loading/reloading additional functions on the fly from within irssi scripts. --- feature-tests/subscript.pl | 78 ++++++++++++++++++++++++++++++++++++++++++++++ feature-tests/test.sub | 6 ++++ 2 files changed, 84 insertions(+) create mode 100644 feature-tests/subscript.pl create mode 100644 feature-tests/test.sub (limited to 'feature-tests') diff --git a/feature-tests/subscript.pl b/feature-tests/subscript.pl new file mode 100644 index 0000000..74a5afe --- /dev/null +++ b/feature-tests/subscript.pl @@ -0,0 +1,78 @@ +use strict; +use Irssi; +use Irssi::TextUI; # for sbar_items_redraw + +use vars qw($VERSION %IRSSI); + +$VERSION = "0.1"; + +%IRSSI = ( + authors => "shabble", + contact => 'shabble+irssi@metavore.org, shabble@#irssi/Freenode', + name => "", + description => "", + license => "Public Domain", + changed => "" +); + +my $functions = {}; + +init(); + +sub load { + my $file = shift; + my $funcs; + if (-f $file) { + print "Loading from file: $file"; + $funcs = do $file; + } + + return unless ref $funcs eq 'HASH'; + print "Got hashref from file"; + + foreach my $name (keys %$funcs) { + my $func = $funcs->{$name}; + $functions->{$name} = $func; + } + + $functions = $funcs; + print "Loaded " . scalar(keys(%$funcs)) . " functions"; +} + +sub init { + Irssi::command_bind('subload', \&cmd_subload); + Irssi::command_bind('sublist', \&cmd_sublist); + Irssi::command_bind('subcall', \&cmd_subcall); +} + +sub cmd_subload { + my $args = shift; + print "Going to load: $args"; + load($args); +} + +sub cmd_sublist { + foreach my $name (keys %$functions) { + my $func = $functions->{$name}; + print "Function: $name => $func"; + } +} + +sub cmd_subcall { + my $args = shift; + my ($cmd, $cmdargs); + if ($args =~ m/^(\w+\b)(.*)$/) { + $cmd = $1; $cmdargs = $2; + } else { + print "Couldn't parse $args"; + return; + } + my $fun = $functions->{$cmd}; + + if (ref $fun eq 'CODE') { + print "Calling $cmd with $cmdargs"; + $fun->($cmdargs); + } else { + print "$cmd is not a coderef. cannot run"; + } +} diff --git a/feature-tests/test.sub b/feature-tests/test.sub new file mode 100644 index 0000000..6a24879 --- /dev/null +++ b/feature-tests/test.sub @@ -0,0 +1,6 @@ +{ + 'moo' => sub { print "Moo" }, + 'hello' => sub { print join(", ", @_) } +}; + + -- cgit v1.2.3 From 745b2901e2f635d6a57cbe3e40695a6fe751f97b Mon Sep 17 00:00:00 2001 From: Tom Feist Date: Sun, 10 Oct 2010 23:11:52 +0100 Subject: tidied up loading code, added demo of calling func in outer scope --- feature-tests/subscript.pl | 26 +++++++++++++++++++++++--- feature-tests/test.sub | 3 ++- 2 files changed, 25 insertions(+), 4 deletions(-) (limited to 'feature-tests') diff --git a/feature-tests/subscript.pl b/feature-tests/subscript.pl index 74a5afe..8550409 100644 --- a/feature-tests/subscript.pl +++ b/feature-tests/subscript.pl @@ -19,6 +19,10 @@ my $functions = {}; init(); +sub _input { + return int rand 1000; +} + sub load { my $file = shift; my $funcs; @@ -26,16 +30,32 @@ sub load { print "Loading from file: $file"; $funcs = do $file; } + if (not defined $funcs) { + if ($@) { + print "failed to parse $file: $@"; - return unless ref $funcs eq 'HASH'; - print "Got hashref from file"; + } elsif ($!) { + print "failed to read $file: $!"; + + } + return; + } + my $ref = ref $funcs; + if ($ref ne 'HASH') { + print "$file didn't return a hashref: ", defined $ref ? $ref : 'undef'; + return; + } foreach my $name (keys %$funcs) { my $func = $funcs->{$name}; + if (exists $functions->{$name}) { + print "Redefining function $name"; + } else { + print "adding function: $name"; + } $functions->{$name} = $func; } - $functions = $funcs; print "Loaded " . scalar(keys(%$funcs)) . " functions"; } diff --git a/feature-tests/test.sub b/feature-tests/test.sub index 6a24879..350e89d 100644 --- a/feature-tests/test.sub +++ b/feature-tests/test.sub @@ -1,6 +1,7 @@ { 'moo' => sub { print "Moo" }, - 'hello' => sub { print join(", ", @_) } + 'hello' => sub { print join(", ", @_) }, + 'inp' => sub { print "input is : ", _input(); }, }; -- cgit v1.2.3