in reply to Re: Help w/ regex in filename
in thread Help w/ regex in filename

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.

Replies are listed 'Best First'.
Re^3: Help w/ regex in filename
by davido (Cardinal) on Jul 06, 2013 at 04:14 UTC

    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

Re^3: Help w/ regex in filename (varnames)
by Anonymous Monk on Jul 06, 2013 at 07:35 UTC
    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