m0ve has asked for the wisdom of the Perl Monks concerning the following question:

i wrote this little HL-serverlogparser, to find people using restricted weapons, but when i start it with perl -w script.pl it wont work and exits at line 57 (logfile could not be opened). however if i dont use the "-w" it works just fine....could someone please tell me why ?

#!/usr/local/bin/perl # # AUTOWEAPONBANS N BANLISTCLEANER # V 1.1 # ############################################ use strict; use warnings; use Data::Dumper; use utf8; binmode(STDOUT, ":encoding(UTF8)"); binmode(STDIN, ":encoding(UTF8)"); our (@steam,@name,@scout,@awp,@date,@time); our %players=([@steam],[@name],[@scout],[@awp]); my $datei1 = "banned.cfg"; my $pfad1 = "H:\\perl\\uebungen\\save\\pme\\logparsing\\srcds1\\"; our $file1 = "$pfad1$datei1"; my $datei2 = "banned.cfg"; my $pfad2 = "H:\\perl\\uebungen\\save\\pme\\logparsing\\srcds2\\"; our $file2 = "$pfad2$datei2"; our $infofile = "info.txt"; { package main; &Scan; &Readinfo("$infofile"); &Read("$file1"); &Read("$file2"); &Write("$file1"); &Write("$file2"); &Writeinfo("$infofile"); #print Dumper (%players); } ###################################################################### +############################# # logfile auswerten ###################################################################### +############################# sub Scan { my $file = $ARGV[0]; my ($line); my $steam; my @oldbans; if (!$file) { print STDERR "\n\t KEIN LOGFILE ANGEGEBEN !\n\n"; exit(); } open(LOG, $file) or die ("\n\tPFAD/FILENAME FALSCH ? \n\nLogfile konnt +e nicht geoeffnet werden"); #$file öffnen while(<LOG>) { #$file zeilenweise auslesen chomp(); $line = $_; my $time = substr($line,2,21); #zeit extrahieren if(substr($line,25,1) eq "\"") #auf """ prüfen (pla +yeraction) { $steam = substr($line,index("$line","STEAM_0:",32),(index("$li +ne",">",index("$line","STEAM_0:",32)) -index("$line","STEAM_0:",32))); if(index("$line","with \"scout\"",32) != -1) { # scou +tkills suchen $players{$steam}{$time} = "scout"; $players{$steam}{'info'} = "restricted wea +pon use"; } if(index("$line","with \"awp\"",32) != -1) { # awpk +ills suchen $players{$steam}{$time} = "AWP"; $players{$steam}{'info'} = "restricted wea +pon use"; } } } close(LOG); #print Dumper (%players); } [...more subs...]

Replies are listed 'Best First'.
Re: problems opening logfile
by Corion (Patriarch) on Aug 10, 2007 at 09:19 UTC

    I doubt that having -w on the command line or not is the cause for Perl being unable to open the file. But Perl tells you why it couldn't open a file in the $! variable:

    open(LOG, $file) or die ("\n\tPFAD/FILENAME FALSCH ? \n\nLogfile konnt +e nicht geoeffnet werden: $!"); #$file öffnen

    Some stylistic notes:

    • Don't pass strings where you pass scalars:
      &Readinfo("$infofile"); &Read("$file1"); &Read("$file2"); &Write("$file1"); &Write("$file2"); &Writeinfo("$infofile");

      should be

      Readinfo($infofile); Read($file1); Read($file2); Write($file1); Write($file2); Writeinfo($infofile);

      and

      index("$line","STEAM_0:",32)))

      should be

      index($line,"STEAM_0:",32)))
      thx for the suggestions, i'll change that. but as i said, the script works just fine without the -w and as i need to learn perl i want to find out why ;)

        The -w switch should not alter the behaviour of Perl, so please reduce your program to a small, self-contained program that exhibits the same behaviour.

        Other than that, I can only recommend to use the following idiom:

        open LOG, $file or die "Couldn't open '$file': $!";

        which tells you both, the filename used and the reason why it failed. Maybe you have other programs writing to the file or something.

Re: problems opening logfile
by FunkyMonk (Bishop) on Aug 10, 2007 at 09:17 UTC
    Try adding $! to the die at the end of the open. It'll tell you what the error was.

    See perlvar for a description of $!.

      it said "no such file or directory"
        You are passing the name of the file to open on the command line, aren't you?

        Something like:

        perl -w script.pl c:\path\to\the\file.txt