First of all, don't use $a and $b. They're a bit special because of their use by sort.
Secondly, @_ == 1 checks the number of arguments provided by the caller. This probably not what you wanted to check. You probably wanted to check the value of the first argument: $_[0] == 1.
The issue is that you are creating a new lexically-scoped variable, assign a value to it, then immediately leave the scope. Your variable is destroyed as soon as you create it! Declare a single variable in the outermost scope where it's needed.
my $y;
sub rout {
my ($x) = @_;
if ($x == 1) {
$y = 3;
} else {
$y = 5;
}
}
my $x = 1;
rout($x);
print "$y\n";
That said, returning a value would make more sense here.
sub rout {
my ($x) = @_;
if ($x == 1) {
return 3;
} else {
return 5;
}
}
my $x = 1;
my $y = rout($x);
print "$y\n";