Perl Print Syntax Error
Contents |
RecentThreads NewestNodes Donate What'sNew on Nov 21, 2008 at 20:41UTC ( #725224=perlquestion: print w/replies, xml ) Need Help?? ITmajor has asked for the wisdom of the Perl Monks concerning the following question: I have the following lines in perl syntax error near ") {" my code: print $q->header("text/html\n\n"), $q->start_html(-title=>"Data Report"), "This is the report for~~
"; [download] When
Perl Else If
I run the program, I keep getting "syntax error near 'print'". I have used the same format in other codes but this is the only one that gives this error. How can I solve this? Update: I appreciate the sugggestions, however the problem still remains. The line above print defines a variable and ends with a semicolon. I changed $q so that it is now my $q = CGI->new; Is there something wrong with my format? Update2: Here is the exact error-> "Syntax error at /home/www/code.cgi line 92 near "print", referer: http://localhost/reports.html" This is the code around print: my $last_time = 0; print $q->header("text/html\n\n"), #This is line 92 $q->start_html(-title=>"Data Report"), "This is the report for~~
"; [download] I'm baffled. This is the only code I'm having trouble with.Comment on Syntax error near PrintSelect or Download Code Replies are listed 'Best First'. Re: Syntax error near Print by ccn (Vicar) on Nov 21, 2008 at 20:47UTC Check if semicolon present at the end of line above the "print"[reply] Re: Syntax error near Print by ikegami (Pope) on Nov 21, 2008 at 20:52UTC That means Perl is seeing print, but it's expecting something else. The error is actually earlier. Check what's missing(?) before the print. [reply][d/l][select] Re: Syntax error near Print by TimToady (Parson) on Nov 21, 2008 at 22:13UTC If that's the actual error message, it doesn't look like a Perl error message to me. Perhaps the code isn't getting anywhere near the perl interpreter in the first place? Check your server configuration...[reply] Re^2: Syntax error near Print (lexicon) by tye (Sage) on Nov 22, 2008 at 00:08UTC If that's the real error message, then my error identification tools say that it is either an SQL or C error. So I'm betting it is a paraphrase of the error message (removing the line number and filename inf
This is okay. Everyone has his or her own little quirks. Mine is that I keep using the assignment operator instead of the equality operator. Just remember what your particular blind spot is. When errors occur, check for your personal common errors first. This section shows some common syntax errors and the error messages that are generated as a result. First, the error message are shown and then the script that generated it. After the script, we'll see why that particular message was generated. Scalar found where operator expected at test.pl line 2, near "$bar" (Missing semicolon on previous line?) $foo = { } # this line is missing a semi-colon. $bar = 5; Perl sees the anonymous hash on the first http://www.perlmonks.org/?node_id=725224 line and is expecting either an operator or the semicolon to follow it. The scalar variable that it finds, $bar, does not fit the syntax of an expression because two variables can't be right after each other. In this case, even though the error message indicates line 2, the problem is in line 1. Bare word found where operator expected at test.pl line 2, near "print("This" (Might be a runaway multi-line "" string starting on line 1) syntax error at test.pl https://www.cs.cf.ac.uk/Dave/PERL/node148.html line 2, near "print("This is " String found where operator expected at test.pl line 3, near "print("" (Might be a runaway multi-line "" string starting on line 2) (Missing semicolon on previous line?) Bare word found where operator expected at test.pl line 3, near "print("This" String found where operator expected at test.pl line 3, at end of line (Missing operator before "); ?) Can't find string terminator '"' anywhere before EOF at test.pl line 3. print("This is a test.\n); # this line is missing a ending quote. print("This is a test.\n"); print("This is a test.\n"); In this example, a missing end quote has generated 12 lines of error messages! You really need to look only at the last one in order to find out that the problem is a missing string terminator. While the last error message describes the problem, it does not tell you where the problem is. For that piece of information, you need to look at the first line where it tells you to look at line two. Of course, by this time you already know that if the error message says line 2, the error is probably in line 1. Can't call method "a" in empty package "test" at test.pl line 1. print(This is a test.\n); # this line is missing a beginning quote. The error being generated here is very cryptic and has little to do with the actual problem. In order to unde
How to get Help for Perl? Perl on the command line Core Perl documentation and CPAN module documentation POD - Plain Old Documentation Debugging Perl scripts Scalars Common Warnings and Error messages in Perl Automatic string to number conversion or http://perlmaven.com/stdout-stderr-and-redirection casting in Perl Conditional statements, using if, else, elsif in Perl Boolean values in Perl Numerical http://www.softpanorama.org/Scripting/Perlorama/Debugging/perl_error_checklist.shtml operators String operators: concatenation (.), repetition (x) undef, the initial value and the defined function of Perl Strings in Perl: quoted, interpolated and escaped Here documents, or how to create multi-line strings in Perl Scalar variables Comparing scalars in Perl String functions: length, lc, uc, index, substr Number Guessing game while loop Scope of variables in Perl Short-circuit in boolean expressions syntax error Files How to exit from a Perl script? Standard output, standard error and command line redirection Warning when something goes wrong What does die do? Writing to files with Perl Appending to files Open and read from text files Don't Open Files in the old way slurp mode - reading a file in one step Lists and Arrays Perl for loop explained with examples Perl Arrays Processing command line arguments - @ARGV in Perl How to perl print syntax process command line arguments in Perl using Getopt::Long Advanced usage of Getopt::Long for accepting command line arguments Perl split - to cut up a string into pieces How to read a CSV file using Perl? join The year of 19100 Scalar and List context in Perl, the size of an array Reading from a file in scalar and list context STDIN in scalar and list context Sorting arrays in Perl Sorting mixed strings Unique values in an array in Perl Manipulating Perl arrays: shift, unshift, push, pop Reverse Polish Calculator in Perl using a stack Reverse an array, a string or a number The ternary operator in Perl qw - quote word Subroutines Subroutines and functions in Perl Variable number of parameters in Perl subroutines Understanding recursive subroutines - traversing a directory tree Hashes, arrays Hashes in Perl Creating a hash from an array in Perl Perl hash in scalar and list context How to sort a hash in Perl? Count the frequency of words in text using Perl Regular Expressions Introduction to Regexes in Perl 5 Regex character classes Regex: special character classes Perl 5 Regex Quantifiers trim - removing leading and trailing white spaces with Perl Perl and Shell related functionality How to remove, copy or rename a file with Perl Reading the content of a directory Traversing the filesystem - using a queue CPAN Dow
Perl Xref Perl Style Perl Programming Environment Perl POD documentation Debugging Software Testing Program understanding Pretty Printing Perl Tips Humor Etc Tracking down syntax errors can be troublesome, but it's a skill that comes with practice. Most of the errors you're likely to experience are going to fall into one of the six categories below. The list below is adapted from Simon Cozen book Beginning Perl: Wrong format of Perl script file on one of the data files If you write Perl script in Windows and then push it to Unix that happens often. It's better to run via dos2unix utility all files "just in case". Missing Semicolons This is one of the most common and most easily avoidable errors. Probably the most common syntax error there is. Every statement in Perl, unless it's at the end of a block, should finish with a semicolon. Sometimes you'll get the helpful hint we got above: (Missing semicolon on previous line?) but otherwise you've just got to find it yourself. Remember that the line number you get in any error message may well not be the line number the problem occurs on � just when the problem is detected. Perl often diagnose wrong line for this error so you need to look at the line listed as a fuzzy pointer. Missing Open/Close Brackets The next most common error comes when you forget to open or close a bracket or brace. Missed closing braces are the most troublesome, because Perl sometimes goes right the way to the end of the file before reporting the problem. For example: !/usr/bin/perl # missing_braces.plx use warnings; use strict; if (1) { print "Hello"; my $file = shift; if (-e $file) { print "File exists.\n"; } This will give us: perl -w missing_braces.pl Missing right curly or square bracket at braces.plx line 12, at end of line syntax error at braces.plx line 12, at EOF Execution of braces.plx aborted due to compilation errors. > The problem is, our missing brace is only at line 7, but Perl can't tell that. To find where the problem is in a large file, there are a variety of things you can do: Indent your code as we have done to make the block structure as clear as possible. This won't affect what perl sees, but it helps you to see how the program hangs together, making it more readily obvious when this sort of thing happens. Deliberately leave out semicolons where you think a block should end, and you'll cause a syntax error more quickly. However, you'll need to remember to add the semicolon if you add extra statements to the block. Use an editor which helps you out: most editors automatically flash up matching braces and brackets (called balancing) and are freely available for both UNIX and