in reply to Understanding variable scope in perl

I didn't get any problem when I ran it this way:
#!/usr/bin/perl -w + # Strict (always using these 2 lines will save you much grief!) use strict; use warnings; + # Global variables my @next_cell_arr; my @protein_matrix; my @sequence_1_arr; my @sequence_2_arr; my $row = 0; my $col = 0; my $dowhat = 0; + # Generate some test data $protein_matrix[$row][$col] = 1; $protein_matrix[$row][$col+1] = 2; $protein_matrix[$row+1][$col] = 3; $protein_matrix[$row+1][$col+1] = 4; + # BioBoy's code follows push( @next_cell_arr, $protein_matrix[$row][$col+1] ); push( @next_cell_arr, $protein_matrix[$row+1][$col+1] ); push( @next_cell_arr, $protein_matrix[$row+1][$col] ); + # - debug #$arr_len = scalar @next_cell_arr; #for( $var = 0; $var < $arr_len; $var++ ){ # print $next_cell_arr[$var]; # print "\n"; #} #print "\n\n"; #exit; # - debug + # - if all values are equal, then the sequence gets a match of suffers + a mismatch # (diagonal move), and not a gap penalty. This also saves me from hav +ing to # compute the max of the three cells if( ($next_cell_arr[0] == $next_cell_arr[1]) && ($next_cell_arr[1] == +$next_cell_arr[2]) ){ push( @sequence_2_arr, $protein_matrix[0][$col+1] ); push( @sequence_1_arr, $protein_matrix[$row+1][0] ); $dowhat = "diagonal"; }
But if you're getting an error on such-and-such a line, I'd like to suggest you try putting debugging right near that line, and breaking it up into multiple statements, so you can see the intermediate values of interest:
#TFD (Temporary for debug)>> my $cell0 = $next_cell_arr[0] || die "cell0 undef!\n"; printf "Cell0 = + %s\n", $cell0; my $cell1 = $next_cell_arr[1] || die "cell1 undef!\n"; printf "Cell1 = + %s\n", $cell1; my $cell2 = $next_cell_arr[2] || die "cell2 undef!\n"; printf "Cell2 = + %s\n", $cell2; if( ($cell0 == $cell1) && ($cell1 == $cell2) ){ print "TFD> All 3 cell values defined and EQUAL\n"; push( @sequence_2_arr, $protein_matrix[0][$col+1] ); push( @sequence_1_arr, $protein_matrix[$row+1][0] ); $dowhat = "diagonal"; } print "TFD> All 3 cell values defined, but NOT equal\n";
(Note that I use the notation "TFD" to mean that a line or a sequence of lines is "temporary for debugging", and can be removed once I uncovered the cause of the problem).

Replies are listed 'Best First'.
Re^2: Understanding variable scope in perl
by BioBoy (Novice) on Sep 29, 2005 at 18:11 UTC
    Thanks for your replys guys. Is it always a good idea to use 'my'? Also, what does use 'strict' allow you to do?
      This is a good node that will answer both of your questions. Use strict and warnings

      Here are my own takes on these subjects:

      Using "my" is generally a good idea. It sets the scope of the variable it's applied to to be localized instead of global. I'm sure others here can provide a much more detailed explanation of it. Basically, my recommendation is to use it whenever possible, except in times when you explicitly do not want to use it for a very good reason. "You have to know the rules in order to break them", that kind of thing.

      Regarding "strict", that's also a good thing to use whenever possible. Just like "my", only turn it off when you have a good reason to and you know exactly what you're doing. Strict forces you to write "better" code, in the sense of barewords, subs, variable scoping and the like. One of the things "strict" does is it forces you to scope all your variables using "my", "local", "our", or the package name.
      For an answer about what "strict" does, see the output of perldoc strict.

      Yes, I would say it's *almost* always a good idea to use "my".  It causes your variables to be lexically-scoped rather than globally-scoped, which can avoid many unintended bugs, and is considered an important programming practice.  (It's also one of the principles of object-oriented programming).

      If you get into the habit of using it religiously, along with "strict" and "warnings", you'll avoid many pitfalls on your path to Perl enlightenment.  I was lucky enough to be steered onto the path of using the "-w" switch, (eg. perl -w), from almost the first time I started using Perl (the -w switch has the same effect as "warnings", which go hand-in-hand with "strict"), and it's saved me from countless headaches!