in reply to Error reading line from file: Operation timed out

Why not just use Text::Template? The first line of the description on CPAN is "This is a library for generating form letters, building HTML pages, or filling in templates generally." As for failing to open the file, you should probably check if the file exists before you try to open it (just restating what moritz said). Also, do you happen to have a lock on the file from the filesystem or an editor? You should also check your permissions and ownership on the file as this could affect the file opening.

The Web is like a dominatrix. Everywhere I turn, I see little buttons ordering me to Submit. (Nytwind)
  • Comment on Re: Error reading line from file: Operation timed out

Replies are listed 'Best First'.
Re^2: Error reading line from file: Operation timed out
by moritz (Cardinal) on May 13, 2009 at 18:20 UTC
    you should probably check if the file exists before you try to open it (just restating what moritz said).

    No (and it's also not what I said).

    If you first check if the file exists, and then try to open it, the file could be created or removed just between those two operations, which gives you a wrong result.

    So the rule of common sense when dealing with the world outside is never to check and then try, but simply to try, and if it went wrong look at what went wrong (which $! tells you).

      Thanks for the answers. Ok, let me clarify a few things:

      1.- The file is local on my MAC.
      2.- No other prog,user or X has the file opened.
      3.- the line I had before was a:
      (!-e $templateLocation || !open($handle, $templateLocation)) following the OR convention... but I removed that for the $isOpen (troubleshooting) hence the bad designed code.
      4.- Thanks for the Template library... I thought I looked for one pretty hard, but It seems that I didn't...
      5.- The file is simple HTML plain text to 0 per line or no \n just a basic one.

      Still getting the error after a few modifications. Actually this is really strange... because it looks like it opens the file, but when is reading the lines is when times out...

      You shouldn't make assumptions. I never said to go ahead and open the file even if it doesn't exist. If it doesn't exist, don't try to open it unless you are wanting to create it... I may not have been completely clear but there is some common sense involved here... "Checking -e after a failed open is prone to race conditions" says exactly what I'm saying. You also made the assumption that this file was in the "outside world" when that wasn't the case as per robertobouza's latest post. If the file is locally on his machine then just hammering it to see if it works is not the answer...


      The Web is like a dominatrix. Everywhere I turn, I see little buttons ordering me to Submit. (Nytwind)

        I don't understand the points you're trying to make. To me the "outside world" is everything that the perl script doesn't control. That includes all files you're trying to open.

        If it doesn't exist, don't try to open it unless you are wanting to create it

        Uhm. Trying to open a file for reading (which is the default mode of open) does not create it under any circumstance.

        You also made the assumption that this file was in the "outside world" when that wasn't the case as per robertobouza's latest post

        Since robertobouza post was a reply to mine, how could I have known what he was going to reply to me?

        Even if it doesn't change in the mean time, there is absolutely no benefit in check its existence before trying to open it. Suppose you want to be thorough, then you'd have to write something like this:

        if (!-e $file) { die "'$file' does not exists'; } if (!-r $file) { die "insufficient permissions to read `$file'"; } if (-d $file) { die "`$file' is a directory, not a plain file"; } # and so on

        Actually there are error conditions (like out of memory) that you can't know in advance.

        Instead you just do open my $handle, '<', $file or die "Can't open `$fil'e for reading: $!"

        And even if the file doesn't change in this case, it can't hurt to stop with bad practices now.

        . If the file is locally on his machine then just hammering it to see if it works is not the answer...

        Why not? I see no reason not just to try it. I hope I illustrated the reasons for doing so (large number of checks necessary; some checks are impossible to do in advance; danger of race conditions)

        Roy,

        Thanks for the Text::Template. Just changed a few logic on my classes and it works like a charm...

        I'm still a little bit intrigued of what I did and why didn't work... but for now I'll leave it on the side. I'll worry about it when this happens again.

        thanks again for your help.