Summary of Problem: You don't seem to understand what strict does.

The use of strict has many, many advantages, but the main one for new Perl programmers is it helps you catch typos in your variable names (scalars and arrays for sure, though it can occasionally be less helpful in this regard with hashes). I ran your code as originally supplied and got the following list of errors:

C:\Steve\Dev\PerlMonks\P-2013-07-26@0942-Strict>perl testStrict.pl Global symbol "%hash" requires explicit package name at testStrict.pl +line 3. Global symbol "%hash" requires explicit package name at testStrict.pl +line 13. Global symbol "$kv" requires explicit package name at testStrict.pl li +ne 14. Global symbol "$vv" requires explicit package name at testStrict.pl li +ne 15. Global symbol "%hash" requires explicit package name at testStrict.pl +line 15. Global symbol "%hash" requires explicit package name at testStrict.pl +line 17. Global symbol "$kv" requires explicit package name at testStrict.pl li +ne 17. Global symbol "$vv" requires explicit package name at testStrict.pl li +ne 17. Global symbol "%hash" requires explicit package name at testStrict.pl +line 20. Global symbol "%hash" requires explicit package name at testStrict.pl +line 21. Global symbol "%hash" requires explicit package name at testStrict.pl +line 23. Execution of testStrict.pl aborted due to compilation errors.

The first error says it doesn't know what %hash is in line 3:

%hash = ( ' Key1', ' Value1');

The reason is because without strict, Perl will happily create %hash for you dynamically. Once you indicate you wish to use strict;, Perl surrenders this behavior and expects you to define %hash yourself.

A friend of mine once indicated he didn't use strict; because he wanted to simplify things. He created some of the most difficult Perl scripts to debug because of it. Trust me: use strict; saves you a metric ton of work more than it costs you to use it.

So back to the problem. You need to manually define %hash.

This is done, in this case, by using the keyword my. So change the line thusly:

%hash = ( ' Key1', ' Value1'); my %hash = ( ' Key1', ' Value1');
When I run this, I get:
C:\Steve\Dev\PerlMonks\P-2013-07-26@0942-Strict>perl testStrict2.pl Global symbol "$kv" requires explicit package name at testStrict2.pl l +ine 14. Global symbol "$vv" requires explicit package name at testStrict2.pl l +ine 15. Global symbol "$kv" requires explicit package name at testStrict2.pl l +ine 17. Global symbol "$vv" requires explicit package name at testStrict2.pl l +ine 17. Execution of testStrict2.pl aborted due to compilation errors.

With a single keyword, we've eliminated over half the errors.

I will bet you can deduce the remainder of your corrective actions from this (key hints exist in other responses in this thread).

Have fun!
(And don't be afraid to ask more questions. Initially, most answers lead to more questions, which is why experience is something you can't buy, you have to earn -- by doing, as you are. Keep up the good work!)


In reply to Re: Strictly frustrating ! by marinersk
in thread Strictly frustrating ! by PearlsOfWisdom

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.