in reply to Help w/ regex in filename

The simple answer is that you're not invoking File::Tail's new constructor properly. From the POD: "If it has only one paramter, it is assumed to be the filename. If the open fails, the module performs a croak."

You're asking File::Tail to open a file named, "D:\ServerTools\Logs\ServerLog\((\d+)-(\d+)-(\d+)@(\d+)-(\d+)\.log". That's a very unlikely file name. It looks more like a regexp appended to a path.


Dave

Replies are listed 'Best First'.
Re^2: Help w/ regex in filename
by dcthehole (Novice) on Jul 06, 2013 at 04:10 UTC

    Ok heres what I updated it to.

    #!/usr/bin/perl use DBI; use File::Tail; use POSIX(); our $bWin; BEGIN { $bWin = ($^O eq "MSWin32") ? 1 : 0; } Loop(); sub Loop { print "Updating Server Log -> DB\n"; Update_DB_Actions(); } sub Update_DB_Actions { my $dbh = DBI->connect('DBI:mysql:database=arma3;host=localhost',' +root','root') || die "Could not connect to database: $DBI::errstr"; my $filename = POSIX::strftime('ServerLog(%m-%d-%Y).log', localtim +e ); my $log = "D:\\ServerTools\\Logs\\$filename"; my $qu_re1 = q{(.?) started server @ (\d+):(\d+):(\d+)}; my $qu_re2 = q{(.?) restarted server @ (\d+):(\d+):(\d+)}; my $qu_re3 = q{(.?) stopped server @ (\d+):(\d+):(\d+)}; my $file = File::Tail->new(name=>$log); while(defined($line=$log->read)) { my $sql = "INSERT INTO `actions` (log) VALUES (?)"; $insa = $dbh->prepare($sql) or die $!; if($line =~ /$qu_re1/i || /$qu_re2/i || /$qu_re3/i) { $insa->execute($line); print "Inserted Action!\n"; } } unlink $log; }

    But now I get a new error.

    Can't locate object method "read" via package "D:\ServerTools\Logs\Ser +verLog(07- 06-2013).log" (perhaps you forgot to load "D:\ServerTools\Logs\ServerL +og(07-06-2 013).log"?) at script.pl line 112.

      Your File::Tail object is referred to using $file. The variable $log probably doesn't refer to a File::Tail object. use strict 'vars'; would probably catch also catch that $line hasn't been pre-declared, unless you're declaring $line at a broader scope and not showing us.

      Updated to reflect that it's $log that is being misused, not $line.


      Dave

      my $log = "D:\\ServerTools\\Logs\\ $filename";
      my $file = File::Tail->new(name=> $log );
      while(defined($line= $log->read)) {

      Well, $log is a filename (a string), $file is a File::Tail object, but you try to read from $log

      Maybe you want

      $filename = "$directory\\$filename"; my $log = File::Tail->new( name=> $filename );

      Its easier to keep track of what each variable represents if you use meaningful variable names

      $log_filename , $log_filetail, $logreader, $coffebreak