0

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eve
  • 41
  • 1
  • 6
  • 4
    The first line is contradictory: you open for writing, but the error message says "reading", which is misleading. – Svante Aug 02 '10 at 21:31
  • @Svante: copy-and-paste.. the source of 54.8% of all bugs in the universe. – DVK Aug 02 '10 at 22:57
  • Why are you writing code that generates code? That's an unusual thing to do, particularly in a basic script. – Ether Aug 02 '10 at 23:15
  • Not easy to see due to the long lines: in the question it is **Firefox/1.5.0.5');"** ---- In DVK's answer it is **Firefox/1.5.0.5')";** – Peter Mortensen Aug 23 '10 at 09:07

3 Answers3

2

You missed the closing ' " ' (double quote) at the end of the last print's string (before semicolon).

Should be:

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')";

... Firefox/1.5.0.5')"; # To show end of that line without scrolling

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.

DVK
  • 126,886
  • 32
  • 213
  • 327
  • yes but when i put " at the end of last print i get this error again . I edit it – Eve Aug 02 '10 at 21:22
  • @Jessica: Did you put it before ':' as in the example in the answer? – DVK Aug 02 '10 at 21:23
  • @Jessica - nothing to be sorry about,my wording was't as clear as it shoulda been (this the actual code example :) Did it work now? – DVK Aug 02 '10 at 21:26
  • @Jessica - LOL... I'm gonna have to pass on the kiss upon the threat of bodily harm from Mrs. DVK, but "accepting" this answer by clicking on a checkmark next to it and/or up-voting it by clicking on an up-arrow next to it would be very much appreciated. – DVK Aug 02 '10 at 21:59
1

You're missing a semicolon on the end of this line:

print FILEHANDLE "use LWP::UserAgent;"
Phil
  • 2,308
  • 1
  • 18
  • 23
  • original version of the question had the semicolon but not the closing quote... the one out there right now is an updated one – DVK Aug 02 '10 at 21:27
0

This is how you'd write it in modern Perl:

use autodie qw(:all);
{
    open my $handle, '>', 'ex.pl';
    print {$handle} <<'PERL_SOURCE';
use LWP::UserAgent;
…
#  ↓ no variable quoting necessary thanks to here-document
my $ua = LWP::UserAgent->new(agent => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.5) Gecko/20060719 Firefox/1.5.0.5');
…
PERL_SOURCE
}

As Ether hinted at in comments at the top, it's almost never necessary to write out dynamically generated code into a file. eval and Moose::Meta::* exist for a reason.

daxim
  • 39,270
  • 4
  • 65
  • 132