Subroutine Parameters and Return Values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • peterv6
    New Member
    • May 2007
    • 8

    Subroutine Parameters and Return Values

    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:

    Code:
    package test_package;
    
    sub subroutine1 {
    	@script = @_;
    	@script = split /\\/,$script[0];
    }
    return 1;
    Here's the call from the calling script:

    Code:
    require 'test_package.pl';
    #
    # call external subroutine!
    $full_name = $0;
    test_package::subroutine1($full_name);
    #
    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.
    Last edited by miller; May 7 '07, 06:52 PM. Reason: Code tag
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    you are missing something. You have not assigned the return value of the subroutine/function to a variable:

    Code:
    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


    Code:
    sub my_sub {
       my ($foo) = @_;
       my $blah = do something with $foo;
       return($blah);
    }
    you should also start using "strict" and "warnings" in all your perl scripts for many very good reasons.

    Comment

    Working...