in reply to Re: newbie learning perl
in thread newbie learning perl
Ouch! So much I dont understand. What is the difference between use strict; & use warnings; and the default settings? Is there an advantage to using my when assigning a value?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: newbie learning perl
by GrandFather (Saint) on Feb 19, 2015 at 20:21 UTC | |
If you don't use "strict" and "my" Perl makes any variables you use "global" (it's a little more subtle that that, but the details aren't important for now). As a general thing global variables are bad news because it's hard to tell where their value might change. "my" declares a variable in the local scope and strict enforces that. strict picks up errors that can be detected at compile time before the program runs. warnings tell you about dodgy stuff that happens as the program runs, like variables that are used before they have been given a value or using == where eq should be used. In Perl scope is important. It controls where a lexical variable (one declared with my) can be used. For example:
Perl is the programming world's equivalent of English
| [reply] [d/l] |
by mkesling (Initiate) on Feb 20, 2015 at 19:03 UTC | |
I added use strict and use warnings. Yikes, lots of errors. I tried to clean them all up. Down to one I cant figure out. Global symbol "@xloc" requires explicit package name The array xloc is used in 2 locations so generates this warning twice. Each item in the array represents the x location of a T and the location in the array represents the y location. The line: if ($skierx eq $xloc$skiery) {print "CRASH!!!"; exit;} is how I detect that the skier has hit a tree. A different warning said I should use $ instead of @ infront of xloc. I dont get it. I seek the wisdom of the monks.
| [reply] [d/l] |
by GrandFather (Saint) on Feb 20, 2015 at 22:44 UTC | |
There are a goodly number of smallish issues in your code and a few outright bugs. Some of the issues clean up if you go back and re-read the scoping reply I wrote above. I've reworked your current code to clean up variable scope and to correctly handle lines and collision detection. Look carefully at how @xloc is managed. I've also removed the END block code and instead put the main work in a sub wrapped by appropriate ReadMode calls. As icing a few blank lines are put at the start of the run to give skiers time to get their eye in and you can tune the number of trees generated per line.
Please read the Perl docs for anything you can't understand, or ask here if you really can't figure it out. But do work through the code line by line and understand what is going on. Oh, and really don't put multiple statements on a line!
Perl is the programming world's equivalent of English
| [reply] [d/l] |
by RonW (Parson) on Feb 20, 2015 at 20:58 UTC | |
Add a declaration for @xloc
| [reply] [d/l] |
by mkesling (Initiate) on Feb 20, 2015 at 20:30 UTC | |
When I comment out use strict and use warnings and ignore the problems in the prior post the game works as intended. However when the score gets to 144 an extra line is added to the addition of the T obstacles and after a few loops the skier gets moved to the bottom of the page. I have no idea why this happens at score of 144. This repeats every time. I changed this so the comma key moves the skier to the left and the period key moves the skier to the right. This game is working except for problem above on my windows 7 machine. Just for fun going to try and get it to work on my linux machine. | [reply] |