|
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: Uri Guttman on 22 Apr 2008 14:41 >>>>> "JE" == J�rgen Exner <jurgenex(a)hotmail.com> writes: >> my $pat; eval '$pat = qr/$_/' or do {prompt $@; next}; JE> And this is where your unorthodox style bites you. Did you really mean JE> to eval() the whole rest of the line? Because the argument to eval is JE> indeed JE> qr/$_/' or do {prompt $@; next} JE> I suppose you meant to eval only the JE> qr/$_/' or has a very low precedence and that should parse as he intended. sure it is bad style in other ways but or will bind lower than a call to eval and its single EXPR. JE> However, I wonder what you are trying to achive by using eval in the JE> first place. I don't see any need for it. i said that before and showed how to just use the qr// directly in my map example. which he then didn't wrap in a sub as i only showed the map call. 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: John W. Krahn on 22 Apr 2008 14:57 Jürgen Exner wrote: > "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. > >> I simply can't see what's wrong with this: > > You got some 'intesting' ways of doing things. I know, there is more > than one way to do things, but your style is kind of pushing it > >> #!/usr/bin/perl -w > > Better to use > use warnings; >> use strict; >> >> my $killrc = "sample.killrc"; >> my @filter; >> my @list1 = qw( Mon Tu Wed); >> open(FILE, "<$killrc") and do { > > The more standard way would be > open (FILE, "<", $killrc) or die ("Cannot open $killrc: $!"; > >> @filter = (); >> foreach (<FILE>) { >> chomp; length or next; /^#/o and next; > > Probably it is just me, but I have an strong adversion against mixing > expressions and flow controll in such a way. I would write it as > next unless length; > next if /^#/o; Also the /o option is superfluous there as there are no variables in the pattern. > IMNSHO this is _much_ easier to read and understand. > >> my $pat; eval '$pat = qr/$_/' or do {prompt $@; next}; > > And this is where your unorthodox style bites you. Did you really mean > to eval() the whole rest of the line? Because the argument to eval is > indeed > qr/$_/' or do {prompt $@; next} No it isn't: $ perl -MO=Deparse -e'my $pat; eval q[$pat = qr/$_/] or do {prompt $@; next};' my $pat; unless (eval '$pat = qr/$_/') { do { $@->prompt; next; }; } -e syntax OK > I suppose you meant to eval only the > qr/$_/' It looks like he meant to eval the string '$pat = qr/$_/'. > Solution: enclose the argument to eval in paranthesis and the mysterious > syntax error is gone: The syntax error is not related at all to this statement. Jim Cochrane posted the correct solution. The problem is that the do {} block following the open() does not end with a semicolon. > my $pat; eval ('$pat = qr/$_/') or do {prompt $@; next}; > > However, I wonder what you are trying to achive by using eval in the > first place. I don't see any need for it. > >> push @filter, $pat; >> } >> close(FILE); >> >> } ^^^^ Missing ; for do {} block. >> print @filter; >> print @list1; John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall
From: Uri Guttman on 22 Apr 2008 16:21 >>>>> "JWK" == John W Krahn <someone(a)example.com> writes: JWK> The syntax error is not related at all to this statement. Jim Cochrane JWK> posted the correct solution. The problem is that the do {} block JWK> following the open() does not end with a semicolon. >>> } JWK> ^^^^ JWK> Missing ; for do {} block. another reason to avoid do blocks. in a 10k line system i wrote i found 2 uses of do blocks. and in the OP's case the normal open or die/return is of course the better style. having a large do block after an or is just insane. an if/then would do the same and be much more normal and readable if you couldn't use statement modifiers or such. 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: Gerry Ford on 22 Apr 2008 17:26 "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? 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. -- "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
From: nolo contendere on 22 Apr 2008 17:30
On Apr 22, 5:26 pm, "Gerry Ford" <ge...(a)nowhere.ford> wrote: > "Jim Cochrane" <allergic-to-s...(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 <ge...(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.u...(a)foobar.com>)(?-xism:^Subject:.*MONEY)(?-xism:^Message-ID:.*googlegroups)MonTuWed > > #end output show sample.killrc > ^From:.*<bad.u...(a)foobar.com> > ^Subject:.*MONEY > ^Message-ID:.*googlegroups > > Any ideas where the ?-xism is coming from? those are from the compiled regex. check out the effect of qr// on a simple regex by printing it. |