begood321 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys,

I can't seem to get my value to pass from subroutine to subroutine call as seen below. I get values in logger reports1 but not in logger reports2.

@reports = &getValidTotals(); $logger->write_log("reports2: @reports", "INFO");. . . . . sub getValidTotals { my ($reportGroup, $total) = @_; my @reports = qw(); if ($reportGroup ne "") { push(@reports,$reportGroup); $logger->write_log("reports1: @reports, total: $total", "INFO"); } return (@reports); }

Hi Ken,

I have use warnings in my code and didn't get warning. I have been trying to use correct syntax. What's the correct syntax.

I tried code below but didn't get array contents

@reports = &getValidTotals($reportGroup, $total);

Replies are listed 'Best First'.
Re: Return array not passed to sub call
by kcott (Archbishop) on Nov 19, 2010 at 04:24 UTC

    Look at the arguments you're passing to getValidTotals() and then look at the arguments you're expecting in that routine.

    If you had use warnings; in your code, you would have seen:

    Use of uninitialized value $reportGroup in string ne at ...

    -- Ken

Re: Return array not passed to sub call
by kcott (Archbishop) on Nov 19, 2010 at 09:05 UTC

    You should reply to a post as opposed to updating your original post with the reply - I only saw your response by accident.

    While it's not incorrect syntax, you should call your subroutines without a leading ampersand (&) - see perlsub for details.

    The warnings pragma is lexically scoped. Perhaps you have it hidden in a block somewhere - you haven't shown it so there's no way to tell if you've used the correct syntax. The documentation will tell you. You haven't shown use of the strict pragma either.

    You're using two @reports arrays. Is that causing you some confusion? Is that what you intended? The first one might be global but you haven't shown enough code for me to tell.

    You could test the values of variables with a simple print statement to see if what you think you're passing around is actually happening.

    You need to show your output. It doesn't matter if it's going to the screen, saved in a log file or reported in some other fashion - you need to include it with your problem description.

    Your code would be a lot easier to read if you used indentation - see perlstyle for some pointers in this regard.

    Have you read How do I post a question effectively? There's a link to this page about 5 lines under the Preview button. It's there whenever you want to post a node. Please familiarise yourself with its contents.

    -- Ken

      I just copied relevant code where I have issues. I am using correct syntax because I have no errors using -wc along with use : warnings, diagnostics, strict in my code. I'm trying to pass the same value from variable reports1 to reports2.

      This is output I get:

      Fri Nov 19 00:29:35 2010 : INFO : expiration_report.ipl : reports1: /iwmnt/default/main/internet/WORKAREA/WCM/expiration_reports/ts_groups/corp_gov/corp_gov_invalid_expiry_date.csv, total: 1

      Fri Nov 19 00:29:35 2010 : INFO : expiration_report.ipl : reports2:

      Fri Nov 19 00:29:35 2010 : INFO : expiration_report.ipl : Reports Created on webcmsib

        Hi Ken,

        I'm using 1 @reports array. I simply want to pass array from sub return to sub call so I can access array values from other parts of code below sub call but above sub return. Hope you understand what I'm trying to achieve. Thanks for help.