Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • I notice that one of the first examples of code in the chapter available online includes this:

    # filename:

    use strict;
    use warnings;

    # Declare $counter lexical within the foreach loop
    foreach my $counter (0 .. 10)
        print "Counter = $counter\n";

    # $counter has disappeared -- fatal compilation error!
    print "Counter at the end is $counter\n";

    I was wondering why $counter is incremented. I thought that would be taken care of as it loops over (0 .. 10). Is that something that is included just to make the point easier to see, or a typo, or does it do something that is not obvious to me?
    • Actually, $counter is not incremented -- the program fails to compile or execute, because "use strict" is active and catches a problem before execution can begin. use strict outlaws the use of unqualified global variables, meaning that every variable must be a lexical (declared with my) or a qualified global (with a $full::package::name).

      In this sample program, $counter is declared to be lexical for the "foreach" block. So foreach increments $counter once per iteration -- and when the block ends, our lex
      • I think he was referring to the apparently spurious "$counter++;".
        • Whoops! The $counter++ is indeed unnecessary, and I'm surprised (er, embarrased?) that neither I nor no one else caught it.

          I guess that the errata listing on my site will go up later today...
          • Writing books is hard, and proofreading them is worse. If that's the worst error you have, you're lucky. :)
            • You said it! The people who complain about errata have never written or edited a book. When confronted by them, I can only shrug and say "we got dozens of people to read the book, and none of them found the bug. What else are we supposed to have done?"


              • To amplify what Gnat said, books have no symbolic debugger. They have no compiler. Every thing must be checked by hand, just like in the Golden Age of mainframe computers. This is an extraordinary task even for a simple book. What frequently seems to happen is that the harder bits of code are looked at much more carefully than the seemingly harmless bits. After all, everyone can toss of "hello, world" right?

                I've tech reviewed a half dozen books and co-authored one and I'm still amazed at how many things ge

          • I agree with chromatic: if that's as bad as it gets, be really happy about the text. :)

            Small suggestion (if I may). Throughout the earlier parts of the chapter where you're explaining lexical and package variables you've got things like: print "main::x = '$x'\n". You make your point exceedingly well, but I'd be really careful about surrounding variable names with ' marks because they're valid package separators (I didn't see you mention that either) and you might wind up saying something you didn't mean
            • You're right that people will occasionally encounter ' as a namespace delimiter instead of ::. But I purposely left out the things that people don't normally use; if you're hacking around in someone's old code that uses $main'foo, then you're probably (hopefully!) smart enough to look through some documentation. A side note wouldn't have been a bad idea, though.

              For example, I originally had a section on prototypes in the book. I dumped it because most Perl programmers don't use them on anything approa