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

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



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

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