|
Prev: FAQ 4.44 How do I test whether two arrays or hashes are equal?
Next: FAQ 5.3 How do I count the number of lines in a file?
From: Tad J McClellan on 22 Apr 2008 07:52 Gerry Ford <gerry(a)nowhere.ford> wrote: > How does a person invoke > the debugger? perldoc -q debug How do I debug my Perl programs? -- Tad McClellan email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
From: Jim Cochrane on 22 Apr 2008 21:08 On 2008-04-22, Gerry Ford <gerry(a)nowhere.ford> wrote: > > "Jim Cochrane" <allergic-to-spam(a)no-spam-allowed.org> wrote in message > news:slrng0r3ie.j1a.allergic-to-spam(a)no-spam-allowed.org... >> On 2008-04-22, Gerry Ford <gerry(a)nowhere.ford> wrote: > >> Change: >>> } >> >> to: >> }; >> >> (i.e., missing a ';' - and your code could be formatted better) > Thanks, Jim. Missing semicolons were the culprit: > > #!/usr/bin/perl -w > > use strict; > use warnings; > > my $killrc = "sample.killrc"; > my @filter; > my @list1 = qw( Mon Tu Wed); > open(FILE, "<$killrc") and do { > @filter = (); > foreach (<FILE>) { > chomp; length or next; /^#/o and next; > my $pat; eval '$pat = qr/$_/' or do {prompt $@; next}; > push @filter, $pat; > }; > close(FILE); > > }; > print @filter; > print @list1; > > # perl mats5.pl 2>text50.txt >text51.txt > __END__ > #end script begin output > > (?-xism:^From:.*<bad.user(a)foobar.com>)(?-xism:^Subject:.*MONEY)(?-xism:^Message-ID:.*googlegroups)MonTuWed > > #end output show sample.killrc > ^From:.*<bad.user(a)foobar.com> > ^Subject:.*MONEY > ^Message-ID:.*googlegroups > > Any ideas where the ?-xism is coming from? > >>> Perl.exe says: >>> syntax error at mats5.pl line 18, near "print" >> >> The compiler was pointing pretty much right at the problem. > I beg to differ. I snipped this from an actual, working perl program. How > do you know which braces need a semicolon after? If I remember correctly, line 18 (from your error message) was either the offending line with the close curly brace missing the ';' or the line (print ...) below it. So you know where to look because of the line # in the error message. Of course, now that you know that 'do {}' blocks need a semicolon after them (right?), you know to look for that now, too. (Of course, compile error messages will not always report a line number that is at or very close to the actual problem in the code.) > > sub read_killrc { > open(FILE, "<$killrc") and do { > @filter = (); > foreach (<FILE>) { > chomp; length or next; /^#/o and next; > my $pat; eval '$pat = qr/$_/' or do {prompt $@; next}; > push @filter, $pat; > } > close(FILE); > }; > } > > The error had nothing to do with the print statements, but with a missing > token several lines before. At least now I've got output, so I can waddle > along. --
From: Uri Guttman on 23 Apr 2008 04:36 >>>>> "JC" == Jim Cochrane <allergic-to-spam(a)no-spam-allowed.org> writes: >> do you know which braces need a semicolon after? JC> If I remember correctly, line 18 (from your error message) was either JC> the offending line with the close curly brace missing the ';' or the JC> line (print ...) below it. So you know where to look because of the JC> line # in the error message. Of course, now that you know that 'do {}' JC> blocks need a semicolon after them (right?), you know to look for that actually do blocks are just expressions and the statement they might be in will need a ; (and of course in perl ; is a statement separator and not a terminator). in this case the do block was the end of the statement and needed a ; before the print. an if block (or other flow control block isn't a statement so they don't need ; after them. uri -- Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Free Perl Training --- http://perlhunter.com/college.html --------- --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: J�rgen Exner on 23 Apr 2008 07:08 Uri Guttman <uri(a)stemsystems.com> wrote: >>>>>> "JC" == Jim Cochrane <allergic-to-spam(a)no-spam-allowed.org> writes: > > >> do you know which braces need a semicolon after? > > JC> If I remember correctly, line 18 (from your error message) was either > JC> the offending line with the close curly brace missing the ';' or the > JC> line (print ...) below it. So you know where to look because of the > JC> line # in the error message. Of course, now that you know that 'do {}' > JC> blocks need a semicolon after them (right?), you know to look for that > >actually do blocks are just expressions and the statement they might be >in will need a ; (and of course in perl ; is a statement separator and >not a terminator). in this case the do block was the end of the >statement and needed a ; before the print. an if block (or other flow >control block isn't a statement so they don't need ; after them. Another reason to avoid do{} blocks that are dozens of lines long. At the end of such a block you surely have forgotten, if it is a flow controll block or an expression. jue
From: Gerry Ford on 23 Apr 2008 20:53
"J�rgen Exner" <jurgenex(a)hotmail.com> wrote in message news:21mr04pro5k3sqjbebv5g2jq5i0doq42t3(a)4ax.com... > "Gerry Ford" <gerry(a)nowhere.ford> wrote: >>Perl.exe says: >>syntax error at mats5.pl line 18, near "print" >>Execution of mats5.pl aborted due to compilation errors. > > Man, dude!!! How are we supposed to guess??? You could have told us > that your beef is not with the functionality of print() but with a > syntax error in that program! Thank you very much for throwing around > red herrings. My problem was that I thought I had a working script before I added the print statements. At this point, I'm simply very error-prone. This is my current version of this script, that I think addresses your criticisms: #!/usr/bin/perl use strict; use warnings; use Data::Dumper qw( Dumper); my $killrc = "kill.rc"; my @filter; open (FILE, "<", $killrc) or die ("Cannot open $killrc: $!"); @filter = (); foreach (<FILE>) { chomp; next unless length; next if /^#/o; my $pat; eval ('$pat = qr/$_/') or do {prompt $@; next}; push @filter, $pat; close(FILE); } print @filter; #print STDERR "The list is " . Dumper( @list ) . "\n"; __END__ How would you indent this? > However, I wonder what you are trying to achive by using eval in the > first place. I don't see any need for it. I could just omit it? -- "Life in Lubbock, Texas, taught me two things: One is that God loves you and you're going to burn in hell. The other is that sex is the most awful, filthy thing on earth and you should save it for someone you love." ~~ Butch Hancock |