aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Feist <shabble@metavore.org>2010-10-10 21:56:19 +0000
committerTom Feist <shabble@metavore.org>2010-10-10 21:56:19 +0000
commit43424b9438dff43a056e4563b47e1f334dd94a25 (patch)
treeb45f22ac37c7faefb592a84bf7b7dd056e3e7b8e
parentre-added additional signals to refresh prompt when needed (diff)
downloadirssi-scripts-43424b9438dff43a056e4563b47e1f334dd94a25.tar.gz
irssi-scripts-43424b9438dff43a056e4563b47e1f334dd94a25.zip
added a brief proof-of-concept for loading/reloading additional functions on the
fly from within irssi scripts.
Diffstat (limited to '')
-rw-r--r--feature-tests/subscript.pl78
-rw-r--r--feature-tests/test.sub6
2 files changed, 84 insertions, 0 deletions
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(", ", @_) }
+};
+
+