summaryrefslogtreecommitdiff
path: root/src/perl/irssi-core.pl
blob: 2a0fa57dc0023bc0c0c810c72abe5b733d3aabda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# NOTE: this is printed through printf()-like function,
# so no extra percent characters.

# %%s can be used once, it contains the
# use Irssi; use Irssi::Irc; etc..
package Irssi::Core;

use Symbol qw(delete_package);

sub destroy {
  my $package = "Irssi::Script::".$_[0];
  delete_package($package);
}

sub eval_data {
  my ($data, $id) = @_;
  destroy($id);

  my $package = "Irssi::Script::$id";
  my $eval = qq{package $package; %s sub handler { $data; }};
  {
      # hide our variables within this block
      my ($filename, $package, $data);
      eval $eval;
  }
  die $@ if $@;

  eval {$package->handler;};
  die $@ if $@;
}

sub eval_file {
  my ($filename, $id) = @_;

  local *FH;
  open FH, $filename or die "File not found: $filename";
  local($/) = undef;
  my $data = <FH>;
  close FH;
  local($/) = "\n";

  eval_data($data, $id);
}