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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Creating multiple files and writing in each file

Replies are listed 'Best First'.
Re: Creating multiple files and writing in each file
by choroba (Cardinal) on Mar 13, 2015 at 10:25 UTC
    Hi irthiza90, welcome to the Monastery!
    #!/usr/bin/perl use warnings; use strict; my @fh; # will store the filehandles. # Open multiple files. for my $i (1 .. 10) { open $fh[$i], '>', "file-$i" or die $!; } # Write in each. Not sure whether it's simultaneous enough. for my $i (1 .. 10) { print {$fh[$i]} $i; }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      You can also use IO::Tee to create a handle that outputs to several files at once:

      #!/usr/bin/perl use strict; use warnings; use feature qw/say/; use IO::Tee; my @fhs = map { open my $fh, ">", "file-$_" or die $!; $fh } 1..10; my $tee = new IO::Tee(@fhs); say $tee "AppleFritter!";

        thank you, it works.

Re: Creating multiple files and writing in each file
by Discipulus (Canon) on Mar 13, 2015 at 10:47 UTC
    welcome irthiza90,
    choroba example is what you need using lexical filehandles. The important part is after print: print can take as first argoument an optional filehandle to work with. choroba put a block { } that return the filehandle just after the print statement. You should choice the lexical way to open filehandle instead of the older one (open FH,'>','file.log'). You'll still find many examples using the old syntax and playing with typeglobs: better if you understand both, but stick with the modern form.

    This is called multiplexing output, and is what also Log::Log4perl can do for you.

    You can also look at my Multiplexing log output: Log4perl of the poors that permit you to tell different level of debugging output to go to different files (the code uses the old syntax with typeglobs!).

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Creating multiple files and writing in each file
by Laurent_R (Canon) on Mar 13, 2015 at 20:50 UTC
    Do you really want to write exactly the same contents to all the files? If so, it might be more efficient to write to one single file, close it, and make copies of the file afterward.

    Je suis Charlie.

      yes, technically the same content over and over in the files. Thank you.

Re: Creating multiple files and writing in each file
by dushyantvaghela (Initiate) on Mar 14, 2015 at 06:51 UTC
    As you start to program more advanced CGI applications, you'll want to store data so you can use it later. Maybe you have a guestbook program and want to keep a log of the names and email addresses of visitors, or a page counter that must update a counter file, or a program that scans a flat-file database and draws info from it to generate a page. You can do this by reading and writing data files (often called file I/O). For more Information please have a look @ http://www.cgi101.com/book/ch6/text.html
    Founder, Explore Quotes