Thanks Andy.

there is another member on this list pointed me a more graceful way,

use lib '/path/..';

Thus I don't need to remember special variables like @INC etc.

Thanks anyway for kind helps.


On 2018/7/27 星期五 AM 1:21, Andy Bach wrote:
$ ls test/
mytest.pm <http://mytest.pm>


> I try to include it but got failed.

$ perl -le 'use mytest'
Can't locate mytest.pm <http://mytest.pm> in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at -e line 1.

Right - notice the current path to mytest.pm <http://mytest.pm> is ./test/mytest.pm <http://mytest.pm> and @INC doesn't have ./test in it (just "." - the current dir)

> It provide the info losing something in @INC.

> Thus I do:

$ perl -le 'push @INC,"./test"; use mytest'
Can't locate mytest.pm <http://mytest.pm> in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at -e line 1.

> Still got failed.

Right - "use" (vs "require") is a "compile time" (interpolation but ...) action, so it happens before any code is actually executed. The "push" happens at execution time. It doesn't matter where in the code the "use" stmt is, it happens first.  See perldoc -f use               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.
vs
perldoc -f require

> Someone pointed to me that I should push @INC in BEGIN{}.

$ perl -le 'BEGIN {push @INC,"./test"} use mytest'

Just so - the BEGIN block also happens at compile time - it is for just such an activity; doing some perl *before* the execution phase.  Likewise the END block
is for doing perl after the program is done. So:
# cat test/mytest.pm <http://mytest.pm>
package mytest;
sub hello
{
  print "hello\n"
}

1;

# cat test/mytest.pm <http://mytest.pm>
package mytest;
sub hello
{
  print "hello\n"
}

1;

$ perl -e 'BEGIN {push  @INC, "test";} use mytest; mytest::hello()'
hello

or, as Perl treats the "::" as a path denoter:
$ perl -e 'use test::mytest; mytest::hello()'
hello

or:
$ perl -mtest::mytest -e 'mytest::hello()'
hello

find ./test/mytest.pm <http://mytest.pm> via @INC's "."


On Wed, Jul 25, 2018 at 9:36 PM, Lauren C. <lau...@miscnote.net <mailto:lau...@miscnote.net>> wrote:

    Hi Morning,

    I have a package file in "./test" dir.

    $ ls test/
    mytest.pm <http://mytest.pm>


    I try to include it but got failed.

    $ perl -le 'use mytest'
    Can't locate mytest.pm <http://mytest.pm> in @INC (@INC contains:
    /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2
    /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14
    /usr/share/perl/5.14 /usr/local/lib/site_perl .) at -e line 1.


    It provide the info losing something in @INC.

    Thus I do:

    $ perl -le 'push @INC,"./test"; use mytest'
    Can't locate mytest.pm <http://mytest.pm> in @INC (@INC contains:
    /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2
    /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14
    /usr/share/perl/5.14 /usr/local/lib/site_perl .) at -e line 1.


    Still got failed.

    Someone pointed to me that I should push @INC in BEGIN{}.

    $ perl -le 'BEGIN {push @INC,"./test"} use mytest'

    This works.

    So my question is what happens in BEGIN{} block?

    thanks.

-- To unsubscribe, e-mail: beginners-unsubscr...@perl.org
    <mailto:beginners-unsubscr...@perl.org>
    For additional commands, e-mail: beginners-h...@perl.org
    <mailto:beginners-h...@perl.org>
    http://learn.perl.org/





--

a

Andy Bach,
afb...@gmail.com <mailto:afb...@gmail.com>
608 658-1890 cell
608 261-5738 wk

Reply via email to