Thanks, Dave.

To answer some previous comments,

Why modify an existing program? I want to learn how to 'use strict' (to catch my tpyos) and to scope properly. This is a small, manageable program, and it's easier to understand and correct my mistakes now than after it grows. It's going to be part of a larger project that might even generate <gasp> income. So I want to write it well.

I'd like to ask 'why' too; why make the iteration variable global? Laziness. There are a few variables (including the outermost loop variable) that all the subs use , and rather than code and keep track of all the passing of parameters to subroutines, I made them global. Of course, I've spent more time trying to understand this issue than I ever would have passing parameters, but hey, that's how you learn.

What confused me was this: If I satisfy strict with

foreach my $color (@colors) { ... }
then I expect $color to be local to the loop. When
my $color;
outside of the loop satisfied strict for the loop variable $color I expected $color would be available outside of the loop during iteration. It wasn't.

There's still one question unanswered - if I use 'our $color' instead of 'my $color', the subroutine can see $color. I know that 'our' scopes globally, and I thought that 'my' at the beginning of the file, outside of any block, would scope to the whole file (effectively global), but it doesn't. So why are they different?

Thanks,

Queeg

use strict; my @colors = qw ( red green blue ); our $color; # our makes it work, my doesn't foreach $color (@colors) { print "\$color is '$color' in loop, "; too_lazy_to_code_passing_parameter(); } #### sub too_lazy_to_code_passing_parameter { print " in sub value is '$color'\n"; } $color is 'red' in loop, in sub value is 'red' $color is 'green' in loop, in sub value is 'green' $color is 'blue' in loop, in sub value is 'blue'

In reply to Re^2: strict, scope, my and foreach - not behaving as expected by queeg
in thread strict, scope, my and foreach - not behaving as expected by queeg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.