in reply to Returning the value generated in package/module problem

This node falls below the community's minimum standard of quality and will not be displayed.
  • Comment on Re: Returning the value generated in package/module problem

Replies are listed 'Best First'.
Re^2: Returning the value generated in package/module problem
by planetscape (Chancellor) on Oct 12, 2010 at 23:55 UTC

    My perl is tasty fresh. Thank you for your sagacious comment.

    I do not need a reresher. I need someone to point out the obvious mixup in my very simple code that will not return a value for some reason.

    I do NOT need a pointer to the 4 tutorials i have already read last night before posting here.

    I do NOT need a sub example : I can do subs just fine. the question is why is the package not returning the value.

    I do NOT need Someone to rewrite my code in 2 lines.

    And of course we knew all this because you mentioned it in your OP...

    What? No? Not there? Oh, yes, then our ESP, that would have told us...

    Oh, dear, Acme::ESP is offline? Oh noes!

    Seriously, please do not chastise those who volunteer their time to help you, and who rely solely on the information you are generous enough to provide in order to do so. Thank you.

    HTH,

    planetscape
Re^2: Returning the value generated in package/module problem
by Anonymous Monk on Oct 12, 2010 at 12:08 UTC
    Can anyone please help me return the value in this code or simply post a package example that can return a value

    Packages, as a rule, do not return values (yes, modules must return a true value for require/use to work). Functions return values.

    Are you having trouble exporting/importing?

    use strict; use warnings; BEGIN { package Wahoo; require Exporter; use vars qw[ @ISA @EXPORT_OK $WA $HOO ]; @ISA = qw[ Exporter ]; @EXPORT_OK = qw[ $WA $HOO ]; $WA = $HOO = 0; sub Wahoo { $WA = $HOO = 4; } $INC{'Wahoo.pm'} = __FILE__; } package main; use Wahoo qw[ $WA $HOO ]; warn "$Wahoo::WA $Wahoo::HOO "; warn "$WA $HOO "; Wahoo::Wahoo(); warn "$Wahoo::WA $Wahoo::HOO "; warn "$WA $HOO "; __END__ 0 0 at - line 16. 0 0 at - line 17. 4 4 at - line 19. 4 4 at - line 20.
    Use strict warnings and diagnostics
Re^2: Returning the value generated in package/module problem
by Marshall (Canon) on Oct 12, 2010 at 12:29 UTC
    If you are trying to export a variable from your package, it needs to be declared as a "our" variable in that package to give it package scope, then you can export it.
    use strict; #update had left this off before use warnings; package MY_package; use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION); use Exporter; our $VERSION=1.0; our @ISA = qw(Exporter); our @EXPORT = qw($c_freq ); our $c_freq = 2; #now $c_freq is visible as a global
    $Price_calc::amastringsentback, amastringsentback needs to be an our $amastringsentback. In this case you use the fully qualified name, so $amastringsentback wouldn't need to be exported, but it still does have to be an "our" variable (not a my variable) to get in the symbol table.

    Update: as Anonymous Monk says undeclared var may be ok. I just never write code without strict and warnings - so didn't consider that point.

      If you are trying to export a variable from your package, it needs to be declared as a "our" variable in that package to give it package scope, then you can export it.

      Not if you don't use strict/warnings, undeclared variables are global variables by default