Showing posts with label Perl Programming. Show all posts
Showing posts with label Perl Programming. Show all posts

Wednesday, March 30, 2011

LS: unix command for listing files and directories

ls is a command to list files and directories in Unix and unix like systems.

if it is invoked without any arguments then will list files and folders in current working directory in bare format. In this bare format its difficult to distinguish files and folders and other parameters.
FORMAT: ls [Options] [Filename]
Following are the most common options to be used with "ls".
-a :  Include all files even which are starting with "."(Hidden files in Unix ), by default they are ignored.
-l  : Long format, lists permissions in unix format, size, owner , date modified etc.
-F : Appends a character to reveal the type of file e.g. * for executables '/' for directory
-R : list recursively all the directories and files
-d : list directories instead of content
-t : sort by modification time
-h : Print size in human readable format like 1M, 4G ( Use with -l)

Example: 
For listing files and directories with their parameters like size permissions and all.
> ls -l 
For listing in as ls -l along with size in human readable format..
>ls -h -l

Note: ls can also be used in windows environment with cygwin installation.

Thursday, September 16, 2010

Perl System Routines: Functions for process

exec: abandon this program to run another

exit: exits from program


fork: creates new process just like this

getppid: get parent process ID

getpriority: returns current priority of the process

kill: sends a signal to a process or group of process

setpriority:

sleep:

syscall:

system:

times:

wait:

waitpid:

warn:

unlink in Perl: to delete the file

Unlink : this command permanently deletes a list of file from system.
Return: returns number of files successfully deleted. on failure sets value of "$!"  and returns false. It will not tell you the name of file which it couldn't delete.
Do not try deleting directory instead use rmdir command.

unlink("file1.txt");
unlink "a.txt", "b.txt";
unlink @list;
unlink glob "*.bak";

chmod in perl: setting file permissions

Changes the permission of the specified list of files. First argument should be numerical octal value. Returns number of files successfully changed.

usage is as follow:

$result = chmod 0777, "file1.txt","file2.txt";
chmod 0755, @filelist;
$mode  = 0655; chmod $mode, @filelist;

$mode = "0655"; chmod oct($mode), @filelist;
$mode = "0655"; chmod $mode, @filelist; # Not a good usage Should avoid, instead use above method

How to decide mode octal value:
mode is of 4 octal digits e.g. 0754, equivalent to (userID)(user)(group)(Other)
0755: u: full, GO: read and execute
0777: all : full permission
0555: all : read and execute

 symbolic representation of file permissions:

Representation
Class
Description
u
User
Owner
g
Group
Members of file group
o
Others
Neither owner nor group
a
All
Everyone

Octal notation:

Octal
System
Description
0
---
No permission
1
--x
execute
2
-w-
write
3
-wx
Write and execute
4
r--
Read
5
r-x
Read and execute
6
rw-
Read and write
7
rwx
Read, write and execute

link in perl

create a new file linked to oldfile. function creates a hard link if you want symbolic link then use symlink function.
returns 1 on success and 0 on failure. NewFile should not be present else will throw an error. This feature is similar to copying and renaming the file.

link OLDFILE, NEWFILE;
link "file1.txt", "file3.txt";

Perl File operations: Functions for file handling and interacting with system

chdir(EXPR): changes the working directory to EXPR, if EXPR is omitted then changes to home directory. refer to chdir in perl for more details.

chmod: changes the permission on a list of files.

glob:expand filenames using wild card

link: create a hard link in the system

lstat: stat a symbolic link

mkdir: makes a directory, similar to md dos command

opendir: opens a directory in code and gives directory handle.

readlink: determines where a symbolic link is pointing

rename: change a filename

unlink: deletes a file permanently

Tuesday, September 14, 2010

chdir in perl for changing working directory

usage:
chdir EXPR
chdir(EXPR)

Changes the working directory to EXPR, if possible. If EXPR is omitted, changes to directory specified by $ENV{HOME}, if not set, changes to $ENV{LOGDIR} directory . if none of these is set then does nothing.

Returns 1 upon success, 0 otherwise.
Example:
chdir("C:\temp\working_dir\");

Using file copy and move in perl

copy function is used for copying a file from one location to another. Its present in File::copy module.
 
use File::Copy; 
copy("C:\temp\file1", "C:\tmp\file1.orig") or die "copy failed: $!";  # will make a copy of file1
copy("C:\temp\Fileout.txt", *STDOUT) or die "copy failed: $!";  # will print Fileout.txt into STDOUT
move("C:\temp\Fileout.txt", "C:\temp2\Fileout.txt") or die "move failed: $!";    # will move first argument to 2nd argument

This doesnt support features like creating backups of file, recursive copying etc. these functions also support file handles as argument.

Friday, August 13, 2010

Converting POD To html ( Plain Old Documentation to HTML Conversion)

First you should know what is POD, so for Writing Documentation in Perl Modules Check Article "Plain OLD Documentation". After reading this article you will know what is POD and then point comes to convert it to distributable format i.e. html format, so no one will have to look into your perl code and one can know what all is there in that particular module.

There is a module in Perl Called as "pod2html" which gives details on how to convert pod to html.
 Following is the command line for converting pod 2 html :

 pod2html --help --htmlroot= --infile=
                           --outfile=  --podpath=:...:
                           --podroot= --libpods=:...:
                           --recurse --norecurse --verbos
                           --index --noindex --title=

--htmlroot: Root path when creating HTML File from pod.
--infile: Specify the pod file name to convert to html 
--outfile: HTML Filename to be created, if not given then output will go to STDOUT
--index : Creates index at TOP of HTML File (Default)
--noindex: Oppsite of "--index", don't create index at top
--recurse: Recurse into subdirectories specified in podpath(default)
--norecurse: Do not recurse
--title: Title of resulting HTML File
--verbose: Display progress messages
--podroot: Base directory for finding library pods



Example: pod2html --infile=Test.pm --outfile=Test.html --header --index --backlink="Go Back to Top"

 For further study: POD::HTMLPlain OLD Documentation



Tuesday, June 8, 2010

Getting Information about file

File Checking
Existence:


if( -e $file) { print "File exists \n";}
else { print "File doesn't exist\n"; }

File exists with size 0(zero) : -z
File exists with size non-zero : -s

Readable/writable/Executable

File Readable : -rFile Writable : -w
File Executable : -x

Text/Binary

Text : -T
Binary : -B

Thursday, April 22, 2010

BEGIN, INIT, CHECK, UNITCHECK, END : Five different code blocks

These are different types of code blocks available in Perl, which get executed during start and end of any Perl program. One can have more than one of these blocks in any Perl program, and will get executed in appropriate moment.

BEGIN: It gets executed in Compile time, as soon as possible, even before the rest of the file is parsed. There may be multiple BEGIN{} blocks and they will be executed in FIFO.

END : It is executed as late as possible, that is, after Perl has finished running the program and just before the interpreter is being exited, even if it is exiting as a result of a die() function. There may be multiple END{} Blocks, within a file. They will be executed in LIFO(reverse) order. END blocks are not executed when you run Perl with -c switch i.e. It's not called in compile time.

UNITCHECK: It's called after the unit which defined them has been compiled. The main program file and module it loads are compilation units.

CHECK: These are run just after initial compile phase ends and before the run time begins in LIFO order. These are used by Perl compile suite to save the compiled state of the program.

INIT : These are run just before Perl run time begins execution in FIFO Order.

INIT, CHECK, UNITCHECK, blocks are used to catch the transition between compilation phase and execution phase of main program.
##########################################################
# One example program for Perl mod
print "8. Ordinary code runs at runtime.\n";

 END { print "12. Read perlmod for the rest of the story.\n" }
 INIT { print " 6. INIT blocks run FIFO just before runtime.\n" }
 UNITCHECK { print " 4. And therefore before any CHECK blocks.\n" }

 print "9. It runs in order, of course.\n";

 BEGIN { print " 1. BEGIN blocks run FIFO during compilation.\n" }
 CHECK { print " 5. CHECK blocks run LIFO after all compilation.\n" }
 BEGIN { print " 2. So this line comes out second.\n" }
 INIT { print " 7. Run this again, using Perl's -c switch.\n" }
 END { print "11. END blocks run LIFO at quitting time.\n" }
 UNITCHECK { print " 3. UNITCHECK blocks run LIFO after each file is compiled.\n" }

 print "10. It merely _looks_ like it should be confusing.\n";

Wednesday, April 7, 2010

including/Using/Require Perl Modules in Program

There are many ways to include modules/packages in your program.

Use Module;

Or
Use Module List;

Which is exactly equivalent to

    BEGIN { require Module; import module; }
                   or
    BEGIN { require Module; import Module List; }
                   or
    require "Module.pm";
    import Module;

All perl modules have extensions.pm, use assumes this so you don't have to spell it out explicitly as "Modules.pm" instead you can say use Module;

Following two statements:

require Module;
require "Module.pm";

differ from each other. In first case, any double colon ( my::module) will be translated to  system's directory separator e.g."/", while in second case you will have to give module name with path literally.


With "require" one can get into following problem:
require myModule;
$some = myModule::somefunc();  # Need to make myModule:: accessible

use myModule;               # Import Names from myModule
$some = somefunc();

require myModule;
$some=somefunc();  # Error: No main::somefunc(),    so be careful in this case. solution is as follow

Above problem can be solved as following:
require "myModule.pm";
import myModule;
$some=somefunc();

Command Line inclusion
For including module from command line use following command:
C:\>perl -MmyModule myprogram.pl

For more information on -M switch check perlrun in perldoc
For more information on this refer to this link.
or check in chapter 11. modules in Perl Programming.

Monday, March 29, 2010

Perl Functions & Keywords : a summary

Here are Perl's functions (including things that look like functions, like some keywords and named operators). Some functions may appear in more than one place just because they can be categorize in more than one place.

SCALARs or strings Functions:

chomp, chop, chr, crypt, hex, index, lc, lcfirst, length, oct, ord, pack, q//, qq//, reverse, rindex, sprintf, substr, tr///, uc, ucfirst, y///

Regular expressions and pattern matching Functions

m//, pos, quotemeta, s///, split, study, qr//, tr///

Numeric functions

abs, atan2, cos, exp, hex, int, log, oct, rand, sin, sqrt, srand

Functions for real @ARRAYs

pop, push, shift, splice, unshift

Functions for list data

grep, join, map, qw//, reverse, sort, unpack

Functions for real %HASHes

delete, each, exists, keys, values

Input and output functions
binmode, close, closedir, dbmclose, dbmopen, die, eof, fileno, flock, format, getc, print, printf, read, readdir, rewinddir, say, seek, seekdir, select, syscall, sysread, sysseek, syswrite, tell, telldir, truncate, warn, write

Functions for fixed length data or records

pack, read, syscall, sysread, syswrite, unpack, vec

Functions for filehandles, files, or directories

-X, chdir, chmod, chown, chroot, fcntl, glob, ioctl, link, lstat, mkdir, open, opendir, readlink, rename, rmdir, stat, symlink, sysopen, umask, unlink, utime, mkpath


Miscellaneous functions

defined, dump, eval, formline, local, my, our, reset, scalar, state, undef, wantarray

Functions for processes and process groups
alarm, exec, fork, getpgrp, getppid, getpriority, kill, pipe, qx//, setpgrp, setpriority, sleep, system, times, wait, waitpid

Low-level socket functions

accept, bind, connect, getpeername, getsockname, getsockopt, listen, recv, send, setsockopt, shutdown, socket, socketpair

System V interprocess communication functions

msgctl, msgget, msgrcv, msgsnd, semctl, semget, semop, shmctl, shmget, shmread, shmwrite

Fetching user and group info

endgrent, endhostent, endnetent, endpwent, getgrent, getgrgid, getgrnam, getlogin, getpwent, getpwnam, getpwuid, setgrent, setpwent

Fetching network info

endprotoent, endservent, gethostbyaddr, gethostbyname, gethostent, getnetbyaddr, getnetbyname, getnetent, getprotobyname, getprotobynumber, getprotoent, getservbyname, getservbyport, getservent, sethostent, setnetent, setprotoent, setservent

Time-related functions

gmtime, localtime, time, times

Keywords related to the control flow of your Perl program
caller, continue, die, do, dump, eval, exit, goto, last, next, redo, return, sub, wantarray

Keywords related to switch
break, continue, given, when, default

(These are only available if you enable the "switch" feature. See feature and "Switch statements" in perlsyn.)

Keywords related to scoping
caller, import, local, my, our, state, package, use
(state is only available if the "state" feature is enabled. See feature.)
 
Keywords related to perl modules
do, import, no, package, require, use

Keywords related to classes and object-orientation
bless, dbmclose, dbmopen, package, ref, tie, tied, untie, use

Functions new in perl5

abs, bless, break, chomp, chr, continue, default, exists, formline, given, glob, import, lc, lcfirst, lock, map, my, no, our, prototype, qr//, qw//, qx//, readline, readpipe, ref, sub*, sysopen, tie, tied, uc, ucfirst, untie, use, when

* - sub was a keyword in perl4, but in perl5 it is an operator, which can be used in expressions.

Functions obsoleted in perl5

dbmclose, dbmopen

For more information on Perl Function Check this link

Wednesday, March 24, 2010

Executing and Capturing the output of a command on /bin/sh or cmd.exe using perl

qx/STRING/ or 'STRING' can be used for the purpose of capturing the output.
This interpolates the STRING and executes it as system command and returns the value as output of command execution. In scalar Context it returns the STDOUT value as a string( may be multi line).
STDOUT is referred as 1 while STDERR as 2.

Usage:
# To Captures both STDOUT and STDERR, Redirects STDERR(2) to STDOUT(1)
$output = `cmd 2>&1`;
 
# Captures Only STDOUT, STDERR is discarded ( STDERR is redirected to null) 
$output = `cmd 2>/dev/null`;  
 
# To capture only STDERR, and discard STDOUT, Order is Important
$output = `cmd 2>&1 1>/dev/null`;  
Using this following command you can redirect STDOUT and STDERR outputs to different files.
system("perl test.pl 1>test.stdout 2>test.stderr");

Check "`STRING`" in perlop (Part of perldoc) for more detail.

Monday, March 22, 2010

Setting environment variable using Perl Programs

Most of the time you may need to add the path of some directories where your code files are present or even your perl modules are there, and you want to use them.

Since %ENV is a special Hash in Perl, which contains values for all of your environment variables. So you can set any variables just like other hash variable in Perl.
So you can add following directories in your Path variable as:

$ENV{PATH} = 'C:\bin; C:\mymodule';

In this way it will replace your previous path to the one you are adding, but that will be till the program runs.

For Adding path in addition to existing path do it like following:

my $path = $ENV{PATH};
$path = $path . ';C:\bin; C:\mymodule';
$ENV{PATH} = $path;
print "\n\n $ENV{PATH}";


Similarly other variables can also be set.
You can check whether it's added or not by :

print $ENV{PATH};

Friday, March 19, 2010

Plain Old Documentation( POD) in perl

If you know how to write the perl codes then you should also know "How to document your script for better user interface". When you run script then if any of the wrong input should be told by script and user should be able to know how to use your script.

So the solution is use of POD::Usage module from perl. If you want to know more on basics of POD refer to this link. This link has good explanation on pod2usage.


pod2usage will print a usage message for the invoking script (using its embedded pod documentation) and then exit the script with the desired exit status. The usage message printed may have any one of three levels of "verboseness": 

Verbose = 0; Only SYNOPSIS
Verbose = 1; SYNOPSIS along with OPTIONS(command line arguments)
Verbose = 2; Full manual Page

Default Exit status: 2, verbose: 0
If exit status < 2, default Verbose = 1, else default verbose = 0
If verbose = 0, default exit status = 2, else default exit =1

If exit status < 2, output printed on STDOUT, otherwise on STDERR.

Usage:
use POD::Usage;

my $message_text = "This text precedes the usage message.";
my $exit_status = 2;
my $verbose_level = 0;
my $filehandle = \*STDERR;

pod2usage($message_text); # Will print a message immediately before printing usage message

pod2usage($exit_status); # Prints usage message depending upon exit status

pod2usage( { -message => $message_text ,
                    -exitval => $exit_status ,
                    -verbose => $verbose_level,
                    -output => $filehandle } );

Hash Keys for pod2usage(Arguments): Refer to this page for more help

-message : To print message before usage


-exitval : value to be passed to exit().


-verbose : Describes Level of usage message to be printed


-output : File handle where to print usage e.g. STDOUT, STDERR or any file


-input : A reference to a filehandle from which the invoking script's pod documentation should be read. Default is $0 i.e. current file.

-sections : Can print sections described in this section list as string e.g. "NAME|SYNOPSIS|OPTIONS", but only when verbose is 99.


-pathlist :A list of directory paths. If input file not in current directory then used.

-noperldoc : It will call perldoc if verbose >2.


A good example for using pod2usage(), for more refer to this link 

=================================================================

use Getopt::Long; 
use Pod::Usage;

my $args = @ARGV; # After GetOptions @ARGV will be undefined
GetOptions(
       'opt=s'    => \$option,
       'help'     => \$help,
       'man'      => \$man,
     ) or pod2usage( -message => "Try \"perl $0 -help\" for more information", exitval => 2);



pod2usage( -verbose => 1 ) if( $help || !$args);

pod2usage(-verbose => 2) if($man);
;1
__END__
 
=head1 NAME
 
   sample - Using GetOpt::Long and Pod::Usage
 
=head1 SYNOPSIS
 
   sample [options] [file ...]
 
   Options:
   -help brief help message
   -man  full documentation
   -opt  Option value
 
=head1 OPTIONS
 
=over 8
 
=item B<-help>
 
   Print a brief help message and exits.
 
=item B<-man>
 
   Prints the manual page and exits.

=item B<-opt>
 
   Takes some integer value
 
=back
 
=head1 DESCRIPTION
 
   B<This program> will read the given input in opt and do something
   useful with the contents thereof.
 
=cut

#=================================================================
Some important Links:
POD TUTORIAL
POD USAGE IN PERLDOC
PERLPOD IN PERLDOC




Saturday, March 13, 2010

Some good example on usage of perl

Function: Split(/<reg exp>/, $scalar)
Use split function for splitting each character in a word or sentence, a example is given below. Return Value bu split is array of splitted  characters/word/symbols.
Code:
my $flist="my blog is http://batchandperl.blogspot.com";
my @klist = split(//, $flist);
print "@klist";



Output:
m y   b l o g   i s   h t t p : / / b a t c h a n d p e r l . b l o g s p o t . c o m






Split at ":" in any sentence
Code: @klist = split(/:/, $flist); # : will be removed and two array variables will get generated.
Output: my blog is http //batchandperl.blogspot.com

Friday, March 12, 2010

Use of caller in Perl

Caller or Caller Expr
Returns the context of the current subroutine call. In scalar context, returns the caller's package name if there is a caller, that is, if we're in a subroutine or eval or require, and the undefined value otherwise. In list context, returns
#      0            1            2
($package, $filename, $line ) = caller;


With EXPR, it returns some extra information that the debugger uses to print a stack trace. The value of EXPR indicates how many call frames to go back before the current one
# 0                       1       2           3             4
($package, $filename, $line, $subroutine, $hasargs,

 #    5                6              7             8           9           10
$wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash)
= caller($i);

(caller 1)[3] will give the name of the calling subroutine.
($package, $filename, $line ) = caller; or

($package, $filename, $line ) = caller 1; # Both of these are equivalent

@caller = caller 0; #will return whole information about the calling subroutine

Creating Copy of File handles in Perl/Redirecting STDOUT to a file and screen

To Create an independent copy of file descriptor for the file handle: Use open with & mode
open(OLDOUT, ">&STDOUT" ) or die "Couldn't dup STDOUT: $!";
open(OLDIN, "<&STDIN" ) or die "Couldn't dup STDIN : $!";

Use open with the &= mode to create an alias for that filehandle's file descriptor:
open(OUTALIAS, ">&=STDOUT") or die "Couldn't alias STDOUT: $!";
open(INALIAS, "<&=STDIN") or die "Couldn't alias STDIN : $!";
open(BYNUMBER, ">&=5") or die "Couldn't alias file descriptor 5: $!";

If we try to create a copy of any file handle descriptor then actually we are calling dup (2) system call, by this we get two independent file descriptors whose file position, locks, and flags are shared, but which have independent stdio buffers. We can close any of file descriptor, which will not affect the other one.

But If we try to create a alias of any file handle then actually we are calling fdopen (3) stdio function. We get a single file descriptor with two stdio buffers accessed through two filehandles. Closing one filehandle closes the file descriptor of any aliases, but not their filehandles

# take copies of the file descriptors
open(OLDOUT, ">&STDOUT");
open(OLDERR, ">&STDERR");

# redirect stdout and stderr
open(STDOUT, "> /tmp/program.out")  or die "Can't redirect stdout: $!";
open(STDERR, ">&STDOUT")            or die "Can't dup stdout: $!";

# run the program
system($joe_random_program);

# close the redirected filehandles
close(STDOUT)                       or die "Can't close STDOUT: $!";
close(STDERR)                       or die "Can't close STDERR: $!";

# restore stdout and stderr
open(STDERR, ">&OLDERR")            or die "Can't restore stderr: $!";
open(STDOUT, ">&OLDOUT")            or die "Can't restore stdout: $!";

# avoid leaks by closing the independent copies
close(OLDOUT)                       or die "Can't close OLDOUT: $!";
close(OLDERR)                       or die "Can't close OLDERR: $!";