L
Louis.
Hi,
The below script will print "var is apple". I find this weird, because
in textbooks you are taught to pass variable to subroutines as
parameters. What is the point of doing this, if the variable is
visible anyway?
How can I make the variable exist in main and yet be invisible to the
subroutine? I know I can create another variable with "my" and with
the same name in the subroutine. This will block the subroutine from
seeing variable in main, but is this really the way to do it, it seems
very cumbersom.
TIA,
Louis.
my $var = "apple";
routine();
sub routine {
print "var is $var.\n";
}
Solution???
{
my $var = "apple";
routine($var);
}
sub routine {
my $var = $_[0];
print "var is $var.\n";
}
The below script will print "var is apple". I find this weird, because
in textbooks you are taught to pass variable to subroutines as
parameters. What is the point of doing this, if the variable is
visible anyway?
How can I make the variable exist in main and yet be invisible to the
subroutine? I know I can create another variable with "my" and with
the same name in the subroutine. This will block the subroutine from
seeing variable in main, but is this really the way to do it, it seems
very cumbersom.
TIA,
Louis.
my $var = "apple";
routine();
sub routine {
print "var is $var.\n";
}
Solution???
{
my $var = "apple";
routine($var);
}
sub routine {
my $var = $_[0];
print "var is $var.\n";
}