Difference between use and require in Perl – use Vs require in perl

perl-use-require-difference

|| use Vs require in perl || What is the difference between use and require?

Except of course that use is evaluated at compile time where as require is evaluated at run time in other word, A use anywhere in the code will be evaluated when the code is run compiled, but require – import’s can only get evaluated when encoutered.

The differences are many and often subtle:

  1. use only expects a bareword, require can take a bareword or an expression
  2. use is evaluated at compile-time, require at run-time
  3. use implicitly calls the import method of the module being loaded, require does not
  4. use excepts arguments in addition to the bareword (to be passed to import), require does not
  5. use does not behave like a function (i.e can’t be called with parens, can’t be used in an expression, etc), whereas require does

do $file is like eval `cat $file`, except the former:
1.1: searches @INC and updates %INC.
1.2: bequeaths an *unrelated* lexical scope on the eval’ed code.

require $file is like do $file, except the former:
2.1: checks for redundant loading, skipping already loaded files.
2.2: raises an exception on failure to find, compile, or execute $file.

require Module is like require “Module.pm”, except the former:
3.1: translates each “::” into your system’s directory separator.
3.2: primes the parser to disambiguate class Module as an indirect object.

use Module is like require Module, except the former:
4.1: loads the module at compile time, not run-time.
4.2: imports symbols and semantics from that package to the current one.

Command to learn more about use and require
> perldoc -f require
> perldoc -f use

Tagged : / / / / / / / /

How to Check File Attributes in Perl ? Perl File Attributes explained

perl-file-attributes

Checking File Attributes in Perl

I have been using perl for quite some time now. I have also been using the file handling logic in my scripts. However, what I did not know till now is that you can quickly check for certain file properties in perl. Here is an example:
#!/usr/bin/perl
my (@description,$size);
if (-e $file)
{
push @description, ‘binary’ if (-B _);
push @description, ‘a socket’ if (-S _);
push @description, ‘a text file’ if (-T _);
push @description, ‘a block special file’ if (-b _);
push @description, ‘a character special file’ if (-c _);
push @description, ‘a directory’ if (-d _);
push @description, ‘executable’ if (-x _);
push @description, (($size = -s _)) ? “$size bytes” : ‘empty’;
print “$file is “, join(‘, ‘,@description),”\n”;
}
Nice, isn’t it?
Here is the complete list of features that you can check:

Operator Description
-A Age of file (at script startup) in days since modification.
-B Is it a binary file?
-C Age of file (at script startup) in days since modification.
-M Age of file (at script startup) in days since modification.
-O Is the file owned by the real user ID?
-R Is the file readable by the real user ID or real group?
-S Is the file a socket?
-T Is it a text file?
-W Is the file writable by the real user ID or real group?
-X Is the file executable by the real user ID or real group?
-b Is it a block special file?
-c Is it a character special file?
-d Is the file a directory?
-e Does the file exist?
-f Is it a plain file?
-g Does the file have the setgid bit set?
-k Does the file have the sticky bit set?
-l Is the file a symbolic link?
-o Is the file owned by the effective user ID?
-p Is the file a named pipe?
-r Is the file readable by the effective user or group ID?
-s Returns the size of the file, zero size = empty file.
-t Is the filehandle opened by a TTY (terminal)?
-u Does the file have the setuid bit set?
-w Is the file writable by the effective user or group ID?
-x Is the file executable by the effective user or group ID?
-z Is the file size zero?
Tagged : / / / / / / / / / / / / / / /