Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2022-12-04 19:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?