in reply to Help w/ regex in filename

G'day dcthehole,

Welcome to the monastery.

You give your error as being "at script.pl line 109" and then you write "Here is my actual script" which is followed by substantially less than 109 lines of code. So, either you've posted something other than script.pl or you've removed dozens of lines from it (probably near the beginning). Given that, the following is very much guesswork.

This line

my $filename = q{ServerLog\((\d+)-(\d+)-(\d+)@(\d+)-(\d+)\.log)};

looks like you're trying to generate a filename using data you've captured in a regexp; however, you're not even close with that syntax: you appear to have embedded the pattern in a literal string. Alternatively, you may be attempting to assemble a regexp but you don't show any code doing that. Take a look at perlretut - Perl regular expressions tutorial.

You have similar code when assigning to $qu_re1, $qu_re2 and $qu_re3. You don't show any use for those variables: I don't know why they're there or what they're supposed to do.

You should probably be calling your subroutines like sub_name() instead of &sub_name; however, maybe the missing code would reveal why you're doing it this way. Take a look at perlsub - Perl subroutines.

You'd do well to use strict at the start of your code. It's also preferable to use warnings instead of the -w switch (see perlrun).

If you need to ask follow-up questions, please follow the guidelines in "How do I post a question effectively?": you'll get a much better answer rather than guesses.

-- Ken

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

    The flip side of the coin is that 109+ lines of code aren't really needed to reproduce the error. Ideally the code producing the error would be reduced to 5-25 lines of code, and the error message would be a copy/paste of what that 5-25 LOC example produces. Often while going through the exercise of narrowing the problem down to a brief snippet of code that can still reproduce the problem, one discovers what the problem is, eliminating the reason to ask.


    Dave

      Yes, I absolutely agree and that's why I included the link to "How do I post a question effectively?" (for any follow-up questions) as it recommends posting:

      "... a minimal script that reproduces your problem ..." [original emphasis]

      The main thrust of my first paragraph was to highlight the fact that the error message didn't marry up with the posted code and my following comments were very much guesses.

      I have just noticed that the second version of the OP's code (Re^2: Help w/ regex in filename) has about 40 lines but the error message shows "at script.pl line 112". There's clearly something else going on there that we're not privy to: maybe that's script.pl and the OP is removing part of it; maybe script.pl is a completely different piece of code that's requiring the posted code; maybe it's neither of those.

      -- Ken

      I just have alot of code commented out. Thats why it is at 109+ lines. I havent found anything in those links to help me fix this problem.

        Here's an example of what I mean by "reduce the problem to 5-25 lines of code". The portion of the code you're having trouble with could be reduced to the following test:

        use strict; use warnings; use File::Tail; use POSIX (); my $filename = POSIX::strftime('ServerLog(%m-%d-%Y).log', localtime ); print "\$filename = <<<$filename>>>\n"; my $log = "D:\\ServerTools\\Logs\\$filename"; print "\$log = <<<$log>>>\n"; my $file = File::Tail->new($log); while( defined( my $line = $log->read ) ) { print "\$line = <<<$line>>>\n"; } unlink $log or die $!;

        And then it would become quite clear to see that $log contains a file path. A file path is not a File::Tail object. Your File::Tail object is referred to by $file. Therefore, if you want to invoke the read method on your File::Tail object, you should be invoking it as:

        while( defined( my $line = $file->read ) ) { ...

        It's pretty obvious that the database stuff wasn't the problem you're asking about. So as you're trying to nail down what the issue is by creating some test code, you can immediately eliminate it. Eliminating that, and any other unrelated code-noise, you're left with just the problem, and it becomes easier to see. I hope this exercise is helpful to you.


        Dave