Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Re: Re: String Manupulation

by LeeC79 (Acolyte)
on Aug 27, 2003 at 17:47 UTC ( [id://287110]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: String Manupulation
in thread String Manupulation

Remember, I'm new. Be gentle. I'm simply trying to create a log file that will have the current date and timestamp as the filename. But I can't figure out what is wrong with my code. The only thing I can think of is that the filename I'm trying to use is to long.
#!/usr/local/bin/perl -w use strict; my $localtime = scalar localtime; my $tmp = ".txt"; my $logfile = $localtime.$tmp; $logfile =~ tr/ /-/; open( OUTFILE, ">$logfile" ); print OUTFILE "Hello"; close(OUTFILE);
And this is the error I get when trying to run:
print() on closed filehandle OUTFILE at log.pl line 12.
Any hints?

Replies are listed 'Best First'.
Re4: String Manupulation
by dragonchild (Archbishop) on Aug 27, 2003 at 17:50 UTC
    Worked fine on my system. I'm running 5.6.0 on AIX. Now, a suggestion:
    open(OUTFILE, ">$logfile") || die "Cannot open '$logfile' for writing: + $!\n";

    That will tell you why it cannot open the file. (That's what the $! variable does.) You should always check the return value of open and similar functions.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Re: Re: Re: String Manupulation
by davido (Cardinal) on Aug 27, 2003 at 19:00 UTC
    The example 'ought to work', but if it doesn't, the problem is related to your open statement failing.

    It is always imperative to check the return value of open, to ensure that it succeeded. One common way is as follows:

    open ( OUTFILE, ">$logfile" ) or die "Can't open $logifle.\n$!";

    The other issue here is that if it turns out that it's failing, the probable reason is that logfile already exists and is either locked or set to permissions that prevent your script from opening it for writing.

    This leads to a couple of additional issues:

    First, you should probably either be testing for file pre-existance with:

    if (-e $filename) { do something } else {do something else}

    or opening for append rather than replace, with:

    open ( OUTFILE, ">>$logfile" ) or die "Can't open $logfile:\n$!";

    The other issue is that if the file is locked, it might just be a matter of waiting a second or two for it to be released by whoever is writing to it. ...which leads to another issue. If someone else (or some other program) is possibly writing to the same logfile, you really should be checking lock status prior to trying to open, and locking it when you use it, and appending to it rather than overwriting it.

    Just a few suggestions and things to consider.

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Re: Re: Re: Re: String Manupulation
by asarih (Hermit) on Aug 27, 2003 at 19:19 UTC
    My guess is that open failed. It's never a good idea to assume that open succeeds.
    open( OUTFILE, ">$logfile") or die "Can't write to $logfile\n";
    Do you get the same error?
Re: Re: Re: Re: String Manupulation
by codingchemist (Novice) on Aug 27, 2003 at 20:25 UTC
    it is probably a permissions problem. you don't have permission to create the logfile. to make sure this is the case try this:
    open (OUTFILE, ">$logfile" ) or die "can't open file: $!\n";

    that will tell you when you try to open the file for writing if you can even do that.
Re: Re: Re: Re: String Manupulation
by ritontor (Acolyte) on Aug 27, 2003 at 18:14 UTC
    Your code works fine, it's a file permissions issue, guaranteed.
Re: Re: Re: Re: String Manupulation
by welchavw (Pilgrim) on Aug 27, 2003 at 20:31 UTC
    You are generating temporary files with invalid names and ignoring the function return code of open(). You should not use the following code, but use one of the temporary filename creation methods available in CPAN modules (perhaps IO::File->new_tmpfile(), but there is probably a better one).
    use strict; use warnings; my $localtime = scalar localtime; my $tmp = ".txt"; my $logfile = $localtime.$tmp; $logfile =~ tr/ /-/; open( OUTFILE, ">$logfile" ) or die "couldn't open $logfile\:$!\n"; print OUTFILE "Hello"; close(OUTFILE)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-03-29 15:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found