in reply to Re: Passing value to a Perl module
in thread Passing value to a Perl module

Yes I forgot to import the "hello" sub. Done that, I still can not print the variable "$hello" in the module, not in the script.
Use of uninitialized value $hello in print at Foo.pm

Replies are listed 'Best First'.
Re^3: Passing value to a Perl module
by hippo (Archbishop) on Jan 10, 2018 at 16:21 UTC

    Indeed. Your module says:

    my $hello = hello(); sub hello { my $h = shift; return; } print $hello;

    So $hello is given the return value of hello() which is undef. You probably want to return $h instead.

    PS. I would strongly advise naming your variables and your subroutines with different names, otherwise it's going to get very confusing very quickly.

    PPS. and as stevieb quite rightly says, pass an argument when you call hello().

Re^3: Passing value to a Perl module
by stevieb (Canon) on Jan 10, 2018 at 16:22 UTC

    Well, when you initialize the module by loading it, this executes:

    my $hello = hello();

    Then later, you try to print $hello. It's going to be uninitialized, because you're not sending a value into hello() in that call. Try: my $hello = hello('blah');

      I see that, I know that, but I am trying to pass the value from the script.pl to the module.pm
      hello('test'); has been called by the script.pl
      I can fix the uninitialized by using this in Foo.pm
       my $hello = hello() || '';
      But how can I send the value from the script.pl to the Foo.pm?
        ... I am trying to pass the value from the script.pl to the module.pm ... how can I send the value from the script.pl to the Foo.pm?

        You can't, at least not with the OPed code. The private lexical (my) scalar variable  $hello in Foo.pm in the OPed code is assigned a value once and only once and print-ed once and only once, both events occurring during module inclusion. The  $hello scalar in the OPed Foo.pm has no getter or setter (accessor/mutator) function defined for it that would allow it ever again to be accessed or given another value; it is a private module variable that is not "exposed". Here's a variation of the OPed code with a getter (but still no setter!) for the lexical variable:

        # Foo.pm package Foo; use warnings; use strict; use parent 'Exporter'; our @EXPORT_OK = qw(hello get_hello); my $hello = hello('evaluated during module inclusion'); sub hello { my $h = shift; return $h; } sub get_hello { return $hello; } print 'from package ', __PACKAGE__, " during use: '$hello' \n"; 1;
        and:
        # use_foo_1.pl use warnings; use strict; use Foo qw(hello get_hello); hello('test'); print 'from package ', __PACKAGE__, " during execution: '", get_hello, + "' \n";
        Output:
        c:\@Work\Perl\monks\Anonymous Monk\1207065>perl use_foo_1.pl from package Foo during use: 'evaluated during module inclusion' from package main during execution: 'evaluated during module inclusion +'

        Update: Here's another example with both a getter and setter defined for the  $hello private module variable. Both the module and it's use-ing script have been changed.


        Give a man a fish:  <%-{-{-{-<

        This works:
        Foo.pm:
        package Foo; use strict; use warnings; use base 'Exporter'; our $hello = \&hellosub; our @EXPORT_OK = ( '$hello'); sub hellosub { my $h = shift; return $h; } print $hello->("Foo Mod init"),"\n"; 1;
        test caller code:
        use strict; use warnings; BEGIN { push ( @INC,"."); } use Foo ('$hello'); print $hello->('test'),"\n";

                        Is a computer language with goto's totally Wirth-less?