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

Greetings fellow perl coders

I am tring to write a package module to make my code easier but i cannot seem to return the value generated. Anyone know what I am missing? It has got to be something simple i missed

use LWP::Simple; use LWP::Simple qw( $ua get ); use LWP::UserAgent; use HTTP::Request::Common qw(POST); use Price_calc; use Price_calc qw($amastringsentback1 $amastringsentback2 @amaglobal $ +amastringsentback); Price_calc::get_ama($amastringsent); # And then some attempted trys to + get value returned by Price_calc::get_ama $amastringsentback1=$Price_calc::amastringsentback; $amastringsentback2=shift; $amastringsentback3=@amagotback[0]; print "DATA: $amastringsentback - $amastringsentback1 - @amaglobal - +$amastringsentback3 - $Price_calc::amastringsentback" ; # everything just prints as empty ---------------------------------- package Price_calc; use LWP::Simple; use LWP::Simple qw( $ua get ); use LWP::UserAgent; use HTTP::Request::Common qw(POST); use Exporter; @ISA = qw(Exporter); @EXPORT_OK = qw($amastringsentback1 $amastringsentback2 @amaglobal $am +astringsentback); sub get_ama { do stuff ## THE CODE WORKS FINE JUST WONT REUTRN VALUES. @amagotback[0]=$amastringsentback; return $amastringsentback; }

Replies are listed 'Best First'.
Re: Returning the value generated in package/module problem
by kcott (Archbishop) on Oct 12, 2010 at 11:39 UTC

    You've got use Price_calc twice. Probably don't want the first one.

    You're not capturing the return value of Price_calc::get_ama($amastringsent);.

    The last line of your package Price_calc; should be 1; (actually, it can be any true value but most people just use a one).

    -- Ken

      Thank you kcott and Anonymous Monk.

      Those are helpfull tips. I DId try

      1 pricecalc

      and

      $amastringsentback9=Price_calc::get_ama($amastringsent);

      $amastringsentback9 comes up with an empty string however code in Price_calc works fine;

      ( I forget to include the 1; in the post but it is is in there)

Re: Returning the value generated in package/module problem
by cdarke (Prior) on Oct 12, 2010 at 12:38 UTC
    If you
    use strict; use warnings;
    you would have spotted most of the issues yourself.
    $amastringsentback3=@amagotback[0];
    would be better written as:
    $amastringsentback3=$amagotback[0];
    but that still would not work, because the array @amagotback is not exported by the module, so it would use a package variable in main.

    The following:
    use warnings; use strict; use LWP::Simple; use LWP::Simple qw( $ua get ); use LWP::UserAgent; use HTTP::Request::Common qw(POST); #use Price_calc; use Price_calc qw(@amagotback $amastringsentback1 $amastringsentback2 +@amaglobal $amastringsentback); my $amastringsent = 'hello module'; Price_calc::get_ama($amastringsent); # And then some attempted trys to + get value returned by Price_calc::get_ama $amastringsentback1=$Price_calc::amastringsentback; $amastringsentback2=shift; #$amastringsentback3=@amagotback[0]; my $amastringsentback3=$amagotback[0]; print "DATA: $amastringsentback - $amastringsentback1 - @amaglobal - +$amastringsentback3 - $Price_calc::amastringsentback" ; ----------------------------------- package Price_calc; use LWP::Simple; use LWP::Simple qw( $ua get ); use LWP::UserAgent; use HTTP::Request::Common qw(POST); use strict; use warnings; use Exporter; our @ISA = qw(Exporter); our (@amagotback, $amastringsentback1, $amastringsentback2, @amaglobal +, $amastringsentback); our @EXPORT_OK = qw(@amagotback $amastringsentback1 $amastringsentback +2 @amaglobal $amastringsentback); sub get_ama { #do stuff ## THE CODE WORKS FINE JUST WONT REUTRN VALUES. $amastringsentback = 42; #@amagotback[0]=$amastringsentback; $amagotback[0]=$amastringsentback; return $amastringsentback; } 1;
    prints:
    DATA: 42 - 42 - - 42 - 42
Re: Returning the value generated in package/module problem
by Anonymous Monk on Oct 12, 2010 at 11:35 UTC
    You need a refresher
    sub Something::something { return 0, 1,2,3,4, 5; } warn Something::something(); warn scalar Something::something(); warn "say ", ( Something::something() )[0,3]; my @stuff = Something::something(); warn "say @stuff "; __END__ 012345 at - line 4. 5 at - line 5. say 03 at - line 6. say 0 1 2 3 4 5 at - line 8.
A reply falls below the community's threshold of quality. You may see it by logging in.