I want to generate some lines of Perl code by using file handling in Perl, for example:
open(FILEHANDLE, ">ex.pl") or die "cannot open file for reading: $!";
print FILEHANDLE "use LWP::UserAgent;"
....
.... some code is here
....
print FILEHANDLE "my \$ua = new LWP::UserAgent(agent => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.5) Gecko/20060719 Firefox/1.5.0.5');"
But when I compile the generator code (not the generated) I get this error:
syntax error at F:\test\sys.pl line 14, near "print"
Execution of F:\test\sys.pl aborted due to compilation errors.
What am I going to do?
You missed the closing
' " '(double quote) at the end of the last print’s string (before semicolon).Should be:
Also, couple of minor notes:
Please consider using 3-argument form of
open(), not 2-argument; as well as lexical filehandles:open(my $fh, ‘>’, “out.txt”) or die “Error opening for writing: $!”;
print $fh “stuff\n”;
You don’t have a
close()of the filehandle at the end – I assume just because you gave incomplete code.