CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Opening an empty file hangs it up

 



ImpInTraining
New User

Mar 23, 2013, 10:12 AM

Post #1 of 6 (871 views)
Opening an empty file hangs it up Can't Post

I run a YaBB forum. Have made a few mods over the past few years. Currently trying to update my mods for the newest version of YaBB (2.5.2).

In one situation, I have a dice-roll logging code. Seems to be working for the most part, but I am having a couple issues that I need to work through. When the program has had the roll log supporting text file emptied (or if it's brand new) it has been hanging up the server. So I added this little check to throw at least one line of text in the file before running the rest of it. But it's still hanging up.


Code
 if (!-s "$vardir/dicelog.txt")  { 
fopen(ROLLLOG, ">>$vardir/dicelog.txt") || die "$RollLog{'1'} $!";
print ROLLLOG "<tr><td>";
print ROLLLOG localtime;
print ROLLLOG "</td><td>System</td> <td> NA </td> <td> NA </td> <td> NA </td> <td> NA </td> <td> New Roll Log Created </td> </tr>

\n";
fclose(ROLLLOG);

}



Is this hanging up because of the way I'm checking for an empty file, or is there some other reason? Is it possible that I need to add a pause just to allow the process time to create the file before it tries to open it?

I have a work around if I can't figure this out. I just have to make sure to log one roll before trying to view the dicelog.txt - because then it isn't empty. I was just hoping to get a clean install a little more user friendly.


Zhris
Enthusiast

Mar 23, 2013, 8:23 PM

Post #2 of 6 (857 views)
Re: [ImpInTraining] Opening an empty file hangs it up [In reply to] Can't Post

Hey,

Unless i'm mistaken, the functions fopen and fclose are incorrect. CPAN File::Open exports an fopen function but not an fclose function. In PHP, the fopen and fclose functions are the core file opening and file closing functions, therefore you may have mixed the languages up.

Replace "fopen" with "open" and "fclose" with "close".

Although i'm unfamiliar with your setup / rest of your code, I would have expected your script to have error'ed.

Chris


ImpInTraining
New User

Mar 23, 2013, 9:17 PM

Post #3 of 6 (849 views)
Re: [Zhris] Opening an empty file hangs it up [In reply to] Can't Post

Thanks for the response.

I don't know about all that though. I do know I've been using fopen in this particular perl code for years and it works fine. In fact, in this very file, I have fopen to store the rolled data, and fopen to read the log file file for display. So long as there is something in the file to read, there doesn't seem to be any problems. I think maybe it's using C++ in the perl argument though, so you could be right, but I've seen other Perl coders use it (hense why I started using it). Here's an example in a Perl tutorial: http://szabgab.com/open-file-in-perl.html


Zhris
Enthusiast

Mar 23, 2013, 9:34 PM

Post #4 of 6 (846 views)
Re: [ImpInTraining] Opening an empty file hangs it up [In reply to] Can't Post

Hey,

Note that the tutorial you linked to uses File::Open which does export an fopen function as suggested in my reply. As you can imagine, it is difficult to provide definitive suggestions without seeing your full script, particularly what modules / libraries you are using i.e. C++ XS interface. I've never had to use anything but open and close therefore unfamiliar with your syntax. Apologies that I can't be of further help.

Chris


(This post was edited by Zhris on Mar 23, 2013, 9:43 PM)


Laurent_R
Veteran / Moderator

Mar 24, 2013, 2:58 AM

Post #5 of 6 (837 views)
Re: [Zhris] Opening an empty file hangs it up [In reply to] Can't Post

Just a couple of things.

1. The File::Open module does not export any fclose function.

2. In theory at least, this module's fopen syntax requires the opening mode to be a second argument, separated from the file name:


Code
my $fh = fopen $file; 
my $fh = fopen $file, $mode;
my $fh = fopen $file, $mode, $layers;


I haven't tried it, but I would expect your "my $fh = fopen ">>file.txt" syntax to mess things up.

Try something like: my $fh = fopen "file.txt", ">>" or die "...";


Zhris
Enthusiast

Mar 24, 2013, 5:41 AM

Post #6 of 6 (834 views)
Re: [Laurent_R] Opening an empty file hangs it up [In reply to] Can't Post

Hi Laurent,

I believe the OP is using the C++ XS interface as oppose to File::Open.

Chris

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives