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};

Adding any directory path to environment variable "PATH" in windows

To add any path to your system's PATH environment variable to be used as search path please follow the steps below.

Right click on "My Computer" then select properties.


Now click on "Environment Variables" button. Now it will open up environment variables list.



Under "System Variables" select "Path" variable and "edit"


Go to last add ";" and add path of your directory path. Suppose you want to add "C:\Bin" in path variable. After adding it say "OK".

And It's done.









To check whether your path is added or not, open a new cmd.exe from run. and type "path". you will see you directory added there.