CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
bug not identified - how come

 



naama
New User

Aug 29, 2011, 7:49 AM

Post #1 of 7 (1776 views)
bug not identified - how come Can't Post

Hi all,

My program is long, so I will cut a long story short. I am parsing a text file that contains mostly words and letters. My question is how come these lines

if ($arr1{$i} eq '-') {$arr1{$i}='-9'}
if ($arr2{$i} eq '-') {$arr2{$i}='-9'}

didn't make my program break. The program was running fine. When I add the missing semicolons

if ($arr1{$i} eq '-') {$arr1{$i}='-9';}
if ($arr2{$i} eq '-') {$arr2{$i}='-9';}

I get different outputs than before, which means that this bug was causing a great deal of trouble. How come it wasn't detected? It's not detected even if I use "use strict;" and "use warnings;".

Thanks for your help,

N


FishMonger
Veteran / Moderator

Aug 29, 2011, 8:33 AM

Post #2 of 7 (1775 views)
Re: [naama] bug not identified - how come [In reply to] Can't Post

There's nothing wrong with those lines and the addition of the semicolon won't change the results.

Within a block, the semicolon is optional on the last statement.
See

Quote
perdoc perlsyn
...
...
Simple Statements
The only kind of simple statement is an expression evaluated for its
side effects. Every simple statement must be terminated with a
semicolon, unless it is the final statement in a block, in which case
the semicolon is optional. (A semicolon is still encouraged if the block
takes up more than one line, because you may eventually add another
line.) Note that there are some operators like "eval {}" and "do {}"
that look like compound statements, but aren't (they're just TERMs in an
expression), and thus need an explicit termination if used as the last
item in a statement.
...
...



naama
New User

Aug 30, 2011, 5:16 AM

Post #3 of 7 (1703 views)
Re: [FishMonger] bug not identified - how come [In reply to] Can't Post

Thanks for your reply. Glad to know that the script worked fine so far...

Unrelated question - if I am running my scripts in the background on linux machines, is there a way to save messages that are printed to the screen? These are messages that are printed since I am using "use warnings".

Thanks again, I really appreciate it ! ;)


FishMonger
Veteran / Moderator

Aug 30, 2011, 10:13 AM

Post #4 of 7 (1694 views)
Re: [naama] bug not identified - how come [In reply to] Can't Post

You can use command line output redirection to redirect stderr to a file. Or, you can accomplis the same thing in the script by closing STDER and reopening it to a file.


naama
New User

Sep 1, 2011, 3:53 AM

Post #5 of 7 (1649 views)
Re: [FishMonger] bug not identified - how come [In reply to] Can't Post

I tried two things and both did't work -

perl script.pl > txt_file &

and

perl script.pl & > text_file

in both cases my screen is flooded with with messages (for some reason the same message is repeated many many times) and there is nothing in the text file! What am I doing worng? This script parses a huge datafile, under linux.

Thanks a lot again, I really appreciate your help.


rovf
Veteran

Sep 1, 2011, 5:46 AM

Post #6 of 7 (1643 views)
Re: [naama] bug not identified - how come [In reply to] Can't Post

Maybe you should catch stderr to a different file?

If this doesn't help, it means that the script is outputting directly to the console. This is rare, but sometimes scripts are written that way.


FishMonger
Veteran / Moderator

Sep 1, 2011, 7:10 AM

Post #7 of 7 (1640 views)
Re: [naama] bug not identified - how come [In reply to] Can't Post

The proper syntax is:


Quote
perl script.pl > text_file 2>&1 &


or


Quote
./script.pl > text_file 2>&1 &


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives