in reply to Re: Undefined subroutine - newbie problem
in thread Undefined subroutine - newbie problem

Thank You all for reply; so: 1. generate_throws_simple consists of:
sub generate_throws_simple { for(my $i = 0; $i < $main::b; $i++) { push(@main::wyniki, int(rand($main::a)+1)); } } 1;
2. modules::gts or rather just gts - wrong copy/paste, but gts module file is in main folder as well

3. UNFORTUNATELY, I need to use "require" instead of "use" in that exercise;

is it possible? if yes, how should I do that?

Replies are listed 'Best First'.
Re^3: Undefined subroutine - newbie problem
by tobyink (Canon) on Aug 14, 2012 at 12:26 UTC

    require Foo; import Foo; should have roughly the same effect as use Foo.

    Or better, BEGIN { require Foo; import Foo; } should have exactly the same effect as use Foo.

    But other than as a lesson in how use works under the hood, I'm not sure why any exercise would insist that you employ require rather than trusty old use.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      it seems that:
      require gts; import gts;
      which holds sub print_throws_simple still gives me: Undefined subroutine &main::generate_throws_simple called :(
Re^3: Undefined subroutine - newbie problem
by tobyink (Canon) on Aug 14, 2012 at 13:06 UTC

    Regarding point #2, there is a major difference between:

    package modules::gts;

    And:

    package gts;

    It's important that the package name on that package line, and the package name you call import on match exactly (and case-sensitively!)

    If you've still not gotten it working, please post the exact and complete source code of all three files, including their file names and paths.

    If you keep only providing partial information about your problem, the best we can do is guess.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      WONDERFUL! You have found the point ;)
      first of all by using "require" I need to use "import" as well, secondly I need to be more accurate and stop messing modules urls ;)

      One last thing - is there "prettier" way to write that code:
      require modules::gts; #generate_throws_simple require modules::pts; #print_throws_simple require modules::sts; #save_thorows_simple require modules::rt; #read_throws require modules::ss; #statistics_simple import modules::gts; #generate_throws_simple import modules::pts; #print_throws_simple import modules::sts; #save_thorows_simple import modules::rt; #read_throws import modules::ss; #statistics_simple

        Errr... yes, use!

        Or you could do something like this:

        use UNIVERSAL::require; for (qw(gts pts sts rt ss)) { my $mod = "modules::$_"; $mod->require and $mod->import; }

        Though I wouldn't especially recommend it.

        You do know, don't you, that a single module can export more than one sub?

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re^3: Undefined subroutine - newbie problem
by roboticus (Chancellor) on Aug 14, 2012 at 11:52 UTC

    blackhat:

    From perldoc -f use:

      use Module VERSION LIST
      use Module VERSION
      use Module LIST
      use Module
      use VERSION
           Imports some semantics into the current package from the named
           module, generally by aliasing certain subroutine or variable
           names into your package.  It is exactly equivalent to
    
             BEGIN { require Module; Module->import( LIST ); }
    
           except that Module must be a bareword.
    

    So you might try calling the import method for the package.

    Alternatively, you can fully-qualify the call to include the package name, like: modules::gts::generate_throws_simple().

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      well I don't really get that... my exc order says: "the program should also execute correctly when use statement is replaced with require";

      I thought, that it will be simple use > require swap + different variables reffs, but It seems much more complex for the moment

        blackhat:

        If you're trying to make it work the same way with use and require, try my second suggestion: use the fully qualified name when you call the function.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.