Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: newbie learning perl

by GrandFather (Saint)
on Feb 19, 2015 at 20:21 UTC ( [id://1117264]=note: print w/replies, xml ) Need Help??


in reply to Re^2: newbie learning perl
in thread newbie learning perl

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:

use strict; use warnings; $undeclaredVar = 0; # strict gets grumpy about this because the variab +le isn't declared my $globalVar = 0; # This variable can be used by any following part o +f the code for my $loopVar (1 .. 10) { # In the scope of the for loop. $loopVar can only be used within t +his loop # Loop variables are somewhat magical my $localVar = $loopVar * 3; # This variable is also local to the +loop. It # is a different variable each time through the loop! my $globalVar = $localVar; # Bad! This $globalVar hides the $glo +balVar # declared outside the loop. Reusing variable names like this +is very # bad style! if ($localVar == 9) { my $nestedVar = rand(10); # This variable is only available in +side this # if block. It's not available in the else part or after t +he if } else { my $nestedVar = rand($localVar); # Reusing the name here is ok +, but note # that this is a different variable than the $nestedVar in + the if # block. Note that we can use variables from the encompass +ing # scope in a nested scope. } my @trailingVar; # This array variable isn't available to any earl +ier part # of the program. In general declare variables in as small a s +cope as # you can and initialize them to the first value they should h +ave. # Arrays and hashes are created empty so they don't generally +need an # explicit initial value. } my @anotherGlobal = ('Hello', 'World'); # This global array variable i +sn't # available to the loop code above because it's declared after the + loop. # Note that we can initialize an array variable using a list
Perl is the programming world's equivalent of English

Replies are listed 'Best First'.
Re^4: newbie learning perl
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.

    #!/usr/bin/perl use strict; use warnings; use Win32::Console::ANSI; use Term::ANSIScreen qw/:color :cursor :screen/; use Term::ReadKey; ReadMode('noecho'); cls; my $col = 0; my $skierx = 40; my $skiery = 0; my $score = 0; my $key = 0; my $wait = .3; my $y = 1; #fill the screen with Ts while ($y < 100) { $col = int(rand(80)); printf ("%${col}s\n", "T"); $y = $y + 1; } $wait = 1; $skierx = 40; $skiery = 0; $y = $y + 5; locate $y,1; print " ------------------------- Ready Set GO! --------------- +----------"; while (1 < 2) { while (not defined ($key = ReadKey(-1))) { # here when no key pressed # place a T and save X location in array, position in array = Y $col = int(rand(80)); $y = $y + 1; $xloc[$y] = $col; locate $y,$col; print "T\n"; # place V $skiery = $y - 50; locate $skiery,$skierx; print "V"; #crash??? if ($skierx eq $xloc[$skiery]) {print "CRASH!!!"; exit;} #score if ($skiery < 105) {locate $skiery,1; print "PRACTICE";} if ($skiery > 105) {$score = $score + 1;locate $skiery,1; print +"$score";} # keep speeding up, reducing pause time select(undef, undef, undef, $wait); #pauses here #$wait = $wait - .005; if ($score eq 50) {$wait = .25;} if ($score eq 100) {$wait = .2;} if ($score eq 150) {$wait = .15;} if ($score eq 200) {$wait = .1;} } # here when key pressed if ($key eq ",") {$skierx = $skierx - 1;} if ($key eq ".") {$skierx = $skierx + 1;} if ($key eq "x") {exit;} } END{ ReadMode('restore'); }

      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.

      #!/usr/bin/perl use strict; use warnings; use Win32::Console::ANSI; use Term::ANSIScreen qw/:color :cursor :screen/; use Term::ReadKey; my $kLines = 50; my $kWidth = 80; my $kMaxTrees = 5; my $kPractise = 5; ReadMode('cbreak'); cls; run(); ReadMode('restore'); sub run { my @xloc; locate 1, 1; my $hLen = ($kWidth - 15) / 2 - 10; print ' ' x 5, '-' x $hLen, " Ready Set GO! ", '-' x $hLen; push @xloc, ' ' x $kWidth for 1 .. 4; push @xloc, newLine($kWidth) for 1 .. $kLines - 2; locate 2, 1; print join "\n", @xloc, ''; my $wait = 1; my $skierx = $kWidth / 2; my $y = $kLines; my $score = 0; while (1 < 2) { my $key = ReadKey(-1); if (defined $key) { # here when key pressed --$skierx if $key eq ","; ++$skierx if $key eq "."; return if $key eq "x"; } push @xloc, newLine($kWidth); # Generate a new line at the end locate ++$y, 1; print "$xloc[-1]\n"; #crash??? if ('T' eq substr $xloc[0], $skierx - 1, 1) { print " CRASH!!! "; return; } # place V shift @xloc; # Remove the previous line locate $y - $kLines + 2, $skierx; print "V"; #score locate $y - $kLines + 1, 1; if ($y < $kPractise + $kLines) { print "PRACTICE"; } else { print ++$score; } # keep speeding up, reducing pause time select (undef, undef, undef, $wait); #pauses here $wait = .25 if $score eq 50; $wait = .2 if $score eq 100; $wait = .15 if $score eq 150; $wait = .1 if $score eq 200; } } sub newLine { my ($width) = @_; my $line = ' ' x $width; substr $line, rand $width, 1, 'T' for 1 .. rand $kMaxTrees; return $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

      Add a declaration for @xloc

      cls; my @xloc; my $col= 0;

      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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-19 22:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found