Automatic generation of textfiles
tamaguchi
created: 2006-02-03 07:54:59
I have the problem. I would like to be able to automaticly generate many outfiles in .txt format in a forloop. Quite similar to when I write "program.pl >outfile.txt" in the command window, exept that now I have to generate many outfiles. Do you have an idea how to create the textfiles automaticly? Thanks for any help.
Re: Automatic generation of textfiles
created: 2006-02-03 08:06:38

I suggest you take a look at perldoc -f open and your questions should be answered.


Dogma is stupid.
Re: Automatic generation of textfiles
created: 2006-02-03 08:31:12
You can use exec or system: exec(':> $filename');
Re^2: Automatic generation of textfiles
created: 2006-02-03 08:35:15

While it's possible in shells to use exec to reopen descriptors, Perl's exec is a different beast. You'll either replace your running program with a very short lived shell, or you'll spawn a child shell which won't affect the parent process' file descriptors in any way.

Re^3: Automatic generation of textfiles
created: 2006-02-03 08:55:47
Maybe I get it wrong, but it seems to me that tamaguchi asked how to create files, not open.
Re^4: Automatic generation of textfiles
created: 2006-02-03 09:06:23

The example he gave was using shell redirection to capture the output of his script. And even so, it's overkill to spawn another shell when a simple { open( my $fh, ">", $newfile ) or die "open $newfile: $!\n" } would suffice.

Re^4: Automatic generation of textfiles
created: 2006-02-04 06:59:55
$ mkdir foo
$ cd foo/
$ perl -e 'open F, ">bar"'
$ ls
bar
Re^2: Automatic generation of textfiles
created: 2006-02-03 08:47:51

Huh?!? Low and behold I'd just point him to

my %fh;
open $fh{$_}, '>', $_ 
  or die "Can't open `$_': $!\n"
  for @many_files;
# if they're not TOO many ;-)
Re: Automatic generation of textfiles
created: 2006-02-03 08:42:30

tirwhan is right, but his link is taking forever to load, at least for me, so I'll add a little more information. Basically, you're looking for information on filehandles. You can open a file in output mode by doing something like open MYHANDLE, '>', $filename or die $!; and then you can write stuff to that open file with something like print MYHANDLE $stuff;

To create multiple files, just do that repeatedly. Some wording in your question leads me to believe you already know a way to do something repeatedly.


Sanity? Oh, yeah, I've got all kinds of sanity. Why, I've got so much sanity it's driving me crazy. In fact, I've developed whole new kinds of sanity.
Re: Automatic generation of textfiles
created: 2006-02-03 09:00:49
If I understand you correctly, then what you're asking for is known in the Unix world as "tee". I remember having seen several solutions to solve this neatly, in Perl. In fact, it's even in the official perlfaq:
How do I print to more than one file at once?

perlmonks.org content © perlmonks.org and bart, blazar, Fletch, idle, jonadab, tamaguchi, tirwhan

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03