$ ls test/
mytest.pm

> I try to include it but got failed.

$ perl -le 'use mytest'
Can't locate 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 is ./test/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 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
package mytest;
sub hello
{
  print "hello\n"
}

1;

# cat test/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 via @INC's "."


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

> Hi Morning,
>
> I have a package file in "./test" dir.
>
> $ ls test/
> mytest.pm
>
>
> I try to include it but got failed.
>
> $ perl -le 'use mytest'
> Can't locate 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 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
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


-- 

a

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

Reply via email to