BEGIN blocks in Perl -


Searching the Internet, I think the BEGIN block will be evaluated and executed in the compiling phase. But can @inc or other variables be inherited?

Below I've written the test for the directory structure as follows:

  | - alexpackages | | - alex1.pm | `- alex2.pm | - foo.pl` - main.pl   

for each file:

cat Alex package / alex1.pm

  package alex1; Sub foo () {print "this is alex1 :: foo \ n"; } 1;   

cat Alex package / alex2.pm

  package alex2; Sub foo2 () {print "this is islex2 :: foo2 \ n"; } 1;   

cat foo.pl

  alex1 :: foo ();   

cat main.pl

  BEGIN {push (@inc, '~ / programmer / perl / alex package '); } Use strict; Use warnings; Use Alex 1; Use Alex 2; # Alex1 :: foo (); # 1. This works fine system ("perl foo.pl"); # 2. This fails   

The way my program indicates that @inc does not work for a new system call. I think the system call system environment will not be inherited, am I right?

And can I create environment variables in the following system calls?

processes system by Variable, but the process of @INC is only a global global variable, not a system environment variable that is not visible outside of the Perl program.

Some notes on your code

  • Package names should be globally, they should be capitalized, so your package should be Alex 1 And Alex 2 , in the file alexpackages / Alex1.pm and alexpackages / Alex2.pm

  • lib is best to manipulate pragma by @INC , so

      use Lib '~ / programs / perl / The best way to use a prototype is to paral sub-routine, so  subfoo ()  should be just  subfoo    
  • You can like to use to copy the calling code in a package's symbols. By the way, when you call it, you do not need to fully qualify your suburban name, like foo () instead of Alex1 :: foo () < / P>

    The code will look like

    main.pl

      Strictly use; Use warnings; Use Lib '~ / programs / perl / alexpackages'; Alex 1; foo ();   

    ~ / programs / perl / alexpackages / Alex 1.pm

      package Alex 1; Strict use; Use warnings; Use the base 'exporter'; Our @EXPORT = qw / foo /; Sub foo {print} "this is Alex 1 :: fu \ n"; } 1;    

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -