CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Creating a file with special Chars in Perl

 



tuxy94
New User

Apr 3, 2008, 1:33 AM

Post #1 of 2 (2709 views)
Creating a file with special Chars in Perl Can't Post

Hello,

I would like to know how I can create a file that contain special characters in Perl. I have been trying to create a file, with the following function, and it works but it creates an error when it run. I think it is due to the data I am trying to put in my file, for example there are %, @ and others that are to be printed in the file, and I think that the error is due to this. Any ideas on how I can accomplish this? I have attached my code with the error message and one line to be put in the file.

Thanks,

Code:


sub
CreateConfig{

my ($file,@data) = @_;

$file = $file.".new";

open(NFILE,">$file");

foreach my $item (@data){

printf NFILE "$item\n";

}

close(NFILE);

}


Error:


Modification of a read-only value attempted



One line of data from the array @data:


passwd chat = *New*UNIX*password* %n\n *ReType*new*UNIX*password* %n\n *passwd:*all*authentication*tokens*updated*successfully*



KevinR
Veteran


Apr 3, 2008, 8:27 AM

Post #2 of 2 (2697 views)
Re: [tuxy94] Creating a file with special Chars in Perl [In reply to] Can't Post

Your program appears to be trying to change the value of a variables that perl thinks or knows to be "read-only". The only modification to a variable in the code you posted is this line:

$file = $file.".new";

Either that or the error is not associated with the code you posted. Printing "special" characters to a file would not be the problem. An example of a read-only variable:


Code
$foo =~ /(foo)/; 
print $1; <--$1 is read-only
$1 .= 'bar'; <-- perl returns an error (read-only modification)

-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives