passing arguments to subroutines


R

Roman Rodriguez

Can anyone tell me if you can pass scalars and list to subroutines? if so,
what is the syntax? what am I doing wrong in the following code?
It seems the correct value is not returned because my subroutine doesn't
appear to be recieveing the parameter from the caller.


sub setMainMenuOption
{
my $SelectedOption = $_; #set $SelectedOption to value passed in
(from user)
if( $SelectedOption eq 's')
{
return("Sleep");
}
return("INVALID");
{
$UserInput = "S\n"; #simulate STDIN user input of the capital letter 'S'
print "\n\$UserInput: $UserInput";

chomp($UserInput);
print "\n\$UserInput after CHOMP: $UserInput";

$UserInput = lc($UserInput);
print "\n\$UserInput after lc(): $UserInput";

$Menu = &setMainMenuOption($UserInput);
print "\n\$Menu after return = $Menu";

exit;

results in the following output:

$UserInput: S
$UserInput after CHOMP: S
$UserUnput after lc(): s
$Menu after return = INVALID

when I step through this code I see that "$_" in the subroutine is not
properly recieving the arguement and therefore variable $SelectedOption is
remaining as "".
what is the proper way to pass arguments to a subroutine?

Thanks!
-RR



Thanks
-RR
 
Ad

Advertisements

J

Josef Moellers

Roman said:
Can anyone tell me if you can pass scalars and list to subroutines? if so,
what is the syntax? what am I doing wrong in the following code?
It seems the correct value is not returned because my subroutine doesn't
appear to be recieveing the parameter from the caller.


sub setMainMenuOption
{
my $SelectedOption = $_; #set $SelectedOption to value passed in
(from user)
if( $SelectedOption eq 's')
{
return("Sleep");
}
return("INVALID");
{
^
This should be a right curly!
$UserInput = "S\n"; #simulate STDIN user input of the capital letter 'S'
print "\n\$UserInput: $UserInput";

chomp($UserInput);
print "\n\$UserInput after CHOMP: $UserInput";

$UserInput = lc($UserInput);
print "\n\$UserInput after lc(): $UserInput";

$Menu = &setMainMenuOption($UserInput);
print "\n\$Menu after return = $Menu";

exit;

results in the following output:

No it does not!
$UserInput: S
There should be an empty line here
$UserInput after CHOMP: S
$UserUnput after lc(): s
$Menu after return = INVALID

Not that it matters in this case, because the answer to the question was
very obvious, but the incorrect reproduction of the source and the
results may confuse people who are willing to help!

Best to use copy-and-paste when posing code and results rather than
typing them in.
 
Ad

Advertisements

J

Jürgen Exner

Roman said:
Can anyone tell me if you can pass scalars and list to subroutines?
if so, what is the syntax?

perldoc perlsub
what am I doing wrong in the following
code?

perl is saying:
Missing right curly or square bracket at C:\tmp\t.pl line 24, at end of
line
That means your curly brackets are not balanced.

[code snipped]

jue
 

Top