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

Hi, My question is about opening a file at run time.I'm trying to open a file for read and write file which doesn't exist. In other words I want to creat a file when I run perl program.Following is my code.
#!/usr/bin/perl $Inputfile='filename'; open (IN,"<$Inputfile") or die "cannot open file: $!\n"; open (OUT, "+<$inputfile.out") or die "cannot open file: $!\n"; while (<IN>){ .... ..... print OUT; #print to $Inputfile.out file } #do something with content of $Inputfile.out
it gives me error on third line where I try to open that outputfile.Is there any way I can create file when I run the program.Any Suggestion.Thanx

Replies are listed 'Best First'.
(tye)Re: Creating file at run time
by tye (Sage) on Nov 23, 2000 at 00:06 UTC

    You can't, in general, do what you want with open() so use sysopen().

    You want to create the file if it doesn't exist but you don't want to truncate the file if it exists. You can do this with open() if you use a mode of "+>>" which has the (perhaps) unfortunate side effect of also opening the file in append mode so that any write to the file will get put after the last byte of the file, no matter where the current file position is when you do the write.

    To get "create but don't truncate and don't append" behavior, you have to use:

    use Fcntl qw( O_RDWR O_CREAT ); sysopen( FILE, "$Inputfile.out", O_RDWR|O_CREAT, 0777 ) or die "$!";
    which is unfortunate but matches the limitations of C's fopen().

            - tye (but my friends call me "Tye")
Re: Creating file at run time
by kilinrax (Deacon) on Nov 22, 2000 at 23:57 UTC
    Change the prefix to the filename:
    open (OUT, ">$inputfile.out") or die "cannot open file: $!\n";
    This will open the file for output, creating it if it does not already exist. '+' gives you read and write access to a file, but it will only be created if you open the file for output or appending.
Re: Creating file at run time
by mrmick (Curate) on Nov 23, 2000 at 00:00 UTC
    I believe that the problem here is that you won't be permitted to open a file for reading if it doesn't exist. Try changing opening your output file to the following (>> for append and > for overwrite):
    open (OUT, ">>$inputfile.out") or die "cannot open file: $!\n";

    Mick
(crazyinsomniac) Re: Creating file at run time
by crazyinsomniac (Prior) on Nov 22, 2000 at 23:57 UTC
    I am by no means an expert, but my advice is always, search the site before posting already answered questions. But seeing how you posted as an anonymous monk, it is clear you are ashamed. anyway what you're doing wrong, is trying to open a file that doesn't exist for reading. You need to open it for writing first, which should create the file. something like this:
    #!/usr/bin/perl $Inputfile='filename'; open (OUT, ">$inputfile.out") or die "cannot open file 4 writing: $!\n"; open (IN,"<$Inputfile") or die "cannot open file: $!\n"; while (<IN>){ .... ..... print OUT; #print to $Inputfile.out file }

    "cRaZy is co01, but sometimes cRaZy is cRaZy".
                                                          - crazyinsomniac

Re: Creating file at run time
by steveAZ98 (Monk) on Nov 22, 2000 at 23:57 UTC
    I'd use something like this.
    HTH
    #!/usr/bin/perl -w use FileHandle; my $fh = FileHandle->new('test','+>'); print $fh $_ while (<DATA>); $fh->close; __DATA__ Test this
Re: Creating file at run time
by arturo (Vicar) on Nov 22, 2000 at 23:57 UTC

    I'm not sure from your description exactly what you're seeking to do: open file A, do stuff to the contents, and print something out to file B? (do you want to then *read* from file B? that's what I'm not sure of)

    Also, please tell us *what* the error message you're getting is.

    That said, the solution to your problem may be as simple as changing your fourth line to read:

    open (OUT, ">$inputfile.out") or die "cannot open $inputfile.out: $!\n +";

    Note that not only will that create a new file, it will overwrite any existing file with the same name; depending on what else you're doing, this may or many not be a problem.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor