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

I have 2 modules vlog.pm and blib.pm and a script gen.pl. i have used the vlog.pm module in the gen.pl by 'use vlog'
and used blib.pm in the vlog.pm.
Now I want to call a sub routine defined in blib.pm in the script gen.pl how can i do this? Will @ISA help me and how?

Replies are listed 'Best First'.
Re: Calling subroutine from other module
by shmem (Chancellor) on Jul 12, 2007 at 11:24 UTC
    @ISA won't help for function calls, only for method calls. You can call functions in other packages by qualifying them, or by switching package temporarily; you could also export a function in a package and import it into your main script (see Exporter). @ISA only works for method lookup, so you need an object.

    Having the *.pm files myblib and vlog

    package myblib; sub foo { print +(caller(0))[3],"(@_)\n" } 1;
    package vlog; use myblib; sub bar { print +(caller(0))[3],"(@_)\n" } 1;
    here are various flavours of calls:
    # line 1 gen.pl use vlog; myblib::foo(1); myblib->foo(2); # foo(); # this would die - no foo() in main:: # bar(); # as would this package myblib; # temporarily switch package foo(3); package main; # switch back @ISA = qw(myblib); $a = bless do { \ my $x }, 'main'; $a->foo(4); # method lookup finds foo() in myblib:: # shoehorn the foo() function into main:: - that's roughly # what Exporter and 'use myblib qw(foo)' do *foo = *myblib::foo; foo(5); # now found in main:: $a->foo(6); __END__ myblib::foo(1) myblib::foo(myblib 2) myblib::foo(3) myblib::foo(main=SCALAR(0x818c688) 4) myblib::foo(5) myblib::foo(main=SCALAR(0x818c688) 6)

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Calling subroutine from other module
by Fletch (Bishop) on Jul 12, 2007 at 18:35 UTC

    Couple of points no one's mentioned yet: all lower case module names are reserved for use as Perl pragma (e.g. strict, warnings). Avoid using them and instead Capitalize the first letter of your modules.

    Which leads to the next point: Perl comes with a blib pragma that's used for loading an uninstalled module during installation and testing. It's possible that any strange behavior you're seeing (just speculating since the original problem doesn't mention this, but I'm getting a hint of XY problem none the less) is because you're picking up Perl's blib not your blib.pm. Proper module naming would avoid potential problems such as this.

    Update: Very valid point from the Anonomunk below. If you're not sure one can always check perlmodlib for a list.

      case insensitive operating systems won't distinguish between Blib.pm blib.pm, so for portability, avoid pragma names regardless of case.
Re: Calling subroutine from other module
by misc (Friar) on Jul 12, 2007 at 11:24 UTC
    I'd say it depends on what exactly you want to do.
    If you have an object oriented approach, it would be POSSIBLY the best to inherit vlog from blib, therefore you had to insert the code below into vlog.pm.(perltoot)
    BEGIN { @ISA = qw/blib/; }

    If you haven't got an object oriented approach, just use blib in gen.pl.
    hth.
Re: Calling subroutine from other module
by citromatik (Curate) on Jul 12, 2007 at 09:41 UTC

    why don't you include blib.pm in gen.pl the same way you included vlog.pm?

    citromatik