Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Cann't create a file

by chriso (Sexton)
on Jul 14, 2005 at 18:34 UTC ( [id://475004]=perlquestion: print w/replies, xml ) Need Help??

chriso has asked for the wisdom of the Perl Monks concerning the following question:

I'm running perl 5.8 on a Netware 6.5 server running Apache 2. I want to create/append to a file but no file is being created. I also don't get any error messages. Is there something wrong with my code? Do I need to declare the file handle or something?

Say for example that $file is ist243\243q1.txt. $log would be 243q1.log.

$clocktime = localtime; @log = split( /\//, $file ); @log = split( /\./, $log[1] ); $log = $log[0]; $log = $log . ".log"; chdir("/perl/web/examscores/$courselocation/access"); open ACCESS, ">> $log" || die "Cannot open $log: $!"; print ACCESS $idinput . " " . $lname . " " . $fname . " " . $clocktime . " " . $useripaddress . " " . $time . "\n"; close(ACCESS);
Thanks.

Chris

Replies are listed 'Best First'.
Re: Cann't create a file
by Transient (Hermit) on Jul 14, 2005 at 18:36 UTC
    Change
    open ACCESS, ">> $log" || die "Cannot open $log: $!";
    to be
    open ACCESS, ">> $log" or die "Cannot open $log: $!"; #or open (ACCESS, ">> $log") || die "Cannot open $log: $!";


    Reason: || has higher precedence than , causing
    open ACCESS, ">> $log" || die "Cannot open $log: $!";
    to be equivalent to
    open ACCESS, ( ">> $log" || die "Cannot open $log: $!"; )
    thus, no error message.
Re: Cann't create a file
by ikegami (Patriarch) on Jul 14, 2005 at 18:43 UTC

    1) Your || has too high precedence.
    open ACCESS, ">> $log" || die "Cannot open $log: $!";
    is equivalent to
    open ACCESS, (">> $log" || die "Cannot open $log: $!");
    which is equivalent to
    open ACCESS, ">> $log";
    Instead, use
    open(ACCESS, ">> $log") || die "Cannot open $log: $!";
    or
    open ACCESS, ">> $log" or die "Cannot open $log: $!";

    2) You don't check if chdir succeeded.

    3) The three argument form of open is safer.

    4) Using lexicals for filehandles is cleaner.

    5) The following is easier to read and less error-prone:

    my $log = $file; $log =~ s#^.*/##; $log =~ s#\.[^.]$##; $log .= '.log';

    6) join might be a better choice instead of all those string concatenations.

    7) Finally, you could eliminate the chdir as follows:

    $log = "/perl/web/examscores/$courselocation/access/$log";

    Fixed code:

    my $clocktime = localtime; my $log = $file; $log =~ s#^.*/##; $log =~ s#\.[^.]$##; $log = "/perl/web/examscores/$courselocation/access/$log.log"; open(my $access, '>>', $log) or die("Cannot open $log: $!\n"); print $access ( join(' ', $idinput, $lname, $fname, $clocktime, $useripaddress, $time, ), "\n"); close($access);

    By the way, localtime returns a string with spaces in it (in scalar contect).

Re: Cann't create a file
by coreolyn (Parson) on Jul 14, 2005 at 18:57 UTC

    I would check what your value of $log[0] is. You may be making a logfile -- just not the one you're expecting. Also better clarity in your variable naming wouldn't hurt. $logname might be clearer than $log and avoid confusion with your @log array.

    If you are still not creating a log file check the permissions on the directory into which it is trying to write. chdir may not be your cwd so your log may be printing somewhere you don't expect. ( Like where you are starting the script from )

    One last suggestion would be to simplify the setting of the logfile name with 1 line $logname= "$log[0]\.log"

Re: Cann't create a file
by BaldPenguin (Friar) on Jul 14, 2005 at 18:37 UTC
    Did you
    use strict; use warnings;
    That would tell you if you missed a declaration. I myself would try wrapping the whole thing in an eval and test the result, see if something else is coughing it up.

    Don
    WHITEPAGES.COM | INC
    Everything I've learned in life can be summed up in a small perl script!
Re: Cann't create a file
by chriso (Sexton) on Jul 14, 2005 at 19:18 UTC
    Thank you all for your input. I tried both variations using "or" instead of || and () when using ||. Both caused my server to reboot. Go figure. Back to the drawing board.

    Chris.

      First, use the open( ... ) form of the function.. given the issues with operator precedence it would be very wise to use the parenthesis (also known as brackets).

      Second, if your server is rebooting I highly recommend pulling out the debugger. perl -d<script>. Then single step through each line until you hit the one that makes your server reboot.

      If you're using CGI through Apache and don't have the option of running a debugger, then one approach you might take to finding the line causing the issue is sprinking sleeps and debugging statements.. e.g.

      $| = 1; # turn on autoflushing of print statements print( "<p>About to open file..</p>" ); sleep( 5 ); open( ... ) or die( ... ); print( "<p>About to append to file..</p>" ); sleep( 5 ); print FILEHANDLE $statement; ...
      and soforth..

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://475004]
Approved by suaveant
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-19 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found