Gossamer Forum
Home : General : Perl Programming :

Creating Files with Open

Quote Reply
Creating Files with Open
Is it possible to create a file with the Open command? I'm trying to create a file (with a dynamically generated filename) at the same time I assign a file handle to it, however, the open command pushes to STDOUT if it fails to find the file. Thanks!

-- Michael

Quote Reply
Re: Creating Files with Open In reply to
Here's an example from my Links plug-in.

Code:
open (OUTFILE,">>$out_file") or die "could not open $filename";
binmode (OUTFILE); ## necessary for NT for some reason, so I'm told.
## stuff
close (OUTFILE);
PUGDOGŪ
PUGDOGŪ Enterprises, Inc.
FAQ: http://pugdog.com/FAQ


Quote Reply
Re: Creating Files with Open In reply to
perldoc -f open
open(FH, ">$your_file") or die "$!\n"; # use >> to concat
print FH $data;
close(FH);

binmode is only needed if you are trying to write binary data. if you are just writing text to it you won't need it.
you have to make sure you include the filehandle (FH in this case) in your print statements if you want the data to go to the opened file.

-g


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';
Quote Reply
Re: Creating Files with Open In reply to
Does this stop the problem of some servers not creating the files when using just;

open (TOFILE,">>$out_file") or die "could not open $filename";
print TOFILE "Test Stuff Here";

???? I have had problems where files are not generated via the 'open' command (e.g. my password script, which creates the .htaccess files etc). Is there a way of CREATING files, not just trying to open them and if its not there, creating them, cos that doesn't work on all servers?!?! Frown

Thanks

Andy



http://www.ace-installer.com
webmaster@Ace-installer.com
Quote Reply
Re: Creating Files with Open In reply to
the die "could not open $filename

will stop the file from being created.

i would probably try and create the file and then later on check if it exist... with that code.. Then follow through with that i wanted to use it for.


Quote Reply
Re: Creating Files with Open In reply to
But surley the die command doesn't just stop it getting created, if that wasn't there it still wouldn't be created, but i just wouldn't get an error reported. What i may do is add a die function that goes to a subroutine and then if the file is not created it will tell then how to create a settings file or .htaccess file rather than the automated process that the script would normally do!!!!

Andy

http://www.ace-installer.com
webmaster@Ace-installer.com
Quote Reply
Re: Creating Files with Open In reply to
If there is an error and the die command comes into action, just removing it from the code will not mean that the file is created and the error will still be occuring but you won't be told about it and the file won't be created.

The "die" stops the rest of the sub executing.

(I think)

Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Re: Creating Files with Open In reply to
yep
the die command is there to keep a chain reaction of bad things happening because of a previous requirement (in this case, opening a file) didn't work and, of course, to debug.
if the open command isn't creating the file there are other issues to look at like file permissions and paths.
what does it spit out when it dies after trying to open the file?

-g


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';
Quote Reply
Re: Creating Files with Open In reply to
What i want to do is create the file. On some servers the file is created when you use the OPEN command, but on others it isn't!!! Is there a commmand something like CREATE or MAKE???Cool

Andy

http://www.ace-installer.com
webmaster@Ace-installer.com