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.
  • According to the first hit I got for "java block local variables" [], it looks like the variable declared in your "for" statement is actually outside the scope of that local block, but otherwise, the scope of a local variable inside a block is in fact inside the block and may temporarily obscure outer variables.

    Now in JavaScript, the rules are different, but I presume you're smart enough not to say "Java" when you mean "JavaScript".

    • Randal L. Schwartz
    • Stonehenge
    • Apparently my example has confused nearly everyone I've asked about this. Yes, I meant Java. The for loop actually has nothing to do with it. Let me say it in Perl, and drop the for:

      sub my_routine
        my($i) = @_;
          my $i = 18;  # different $i from the other one

      Of course, you can argue all day that declaring a variable in an inner block that shadows a variable in an outer block like this is a bad idea. It probably is. :) But the question for me is: did Java designers actually sit down and intend to prevent this, or were they just wimpy language designers who couldn't figure out how to do something that's perfectly possible for Perl, C, Scheme, and even PL/SQL?

      The scenario that's going on here is I'm dealing with somebody else's 4000-line method, I was wanting to insert a little block somewhere for debugging purposes, and I wanted to declare my own temporary local variables for that block without having to worry about what other variables had been declared by Somebody Else in said monstrously-long method. It was a temporary annoyance, easily fixable reading the compiler's gripes and using a different variable name. But it spawned this larger question for me as to why Java chose to be this way.

      J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers