Re: cat (.sh) in Perl Seanie wrote: > yitzle wrote: >>`cat m.top.html m.mid.html m.arc.html m.bot.html > blah` >>How can this be done without a system call? >  > As a general rule, if your script contains 'system()' anywhere in it, you've  > done it wrong. This is especially true for simple file operations such as  > the above, and for anything else CPAN has yet to fail me. I can't remember  > the last time I was forced to shell out to get something done. >  >>Here's my first guess: >>@doc = (); >>for (qw/m.top.html m.mid.html m.arc.html m.bot.html/) { >>	open $FILE,"<","$root/$_"; >>	my @tmp = <$FILE>; >>	@doc = (@doc,@tmp); >>	close $FILE; >>} >>open $FILE,">","blah"; >>print $FILE $_ foreach(@doc); >>close $FILE; >> >>Is there something I'm missing? >  > Quite a lot, yes. >  > Always use the strict and warnings pragmas, no exceptions, and test to ensure  > operations on files, sockets etc have worked before moving on. > You don't need to loop your file contents in, and then loop them back out  > again separately later; one for loop will suffice for both. > Your syntax for the open() statements is a bit dodgy too :-) Perl defines the syntax so you must mean something else?  :-) > Here's a fairly verbose chunk of code that ought to do what you want: >  > use strict; > use warnings; > open OUTFILE, ">blah" or die "error creating output file\\n"; You should also include the $! or $^E variable in the error message so you know *why* it failed. > for my $infile (qw|m.top.html m.mid.html m.arc.html m.bot.html|){ >   die "$infile not found" unless -r "$root/$infile"; # or use next to skip it $infile could exist and not be readable so the message "$infile not found" does not accurately describe the problem and if you are logged in as root then the file is readable reguardless.  Also you have a race condition where the file could be made unreadable between that statement and the next one. >   open INFILE, "<$root/$infile" or die "error opening input file\\n"; See above about $! and/or $^E. >   print OUTFILE while (); >   close INFILE; >   } > close OUTFILE; >  > Keep in mind that there are plenty of other ways of doing this, some terser  > than others. You can even do this: >  > perl -pe m.top.html m.mid.html m.arc.html m.bot.html > blah >  > ....which is Perl doing a very convincing cat impression. Except that you are saying that 'm.top.html' is a Perl program that perl should compile and run (which will probably produce a *lot* of error messages.) John --  Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order.       -- Larry Wall --  To unsubscribe, e-mail: beginners-unsubscribe@perl.org For additional commands, e-mail: beginners-help@perl.org http://learn.perl.org/