Perl how does inc get set




















The use statement When perl encounters use My::Module; it goes over the elements of the built-in INC array that contains directory names. In each directory it checks if there is a subdirectory called "My" and if in that subdirectory there is a file called "Module. The first such file it encounters will be loaded into memory. If it does not find the file you get the above error messages. INC is defined when perl is compiled and it is embedded in the binary code.

You cannot change that, unless you recompile perl. Not something we would do every day. Luckily the INC array can be changed in several ways when we execute a script. We'll see these solutions and discuss when each one of them is appropriate to use. Every directory listed in this variable will be added to the beginning of INC. In the System Properties window, click on the Advanced tab. In the Advanced section, click the Environment Variables button.

Windows that you open after this will already know about the new variable. Regardless who and in what environment runs it. I saw several companies where use lib statements were added to the modules so they will be able to load their dependencies. If you need access to the data from outside the package, then you can return it from a subroutine, or you can create a global variable i.

The reference must be global so that you can refer to it by a fully qualified name. But just like in C, try to avoid having global variables. Using OO methods generally solves this problem by providing methods to get and set the desired value within the object that can be lexically scoped inside the package and passed by reference. This is perfectly legitimate and cannot be compared to the terrible mistake of taking a pointer to an automatic C variable and returning it from a function — when the pointer is dereferenced there will be a segmentation fault.

With use vars , you are making an entry in the symbol table, and you are telling the compiler that you are going to be referencing that entry without an explicit package name. The compiler figures out at compile time which my variables i.

References to such a variable are also global … only the value changes. Aside: that is what causes variable suicide. Any references to such a variable at runtime turn into unique anonymous variables on each scope exit. Whereas PATH contains a list of directories to search for executables, INC contains a list of directories from which Perl modules and libraries can be loaded. When you use , require or do a filename or a module, Perl gets a list of directories from the INC variable and searches them for the file it was requested to load.

If the file that you want to load is not located in one of the listed directories, then you have to tell Perl where to find the file. You can either provide a path relative to one of the directories in INC , or you can provide the full path to the file. Otherwise, the file is loaded into memory and an attempt is made to compile it. The key is the name of the file or module as it was passed to the one of the three functions we have just mentioned.

If it was found in any of the INC directories except ". Since strict. It does nothing, but returns a true value when loaded. Since the file was found relative to. Here we still get the relative path, since the module was found first relative to ". If we execute the same code from a different directory, then the ". We can also prepend the path with unshift , so it will be used for matching before ". These approaches to modifying INC can be labor intensive, since if you want to move the script around in the file-system, then you have to modify the path.

This can be painful, for example, when you move your scripts from development to a production server. If we move the script somewhere else… e. This has a slight overhead, because it will load from disk and recompile the FindBin module on each request. So it may not be worth it. If it has, then require just returns without doing a thing. Otherwise, an attempt will be made to load and compile the file.

If the argument is a full path to the file, then it just tries to read it. For example:. If the path is relative, then require will attempt to search for the file in all the directories listed in INC. If there is more than one occurrence of the file with the same name in the directories listed in INC , then the first occurrence will be used. The file must return TRUE as the last statement to indicate successful execution of any initialization code.

Since you never know what changes the file will go through in the future, you cannot be sure that the last statement will always return TRUE. Although you should use the real filename for most files, if the file is a module, then you may use the following convention instead:.

To prevent this, the require statement can be enclosed into an eval exception-handling block, as in this example:. Alexey Melezhik - Sep 13 ' DEV Community is a community of , amazing developers We're a place where coders share, stay up-to-date and grow their careers. Create account Log in. Twitter Facebook Github Instagram Twitch. Can't locate Hello. BEGIN failed--compilation aborted at tesinc. Upload image. Submit Preview Dismiss.

Docker and Dancer Dave Cross - May 19 ' Log in Create account.



0コメント

  • 1000 / 1000