Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Strictly frustrating !

by marinersk (Priest)
on Jul 26, 2013 at 16:00 UTC ( #1046547=note: print w/replies, xml ) Need Help??


in reply to Strictly frustrating !

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!)

Replies are listed 'Best First'.
Re^2: Strictly frustrating !
by PearlsOfWisdom (Initiate) on Jul 26, 2013 at 16:40 UTC
    Thank you marinersk. I think with the help of the various hints here, I've managed to get it working. Thank you all.
      I know strict can be frustrating when starting out however if you get used to coding with strict in mind you will save so much time troubleshooting larger scripts and it helps with making your scripts more readable to others.

      Sparky
      FMTEYEWTK

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1046547]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2022-11-29 03:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?