sirpots has asked for the wisdom of the Perl Monks concerning the following question:

Hello Everyone! I am trying to write a basic perl program that will compare two numbers and display the biggest and the smallest. but I keep on getting this error with my code Error: "my" variable $a masks earlier declaration in same scope at bigsmall.pl line 8 (#1) Code:
#!/usr/bin/perl use warnings; use strict; use diagnostics; my ($a, $b, $largest, $smallest); print "Enter your First Number:\n" chomp ($a = <>); $largest = $a; $smallest = $a; print "Enter your Second Number:\n" chomp ($b = <>); if ($a > $b); { $largest=$a; $smallest=$b; } else { $largest=$b; $smallest=$a; } print "The Biggest number is $largest and the smallest number is $smal +lest\n";
What is going wrong? Sorry I am new to perl!
  • Comment on I Keep getting the error: "my" variable $a masks earlier declaration in same scope at bigsmall.pl line 8 (#1)
  • Download Code

Replies are listed 'Best First'.
Re: I Keep getting the error: "my" variable $a masks earlier declaration in same scope at bigsmall.pl line 8 (#1)
by CountZero (Bishop) on Oct 28, 2011 at 21:01 UTC
    $a and $b are pre-declared variables in the main package, so anytime you try to initialize the lexical variables my $a or my $b Perl warns you that you cannot access the original $a and $b in this scope.

    $a and $b are used with the sort function.

    Rather than using an if test to find the largest and the smallest value, you could have used the sort function:

    my ($smallest, $largest) = sort {$a <=> $b} ($first, $second);
    (I have replaced your $a and $b by $first and $second.)

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: I Keep getting the error: "my" variable $a masks earlier declaration in same scope at bigsmall.pl line 8 (#1)
by toolic (Bishop) on Oct 28, 2011 at 20:10 UTC
    I could not reproduce your error with your posted code. I got other compile errors which I fixed by adding/removing semicolons (see my "semi" comments). This code compiles and runs for me:
    use warnings; use strict; use diagnostics; my ($a, $b, $largest, $smallest); print "Enter your First Number:\n"; # semi chomp ($a = <>); $largest = $a; $smallest = $a; print "Enter your Second Number:\n"; # semi chomp ($b = <>); if ($a > $b) # semi { $largest=$a; $smallest=$b; } else { $largest=$b; $smallest=$a; } print "The Biggest number is $largest and the smallest number is $smal +lest\n";
    It is important to note that $a and $b are special variables to Perl.
Re: I Keep getting the error: "my" variable $a masks earlier declaration in same scope at bigsmall.pl line 8 (#1)
by johnny_carlos (Scribe) on Oct 28, 2011 at 20:26 UTC
Re: I Keep getting the error: "my" variable $a masks earlier declaration in same scope at bigsmall.pl line 8 (#1)
by Marshall (Canon) on Oct 29, 2011 at 09:47 UTC
    As others have pointed out $a and $b have special meaning because they are used by sort.
    After getting tripped up by this a few times, you will learn to use $x,$y or something else other than $a and $b!

    The two statements that delete leading and trailing spaces are Perl idioms. You will see them often. By convention, any leading or trailing spaces from user input should be ignored. "space characters", matching "\s" are: space,\n\r\t\f

    Also in Perl, you do not need to use an intermediate variable to swap two variables this is re-ordering of a list.

    Also, in Perl you can write the "if" at the end of the line if it is a "single action" if, a one statement if clause.

    #!/usr/bin/perl use warnings; use strict; use diagnostics; print "Enter your First Number:\n"; my $first = <>; $first =~ s/^\s*//; #no leading spaces $first =~ s/\s*$//; #no trailing spaces (also "zapps" \n) print "Enter your Second Number:\n"; my $second = <>; $second =~ s/^\s*//; $second =~ s/\s*$//; ($first,$second) = ($second,$first) if $second < $first; print "The Biggest number is $second and the smallest number is $first +\n";