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

Hi monks i want to create some text files at run time and want to write on each of the file something.
#!/usr/bin/perl open ( NAMES, "name.txt") or die "Could not open names.txt.\n"; @list = <NAMES>; print "@list"; while ($list[$y]) { open (OUT, ">$list[$y].txt") or die "cannot open file for writing: $!\ +n"; print " hello"; $y=$y+1 }
The input of a file can be
john peter outer
Here 3 files should be created as john.txt , peter.txt and outer.txt and i want to do some calculation on this. But the code i have written does not give the output.

Replies are listed 'Best First'.
Re: creating a text file at run time and writing to it.
by phenom (Chaplain) on Jul 20, 2006 at 11:22 UTC
    I'd probably do this:
    #!/usr/bin/perl use strict; use warnings; open( NAMES, "<", "name.txt") or die $!; my @list = <NAMES>; close(NAMES); print "list = @list\n"; for my $name (@list) { open(OUT, ">", $name) or die $!; print OUT "hello"; close(OUT); }
    Notice the differences:
    1. use strict and use warnings; 2. use the "$!" so you get the explicit error when there is one 3. you didn't define $y - and it looks to be unnecessary for this 4. you didn't close the FILE handles 5. you need to specify the FILE handle you want to write to
    Start with "use strict" and "use warnings" and that will be very helpful!
    hth, dave

    Update: Oops, forgot the .txt part - see madtoperl's node.

      Hi Thank you for the response but gives mesg " invalid argument" at line 11
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: creating a text file at run time and writing to it.
by zigdon (Deacon) on Jul 20, 2006 at 11:47 UTC
    Is it possible you're running on windows? Since you don't chomp your input, you're going to try to create a file called 'john\n.txt', which might blow up on a windows filesystem. What is the exact error you get when you try to create the file? Try this:
    #!/usr/bin/perl -w use strict; open (NAMES, "names.txt") or die "Failed to read names: $!"; while my $name (<NAMES>) { chomp $name; open(OUT, ">$name.txt") or die "Failed to write $name.txt: $!"; print OUT " hellow"; close OUT; } close NAMES;

    -- zigdon

      Absolutly correct - updated. That's what I get for switching to a named var from $_ :)

      -- zigdon

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: creating a text file at run time and writing to it.
by Hofmator (Curate) on Jul 20, 2006 at 11:16 UTC
    You have to write to the file: print OUT " hello ";

    -- Hofmator

      Hi Gives the mesg " cannot open file for writing" invalid argument
        Are you sure your have the required permissions to write to the file?

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: creating a text file at run time and writing to it.
by davido (Cardinal) on Jul 20, 2006 at 16:21 UTC

    Here's how I would re-write that code:

    use strict; use warnings; open my $name_fh, '<', 'name.txt' or die "Couldn't open name.txt.\n$!"; my $y = 0; while ( <$name_fh> ) { chomp; next unless $_; # Skip empty filenames. my $name = $_ . '.txt'; open my $out_fh, '>', $name or die "Couldn't open $name for output:\n$!"; print $out_fh "Hello\n"; $y++; close $out_fh or die "Couldn't close output file $name.\n$!"; } close $name_fh; # Don't need to check for errors closing input files. print "Created $y files.\n";

    I've used lexical filehandles, avoided creating a list of filenames by simply dealing with the names one by one, checked for errors as needed. Note: I didn't validate the input. In other words, if your input file has garbage for filenames, you're going to get garbage filenames, or possibly illegal filenames. The only validation I've done is to skip empty filenames. Oh, and I also used strict and warnings as a best practice.


    Dave

Re: creating a text file at run time and writing to it.
by Moron (Curate) on Jul 20, 2006 at 14:56 UTC
    To add to phenom's recommendations, I'd also suggest letting Perl allocate the filehandle rather than hardcoding it, e.g. open my $fh, ...

    -M

    Free your mind

A reply falls below the community's threshold of quality. You may see it by logging in.