Peter, et al -- ...and then Peter Scott said... % % At 11:18 AM 7/12/02 -0500, David T-G wrote: % > % >...and then Peter Scott said... % >% ... % >% possible. Later on I show the beginners a nasty example of the % >% consequences of leaving out use strict, -w/use warnings, or both. % > % >Oooh! Oooh! Show me an example or two! Please? Sounds great! % > % >Simple, concise examples are great convincers. % % Well, I actually use it to illustrate scoping, but then I show what % happens with the -w and strict removed; anyway, here you are: % ... % With strict/-w it does not run. With -w it warns but runs. With % neither it runs without any indication that it cannot possibly be doing % what the author probably intended, and yet the author clearly has a
Exactly. % fundamental misunderstanding about the nature of scoping. Then I % suggest to them that all those strict/-w - less scripts they've % encountered probably have all kinds of problems like this within them. Yeah :-) % % An even more killer example of this would be very handy. I don't know if it's more killer or not, but I've done a little bit of reformatting to make it easy to comment out parts and then broken the example into seven steps (suitable for comparing single differences between versions and for throwing up onto something as archaic as an overhead projector), which are attached since they're so small. Step 1 fails to run and step 2 complains a bit, but then it's just code. Step 3 shows that $x gets overwritten. Step 4 exposes $z. Step 5 trashes $y. Step 6, it seems to me, is one of the most important to note. Even with warnings turned on again the code still runs quietly, and yet it obviously has problems. Not until we restore strict in Step 7 do we finally start to get some complaints, though they still won't take care of $y getting stomped. I think this could be expanded to show use of "local" or "our" vars for those who *want* to expose $z but must still see the dangers in not using "my". Just a thought. This leads me to ruminating on global variables in general. Things like config vars -- %ENV or a switch that says to do this one way or another or the like -- seem well suited to being global. Perhaps if declared with an "our" at the main scope level... Can one safely and sanely make the argument "thou shalt NEVER have an unadorned variable" or is that just pushing a little too hard? I'm sure this has been covered here on this list, though I don't remember such a strong statement in the Camel Book. Time to do some mail searching to see what you experts say :-) % % One of the best testimonials I got (probably on this issue) was % second-hand: someone was relating how a mutual colleague was creating a % program and said, "This isn't how Peter would do it, but it saves % time." It didn't work. So then they asked deferentially, "So... how % would Peter do it?" He changed the program and it worked. Anecdotes % like that make this job so worthwhile :-) *grin* I bet! % % -- % Peter Scott % Pacific Systems Design Technologies % http://www.perldebugged.com/ Thanks & HTH & HAND :-D -- David T-G * It's easier to fight for one's principles (play) [EMAIL PROTECTED] * than to live up to them. -- fortune cookie (work) [EMAIL PROTECTED] http://www.justpickone.org/davidtg/ Shpx gur Pbzzhavpngvbaf Qrprapl Npg!
#!/usr/bin/env perl use strict; use warnings; my $x; my $y; $x = "x is global"; $y = "y is also global"; print "$x, $y\n"; { my $x; my $z ; $x = "x is now local"; $z = "z is also local"; can_I_see_z(); print "$x, $y\n"; } sub can_I_see_z { my $y; $y = "y is trashed!"; print "$z\n"; }
#!/usr/bin/env perl #use strict; use warnings; my $x; my $y; $x = "x is global"; $y = "y is also global"; print "$x, $y\n"; { my $x; my $z ; $x = "x is now local"; $z = "z is also local"; can_I_see_z(); print "$x, $y\n"; } sub can_I_see_z { my $y; $y = "y is trashed!"; print "$z\n"; }
#!/usr/bin/env perl #use strict; #use warnings; my $x; my $y; $x = "x is global"; $y = "y is also global"; print "$x, $y\n"; { my $x; my $z ; $x = "x is now local"; $z = "z is also local"; can_I_see_z(); print "$x, $y\n"; } sub can_I_see_z { my $y; $y = "y is trashed!"; print "$z\n"; }
#!/usr/bin/env perl #use strict; #use warnings; my $x; my $y; $x = "x is global"; $y = "y is also global"; print "$x, $y\n"; { # my $x; # my $z ; $x = "x is now local"; $z = "z is also local"; can_I_see_z(); print "$x, $y\n"; } sub can_I_see_z { my $y; $y = "y is trashed!"; print "$z\n"; }
#!/usr/bin/env perl #use strict; #use warnings; my $x; my $y; $x = "x is global"; $y = "y is also global"; print "$x, $y\n"; { # my $x; # my $z ; $x = "x is now local"; $z = "z is also local"; can_I_see_z(); print "$x, $y\n"; } sub can_I_see_z { # my $y; $y = "y is trashed!"; print "$z\n"; }
#!/usr/bin/env perl #use strict; use warnings; my $x; my $y; $x = "x is global"; $y = "y is also global"; print "$x, $y\n"; { # my $x; # my $z ; $x = "x is now local"; $z = "z is also local"; can_I_see_z(); print "$x, $y\n"; } sub can_I_see_z { # my $y; $y = "y is trashed!"; print "$z\n"; }
#!/usr/bin/env perl use strict; use warnings; my $x; my $y; $x = "x is global"; $y = "y is also global"; print "$x, $y\n"; { # my $x; # my $z ; $x = "x is now local"; $z = "z is also local"; can_I_see_z(); print "$x, $y\n"; } sub can_I_see_z { # my $y; $y = "y is trashed!"; print "$z\n"; }
msg27522/pgp00000.pgp
Description: PGP signature