Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Passing value to a Perl module

by Anonymous Monk
on Jan 10, 2018 at 15:51 UTC ( #1207065=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I need to pass a value to a module from a script to do some checks.
I am trying this way, but I am getting this error:
Undefined subroutine &main::hello called at code.pl line 8.
Any suggestions?

script.pl:
use strict; use warnings; use Foo; hello('test');

Foo.pm
package Foo; use strict; use warnings; use base 'Exporter'; our @EXPORT_OK = qw( hello ); my $hello = hello(); sub hello { my $h = shift; return; } print $hello; 1;
Thanks for looking!

Replies are listed 'Best First'.
Re: Passing value to a Perl module
by hippo (Abbot) on Jan 10, 2018 at 15:59 UTC

    You have not imported hello into script.pl. Change your use statement to:

    use Foo qw/hello/;

    and try again. See Exporter for examples.

      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

        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().

        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');

Re: Passing value to a Perl module
by thanos1983 (Vicar) on Jan 10, 2018 at 16:07 UTC

    Hello Anonymous Monk,

    See sample bellow:

    #!/usr/bin/perl use strict; use warnings; use feature 'say'; =alternative BEGIN { push ( @INC,"/your/dir/path"); } =cut use lib qw(/your/dir/path); use Foo::Bar qw(hello echoTest); # update adding echoTest say hello(); say echoTest('Expecting Echo!'); # update adding echoTest subroutine __DATA__ $ perl test.pl Hello! Expecting Echo! # update adding echoTest stdout

    Sample of the module, in my dir /my/dir/script I have my module Foo::Bar or Foo/Bar.pm.:

    package Foo::Bar; use strict; use warnings; use Exporter qw(import); our @EXPORT_OK = qw(hello echoTest); # update adding echoTest sub hello { return 'Hello!'; } # update adding echoTest sub sub echoTest { return shift; } 1;

    Update: I just noticed that you are wondering how you pass a value to the module. I have updated the code by adding a new subroutine that returns the input string. I hope this is what you are looking for. If not update your question so we can provide more information.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Passing value to a Perl module
by karlgoethebier (Monsignor) on Jan 10, 2018 at 17:11 UTC

    Deluxe edition:

    Please see Class::Tiny for further variations.

    Update: Fixed link to wrong module.

    Update 2: A fellow monk asked for some more flexibility:

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1207065]
Front-paged by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2018-05-24 02:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?