in reply to Escaping special chars to add in a file
The easy fix is to use print instead of printf. See also the documentation of printf, which says:
Don't fall into the trap of using a printf when a simple print would do. The print is more efficient and less error prone.
I'm not sure why printf has this problem, but it seems to fatally dislike that you pass it strings with arguments to be interpolated (%) where there is nothing to interpolate. I'm not sure that it should choke on this. I won't provide you the "other easy" fix for this, because you should use print instead if you want to output a simple string.
You should also indent your code properly, not clobber NFILE (or even better, use lexical filehandles) and check whether open fails:
use strict; sub CreateConfig{ my ($file,@data) = @_; $file = $file.".new"; local *NFILE; open(NFILE,">$file") or die "Couldn't create '$file': $!"; foreach my $item (@data){ print NFILE "$item\n"; }; close(NFILE); } CreateConfig('test.cfg','passwd chat = *New*UNIX*password* %n\n *ReTyp +e*new*UNIX*password* %n\n *passwd:*all*authentication*tokens*updated* +successfully*');
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Escaping special chars to add in a file
by kyle (Abbot) on Apr 03, 2008 at 15:10 UTC |