By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,159 Members | 1,941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,159 IT Pros & Developers. It's quick & easy.

Subroutine Parameters and Return Values

P: 8
I'm using a "package" type subroutine, called test_package.pl. I'm calling it from a script called split0.pl. I want to pass the $0 variable, use the subroutine to split out just the filename, and pass that filename value back to the calling script. The subroutine does the processing correctly (I've verified with print statements), but I'm having trouble getting the value passed back to the calling script.

Here's the subroutine:

Expand|Select|Wrap|Line Numbers
  1. package test_package;
  2.  
  3. sub subroutine1 {
  4.     @script = @_;
  5.     @script = split /\\/,$script[0];
  6. }
  7. return 1;
  8.  
Here's the call from the calling script:

Expand|Select|Wrap|Line Numbers
  1. require 'test_package.pl';
  2. #
  3. # call external subroutine!
  4. $full_name = $0;
  5. test_package::subroutine1($full_name);
  6. #
  7.  
I'm pretty new to all this, so I'm assuming I'm overlooking something simple. I've looked at some Perl books, but haven't figured it out yet. I'd appreciate any help.
May 6 '07 #1
Share this Question
Share on Google+
1 Reply


KevinADC
Expert 2.5K+
P: 4,059
you are missing something. You have not assigned the return value of the subroutine/function to a variable:

Expand|Select|Wrap|Line Numbers
  1. my $var = your_function($somevalue);
you should also use an explicit return() in your function and not depend on perl to return the correct data from a sub routine


Expand|Select|Wrap|Line Numbers
  1. sub my_sub {
  2.    my ($foo) = @_;
  3.    my $blah = do something with $foo;
  4.    return($blah);
you should also start using "strict" and "warnings" in all your perl scripts for many very good reasons.
May 6 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.