What people are objecting to is that your program is creating new variables at will. Your expression:

${"Window".$WindowCount} = new Win32::GUI::Window(...);

creates a variable named $Window0 when $WindowCount contains a value of 0. Conceptually, it seems like a nice way to do things. But as you gain experience, you find that there are just too many ways to make mistakes, and too many possibilities for unintended consequences. Code that uses this trick tends to be a maintenance headache

As an example, if $counter doesn't have a value, then you're trying to access the RegExp member of variable $Window, which either doesn't exist or doesn't contain a value. Or perhaps you used $counter for another purpose somewhere, and it contains "foo", in which case $Windowfoo similarly doesn't exist or contain a value.

When you compute your variable names, you can make mistakes which lead to rather surprising behavior in the event that your expression comes up with something annoying like "_", in which case you'd be mangling the $_ variable.

So people are suggesting that you put your variables in a hash to limit the possible interactions with other variables. People often use a hash to do this, because you can use your computed name as a hash key, like so:

$Windows{"Window".$counter} = new Win32::GUI::Window(...);

However, since your counter is simply a number, BrowserUK is suggesting that you use an array instead of a hash. After all, a hash that uses integers as a key is pretty darned similar to an array in useability:

$Windows[$counter] = new Win32::GUI::Window(...);

But an array has better performance1 and is pretty easy for new(er) programmers to use/understand. (Hashes are beautiful, but have a couple of behaviors2 that newer programmers often find surprising.)

...roboticus

Notes:

1 I think...I've never bothered to profile it, so I could easily be wrong. In any event, it would depend on how (and how frequently) you access it, too.

2 Specifically, (1) when you iterate through the keys of a hash, the order they keys are traversed is arbitrary, having no relation to the order in which you put the keys in the hash; (2) you can't sort a hash--you can access the keys in sorted order, but you have to create a new sorted list of keys to do it--you can't actually change the order of the items in the hash itself. There may be a couple of others, but they don't come to mind at the moment.


In reply to Re: win32 perl module usage problem by roboticus
in thread win32 perl module usage problem by Anonymous Monk

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.