Просмотр исходного кода

Stop using FindBin, to many problems with it.

Morten Brix Pedersen 25 лет назад
Родитель
Сommit
1dc84c78a5
2 измененных файлов с 13 добавлено и 6 удалено
  1. 5 2
      modules/Pisg.pm
  2. 8 4
      pisg.pl

+ 5 - 2
modules/Pisg.pm

@@ -28,6 +28,7 @@ sub new
     my $self = {
         override_cfg => $args{override_cfg},
         use_configfile => $args{use_configfile},
+        search_path => $args{search_path},
         chans => {},
         users => {},
         cfg => {},
@@ -200,7 +201,7 @@ sub get_language_templates
 {
     my $self = shift;
 
-    open(FILE, $self->{cfg}->{langfile}) or open (FILE, $FindBin::Bin . "/$self->{cfg}->{langfile}") or die("$0: Unable to open language file($self->{cfg}->{langfile}): $!\n");
+    open(FILE, $self->{cfg}->{langfile}) or open (FILE, $self->{search_path} . "/$self->{cfg}->{langfile}") or die("$0: Unable to open language file($self->{cfg}->{langfile}): $!\n");
 
     while (my $line = <FILE>)
     {
@@ -267,7 +268,7 @@ sub init_config
 {
     my $self = shift;
 
-    if ((open(CONFIG, $self->{cfg}->{configfile}) or open(CONFIG, $FindBin::Bin . "/$self->{cfg}->{configfile}"))) {
+    if ((open(CONFIG, $self->{cfg}->{configfile}) or open(CONFIG, $self->{search_path} . "/$self->{cfg}->{configfile}"))) {
 
         my $lineno = 0;
         while (my $line = <CONFIG>)
@@ -490,6 +491,8 @@ B<override_cfg> - This defines whichever configuration variables you want to
 override from the configuration file. If you set use_configfile to 0, then
 you'll have to set at least channel and logfile here.
 
+B<search_path> - This defines an optional search path. It's used when you want to hardcode an alternative path where pisg should look after its language and config file.
+
 =back
 
 =head1 AUTHOR

+ 8 - 4
pisg.pl

@@ -2,7 +2,6 @@
 
 use strict;
 use Getopt::Long;
-use FindBin;
 
 # pisg - Perl IRC Statistics Generator
 #
@@ -24,7 +23,9 @@ use FindBin;
 
 sub main
 {
-    my $cfg = get_cmdline_options();
+    my $script_dir = $0;
+    $script_dir =~ s/\/[^\/]*$//;
+    my $cfg = get_cmdline_options($script_dir);
     push(@INC, $cfg->{modules_dir});
 
     my $pisg;
@@ -34,7 +35,8 @@ use Pisg;
 
 \$pisg = new Pisg(
     use_configfile => '1',
-    override_cfg => \$cfg
+    override_cfg => \$cfg,
+    search_path => \$script_dir,
 );
 \$pisg->run();
 END
@@ -49,8 +51,10 @@ END
 
 sub get_cmdline_options
 {
+    my $script_dir = shift;
+
     my $cfg = {
-        modules_dir => $FindBin::Bin . "/modules",     # Module search path
+        modules_dir => "$script_dir/modules/",     # Module search path
     };
 
     my $tmp;