Perl helpfull Trics/Tips:
- Get all the Perl module installed via ubuntu/Linux command line
perl -MFile::Find=find -MFile::Spec::Functions -Tlw -e 'find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC'File::Find and File::Spec::Functions module are used to list all installed modules.
- -M option loads the module. It executes use module before executing the script
- -T option enables taint checking, which instructs perl to keep track of data from the user and avoid doing anything insecure with it. Here this option is used to avoid taking the current directory name from the @INC variable and listing the available .pm files from the directory recursively.
- -l option enables automatic line-ending processing in the output. Print statements will have the new line separator (\n) added at the end of each line.
- -w option prints any warning messages.
- -e option indicates that the following string is to be interpreted as a perl script (i.e., sequence of commands).
More simpler Way :
>find / -name \*.pm -type f
to know the pakage related to INC :
find `perl -e 'map {print "$_ "} @INC'` -name \*.pm -type f
>find / -name \*.pm -type f
to know the pakage related to INC :
find `perl -e 'map {print "$_ "} @INC'` -name \*.pm -type f
2. View Perl Documentation From Unix Command Line
You can use either perldoc or man command to get help about a particular perl module as shown below.
$ perldoc Regexp::Common (or) $ man Regexp::Common
If the perl document is not enough, use perldoc option -m, to view both source code and unformatted pod documentation of the specified perl module.
3.Verify Perl Module Is installed or not ?
Syntax: Perl -M"Module name"
if it gives not Output i.e indicated that the module is installed, else it show an error stating the module not installed.
4.The flip-flop operator is useful for skipping the first iteration when looping through the records (usually lines) returned by a file handle, without using a flag variable:
while(<$fh>)
{
next if 1..1; # skip first record
...
}
5.The null filehandle diamond operator
<>
has its place in building command line tools. It acts like <FH>
to read from a handle, except that it magically selects whichever is found first: command line filenames or STDIN. Taken from perlop:while (<>) {
... # code for each line
}
6.Map
The map built-in function is one of the most useful tools in your toolkit. map
takes a list and applies a code block to every element, returning the list. You can think of it as stream processing: you push the list in one side and get it back on the other side with some transformation applied. Inside the code block, you refer to the current element with the traditional "$_" variable.
Example below demonstrate the function for perl code using map to conver the Uppercase .
my @l = qw(perl php python);
my @uc_l = map { uc($_) } @l;
# Result: PERL PHP PYTHON
7. ||= operator
In Perl, you can use the||=
operator. Its precedence rules are such that it’ll only do an assignment if the value is false (‘false’ generally means zero, undefined, or the empty string):$a ||= 5;