Fwd: re: [MacPerl-WebCGI] A v B = ?
[email protected] Sat, 10 Mar 2001 05:02:57 +0900
| Newsgroups | perl.macperl.webcgi |
|---|---|
| Message-ID | <v04011700b6ced4f1deb9@[61.115.107.194]> |
[email protected] wrote: >every time I think I have something figured out, someone like >you comes along and shows me that I don't! It's frustrating -- I get that a lot on this list too :-), but that's also why I'm still suubscribed - it's good to have your convictions challenged because sometimes they stop you from seeing other stuff >..... the -w switch...... If something doesn't compile, should it also >not run? imagine this, the first time you write a script which reads in from a text file you put: open (IN, 'text_file.text'); this works. The script runs and your happy, but it has introduced a very subtle bug into the script - if something happens to 'text_file.text' suddenly your script starts failing as later parts of the script will assume that the data which comes from (the now missing ) 'text_file.text' will always be there when they as for it. But for the sake of argument, lets say this first script never fails and you get into the habit of writing every open() statement in the same way because it has always worked before. Then one day, a butterfly in outer Mongolia flaps it's wings, and the air it displaces turns into a breeze, which finding itself somewhere off the Bearing Strait, gets sucked into a vicious convection current giving it a whole load of spin. Next thing you know this wild hurricane is passing by the computer company that has the server which houses a web site you built, causing the photocopier manual to fall on the HD physically damaging one of the blocks which just happens to be part of a text file your CGI uses to read from and store crucial data. This of course is years after you wrote the script and you no longer even remember why you wrote the things you did, and now you have to figure out why it's not working anymore. Or to be more brief, a lot of things can go wrong so it's better to get into the habit of programming to limit the number of problems you could make for yourself, so: open (IN, 'text_file.text')|| die "can't find the swine anymore : $!"; is better than open (IN, 'text_file.text'); and die() is just another form of '-w' - something which can save you grief and time. >Please note the following code -- which is a version of yours/mine (I >couldn't get yours to run as stated -- the "-w" option). While the >following will run, and do what is intended, it will NOT run with the >"-w" option. Why is '-w' optional? Perl was conceived out of a desire to be able to write quick and dirty scripts to deal with the logistics of running a computer from the command line. Other languages (like C) are intensely anal retentive as far as programming structure goes, so if you want a quick three liner to look for a file called 'bob' in all your dirs you write it in perl not C because you can wham out a solution without having to go through the stylistic contortions of a sadistic grammarian. CGIs however aren't quick and dirty three liners, they're a lot longer, and the longer you make a script the more potential you have for introducing errors (typos, wrong copy n pastes, whatever). The '-w' flag doesn't check for logical errors it checks for things like ';' at the end of each line or that your commas and braces match. Basically structure. The script which > will run, and do what is intended, it will NOT run with the "-w" option run with the '-w' flag, gives the error: # Can't find string terminator " DB" anywhere before EOF. File 'test'; Line 10 line 10 was: print <<" DB"; As per the FAQs I checked for common errors with '<<here' docs and found the corresponding tail tag wasn't the same - it had 1 space too many, so I took away the xtra space, the error disappeared, but I got another which was: # Can't find string terminator " ROW" anywhere before EOF. File 'test'; Line 34 line 34 was: <<" ROW"; On a hunch I assumed the corresponding tail tag wasn't the same , so I took away 1 space, the error disappeared and the script then ran as expected. So what, you say, it ran anyway without the '-w' switch. Sure but supposing you added more parts to the script and suddenly it stops working because the interpreter thinks whatever comes after the '<<here' doc is part of the '<<here' doc because it hasn't found the tail tag. It's another 'subtle' bug like not using || die( )in the open() statement. Point being, without the '-w' switch you just won't see certain errors. Having said all that, sometimes you have to do stuff just to keep '-w' happy, but that's the price you pay for being able to eliminate all kinds of probs very early on before things get complicated. to paraphrase you: if a airplane doesn't explode in flight, but an engine drops off due to faulty maintenance, the plane'll land and deliver its passengers, but they won't necessarily be grateful to the airline. HTH Robin