0

When I pass a variable through a couple of subs, it always turns up empty. Why is this ?

sub Main {

    my $myVariable = "Test string";
    firstSub($myVariable);
}


sub firstSub {

    my($myVariable) = @_;
    my @array = `some command`;
    secondSub(@array, $myVariable);
}

sub secondSub {

    my(@array, $myVariable) = @_;
    print $myVariable; 
}

echo will be undef.

2
  • echo is no perl function. ALways add use strict; and use warnings; to your scripts. Commented Oct 19, 2012 at 7:46
  • Why did you edit the code by adding @array? Your first code was correctly passing the variables into the function. Now you have confused the issue by adding another problem after your original problem was answered. Commented Oct 19, 2012 at 7:52

4 Answers 4

4

echo is not a valid Perl function. You're confusing shells with Perl here. Try "print" or "say" (the latter with Perl 5.10 and newer).

Also you cannot assign an array & a scalar variable to another array & scalar variable. Meaning this won't work because all of the elements of the right-hand side will be assigned to the array on left-hand side, and nothing will be assigned to the scalar: my (@array, $myVariable) = @_; Either swap the order of the elements my ($myVariable, @array) = @_; (also when calling the function) or use array references instead of full arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I'm sorry, I wrote this by hand just now to give an example. I'm using print in my code.
2

Your code doesn't do anything because you have defined three subroutines, but you have never called them.

Just add Main(); to actually run your main sub.

Also, you need print instead of echo.

Also, the passing of variables is incorrect, as Moritz Bunkus explained.

2 Comments

This is just something I wrote down to give an example. I have the Main() function in my code. This runs fine; if I change print $myVariable to print "Some text", it will show up.
@user1758367, that wasn't clear from the example code that you posted. The original code didn't have any errors other than no call to Main and using echo instead of print. I assumed that you must have never called Main, because otherwise you would get an error that echo is not a function (the issue has now been confused by edits to your question). The moral of the story is, always post a complete code example with your question, and always test your own example before you post it.
1

When you call secondsub() the @array and the $myVariable is being sent as a list(a single element) and is been assigned to @array in the secondsub function. You can see both the @array and $myVariable values when you print @array in secondsub.

You have to pass the array as a reference and receive it as a scalar value in secondsub. The below code will work.

&Main();
sub Main {

    my $myVariable = "Test string";
    firstSub($myVariable);
}


sub firstSub {

    my($myVariable) = @_;
    my @array = `some command`;
    secondSub(\@array,$myVariable);


}

sub secondSub {

    my($ref,$myVariable) = @_;
   print $myVariable;
}

Comments

-1

Passing varibles:

  my $txt = "this text for sample";

Function_Passing_varible($txt);

sub Function_Passing_varible{
                my $text = shift;
                print $text;

}

I think you like this answer......

1 Comment

You should probably explain in a little further.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.