abs
#!/usr/bin/perl -w
# absolute value function
print "Absolute number of -100 is ", abs(-100), "\n";
print "Absolute number of +200 is ", abs(+200), "\n";
OUTPUT:
Absolute number of -100 is 100
Absolute number of +200 is 200
perldoc5.8.0
accept
#!/usr/bin/perl -w
# accept an incoming socket connect
perldoc5.8.0
alarm
#!/usr/bin/perl -w
# schedule a SIGALRM
perldoc5.8.0
atan2
#!/usr/bin/perl -w
# arctangent of Y/X in the range -PI to PI
perldoc5.8.0
bind
#!/usr/bin/perl -w
# binds an address to a socket
bind SOCKET, NAME;
bind S, $socketaddr or die "Can't bind address: $!\n";
perldoc5.8.0
binmode
#!/usr/bin/perl -w
# prepare binary files for I/O
open WP, "$file.wp" or die "Can't open $file.wp: die $!\n";
binmode WP;
while (read WP, $buf, 1024) {...}
perldoc5.8.0
bless
#!/usr/bin/perl -w
# create an object
bless REF, CLASSNAME;
bless REF;
perldoc5.8.0
caller
#!/usr/bin/perl -w
# get context of the current subroutine call
cal();
sub cal {
my ($package,$filename,$line) = caller;
print "package is $package\n";
print "filename is $filename\n";
print "line is $line\n";
}
OUTPUT:
package is main
filename is caller.pl
line is 3
perldoc5.8.0
chdir
#!/usr/bin/perl -w
# change your current working directory
my $ok = chdir($ENV{"HOME"});
system("pwd");
print "$ok\n";
OUTPUT:
/home/ana
1
perldoc5.8.0
chmod
#!/usr/bin/perl -w
# changes the permissions on a list of files
my @files = ("file1.pl","file2.pl","file3.pl");
my $counter = chmod 0755 @files;
print "$counter\n";
OUTPUT:
3
perldoc5.8.0
chomp
#!/usr/bin/perl -w
# remove a trailing record separator from a string
print "Enter line: ";
my @line = <STDIN>;
my $cnt = chomp @line;
print "Line: @line\n";
print "chomp removed $cnt newline characters.\n";
OUTPUT:
Enter line: first
second
third
d
Line first second third
chomp removed 3 newline characters.
perldoc5.8.0
chop
#!/usr/bin/perl -w
# remove the last character from a string
my $line = "Line" . "\n" . "\n";
chop $line;
print "$line";
chop $line;
print "$line";
chop $line;
print "$line";
OUTPUT:
Line
LineLin
perldoc5.8.0
chown
#!/usr/bin/perl -w
# change the owership on a list of files
chown $uid, $gid, @filenames;
sub chown_by_name {
local ($user,$pattern) = @_;
chown ((getpwnam($user))[2,3], glob($pattern));
}
&chown_by_name("fred", ".pl");
perldoc5.8.0
chr
#!/usr/bin/perl -w
# get character this number represents
my $ch = chr(65);
print "$ch\n";
OUTPUT:
A
perldoc5.8.0
chroot
#!/usr/bin/perl -w
# make directory new root for path lookups
perldoc5.8.0
close
#!/usr/bin/perl -w
# close file handle
open (OUT,">test.txt");
print OUT "test line\n";
close(OUT);
perldoc5.8.0
closedir
#!/usr/bin/perl -w
# close directory handle
closedir DIRHANDLE
perldoc5.8.0
connect
#!/usr/bin/perl -w
# connect to a remote socket
connect SOCKET, NAME;
connect S, $destadd or die "Can't connect to $hostname: $!\n";
perldoc5.8.0
continue
#!/usr/bin/perl -w
# optional trailing block in a while or foreach
while (EXPR) {
### redo always comes here
do_something;
} continue {
### next always comes here
do_something_else;
# then back the top to re-check EXPR
}
### last always comes here
perldoc5.8.0
cos
#!/usr/bin/perl -w
# cosine function
perldoc5.8.0
crypt
#!/usr/bin/perl -w
# one-way passwd-style encryption
$pwd = (getpwuid ($<))[1];
$salt = substr $pwd, 0, 2;
system "stty -echo";
print "Password: ";
chop($word = <STDIN>);
print "\n";
system "stty echo";
if (crypt($word,$salt) ne $pwd) {
die "Sorry...\n";
} else {
print "ok\n";
}
perldoc5.8.0
dbmclose
#!/usr/bin/perl -w
# breaks binding on a tied dbm file
dbmclose HASH;
perldoc5.8.0
dbmopen
#!/usr/bin/perl -w
# create binding on a tied dbm file
dbmopen %ALIASES, "/etc/aliases", 0666
or die "Can't open aliases: $!\n";
while (($key,$val) = each %ALIASES){
print $key, ' = ', $val, "\n";
}
dbmclose %ALIASES;
perldoc5.8.0
defined
#!/usr/bin/perl -w
# test whether a value, variable, or function is defined
@ary = ('first',undef,'second','third');
print "$val\n" while defined($val = pop(@ary));
OUTPUT:
third
second
perldoc5.8.0
delete
#!/usr/bin/perl -w
# deletes a value from a hash
%HASH = (1 => first, 2 => second, 3 => third);
foreach $key (keys %HASH) {
print "$key = $HASH{$key}\n";
}
if (exists $HASH{"2"})
{
delete $HASH{"2"};
}
foreach $key (keys %HASH) {
print "$key = $HASH{$key}\n";
}
OUTPUT:
1 = first
2 = second
3 = third
1 = first
3 = third
perldoc5.8.0
die
#!/usr/bin/perl -w
# raise an exception or bail out
$file = "filename.dat";
open (IN,"$file")
or die "Can't open $file $!\n";
OUTPUT:
Can't open filename.dat No such file or directory
perldoc5.8.0
do
#!/usr/bin/perl -w
# turn a BLOCK into a TERM
$cnt = 5;
do {
print "Counter = $cnt\n";
$cnt--;
} while ($cnt);
OUTPUT:
Counter = 5
Counter = 4
Counter = 3
Counter = 2
Counter = 1
perldoc5.8.0
dump
#!/usr/bin/perl -w
# create an immediate core dump
dump LABEL if $ARGV[0] eq '-d';
perldoc5.8.0
each
#!/usr/bin/perl -w
# retrieve the next key/value pair from a hash
%HASH = (1 => first, 2 => second, 3 => third);
while (($key,$value) = each %HASH) {
print "$key = $value\n";
}
OUTPUT:
1 = first
2 = second
3 = third
perldoc5.8.0
eof
#!/usr/bin/perl -w
# test a filehandle for its end
while (<>) {
if ( eof() ) {
print "*" x 20, "\n";
}
print;
}
OUTPUT:
first line
second line
********************
perldoc5.8.0
eval
#!/usr/bin/perl -w
# catch exceptions or compile and run code
perldoc5.8.0
exec
#!/usr/bin/perl -w
# abandon this program to run another
exec echo, "Arguments are: ", "@ARGV\n";
#/usr/bin/perl exec.pl 1 2 3 4 5
OUTPUT:
Arguments are: 1 2 3 4 5
perldoc5.8.0
exists
#!/usr/bin/perl -w
# test whether a hash key is present
print "Exists\n" if exists $hash{$key};
print "Defined\n" if defined $hash{$key};
print "True\n" if $hash{$key};
print "Exists\n" if exists $array[$index];
print "Defined\n" if defined $array[$index];
print "True\n" if $array[$index];
print "Exists\n" if exists &subroutine;
print "Defined\n" if defined &subroutine;
if (exists $ref->{A}->{B}->{$key}) { }
if (exists $hash{A}{B}{$key}) { }
if (exists $ref->{A}->{B}->[$ix]) { }
if (exists $hash{A}{B}[$ix]) { }
if (exists &{$ref->{A}{B}{$key}}) { }
exists ⊂ # OK
exists &sub(); # Error
perldoc5.8.0
exit
#!/usr/bin/perl -w
# terminate this program
$answer = <STDIN>;
exit 0 if $answer =~ /^[nN]/;
perldoc5.8.0
exp
#!/usr/bin/perl -w
# raise e to a power
$var = exp(3.3);
print $var, "\n";
OUTPUT:
27.1126389206579
perldoc5.8.0
fcntl
#!/usr/bin/perl -w
# file control system call
use Fcntl;
fcntl($filehandle, F_GETFL, $packed_return_buffer)
or die "can't fcntl F_GETFL: $!";
perldoc5.8.0
fileno
#!/usr/bin/perl -w
# return file descriptor from filehandle
print fileno(STDIN), "\n";
print fileno(STDOUT), "\n";
print fileno(STDERR), "\n";
OUTPUT:
0
1
2
perldoc5.8.0
flock
#!/usr/bin/perl -w
# lock an entire file with an advisory lock
use Fcntl ':flock'; # import LOCK_* constants
sub lock {
flock(MBOX,LOCK_EX);
# and, in case someone appended
# while we were waiting...
seek(MBOX, 0, 2);
}
sub unlock {
flock(MBOX,LOCK_UN);
}
open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
or die "Can't open mailbox: $!";
lock();
print MBOX $msg,"\n\n";
unlock();
perldoc5.8.0
fork
#!/usr/bin/perl -w
# create a new process just like this one
perldoc5.8.0
format
#!/usr/bin/perl -w
# declare a picture format with use by the write function
# a report on the /etc/passwd file
format STDOUT_TOP =
Passwd File
Name Login Office Uid Gid Home
#------------------------------------------------------------------
.
format STDOUT =
@<<<<<<<<<<<<<<<<<< @||||||| @<<<<<<@>>>> @>>>> @<<<<<<<<<<<<<<<<<
$name, $login, $office,$uid,$gid, $home
.
perldoc5.8.0
formline
#!/usr/bin/perl -w
# internal function used for formats
$str = formline <<'END', 1,2,3;
@<<< @||| @>>>
END
print "$^A' from the accumulator!\n";
OUTPUT:
1 2 @ from the accumulator!
perldoc5.8.0
getc
#!/usr/bin/perl -w
# get the next character from the filehandle
perldoc5.8.0
getgrent
#!/usr/bin/perl -w
# get next group record
perldoc5.8.0
getgrgid
#!/usr/bin/perl -w
# get group record given group user ID
perldoc5.8.0
getgrnam
#!/usr/bin/perl -w
# get group record given group name
perldoc5.8.0
gethostbyaddr
#!/usr/bin/perl -w
# get host record given its address
perldoc5.8.0
gethostbyname
#!/usr/bin/perl -w
# get host record given name
perldoc5.8.0
gethostent
#!/usr/bin/perl -w
# get next hosts record
perldoc5.8.0
getlogin
#!/usr/bin/perl -w
# return who logged in at this tty
perldoc5.8.0
getnetbyaddr
#!/usr/bin/perl -w
# get network record given its address
perldoc5.8.0
getnetbyname
#!/usr/bin/perl -w
# get networks record given name
perldoc5.8.0
getnetent
#!/usr/bin/perl -w
# get next networks record
perldoc5.8.0
getpeername
#!/usr/bin/perl -w
# find the other end of a socket connection
perldoc5.8.0
getpgrp
#!/usr/bin/perl -w
# get process group
perldoc5.8.0
getppid
#!/usr/bin/perl -w
# get parent process ID
perldoc5.8.0
getpriority
#!/usr/bin/perl -w
# get current nice value
perldoc5.8.0
getprotobyname
#!/usr/bin/perl -w
# get protocol record given name
perldoc5.8.0
getprotobynumber
#!/usr/bin/perl -w
# get protocol record numeric protocol
perldoc5.8.0
getprotoent
#!/usr/bin/perl -w
# get next protocols record
perldoc5.8.0
getpwent
#!/usr/bin/perl -w
# get next passwd record
perldoc5.8.0
getpwnam
#!/usr/bin/perl -w
# get passwd record given user login name
perldoc5.8.0
getpwuid
#!/usr/bin/perl -w
# get passwd record given user ID
perldoc5.8.0
getservbyname
#!/usr/bin/perl -w
# get services record given its name
perldoc5.8.0
getservbyport
#!/usr/bin/perl -w
# get services record given numeric port
perldoc5.8.0
getservent
#!/usr/bin/perl -w
# get next services record
perldoc5.8.0
getsockname
#!/usr/bin/perl -w
# retrieve the sockaddr for a given socket
perldoc5.8.0
getsockopt
#!/usr/bin/perl -w
# get socket options on a given socket
perldoc5.8.0
glob
#!/usr/bin/perl -w
# expand filenames using wildcards
perldoc5.8.0
gmtime
#!/usr/bin/perl -w
# convert UNIX time into record or string using Greenwich time
perldoc5.8.0
goto
#!/usr/bin/perl -w
# create spaghetti code
perldoc5.8.0
grep
#!/usr/bin/perl -w
# locate elements in a list test true against
# a given criterion
my @ary = qw(one Bob Antony two);
my $how_many = grep { /^[A-Z]/ } @ary;
print "There are $how_many names\n";
my @names = grep { /^[A-Z]/ } @ary;
print "@names\n\n";
my @miss = qw(1 one two 12 4 three);
my @numbers = grep number($_), @miss;
print "@numbers\n";
sub number {
if ( $_ =~ /^\d+$/ ) {
return $_;
}
}
OUTPUT:
There are 2 names
Bob Antony
1 12 4
perldoc5.8.0
hex
#!/usr/bin/perl -w
# convert a string to a hexadecimal number
perldoc5.8.0
import
#!/usr/bin/perl -w
# patch a module's namespace into your own
perldoc5.8.0
index
#!/usr/bin/perl -w
# find a substring within a string
perldoc5.8.0
int
#!/usr/bin/perl -w
# get the integer portion of a number
perldoc5.8.0
ioctl
#!/usr/bin/perl -w
# system-dependent device control system call
perldoc5.8.0
join
#!/usr/bin/perl -w
# join a list into a string using a separator
# join(DELIMITER,LIST);
my @pass = qw( root x 0 0 root /root /bin/bash );
print "@pass\n";
my $str = join (":" => @pass);
print "$str\n";
# prepare pattern
my @keys = qw( print do while if else );
my $pat = qr/@{[ join ('|' => @keys) ]}/;
print "$pat\n";
OUTPUT:
root x 0 0 root /root /bin/bash
root:x:0:0:root:/root:/bin/bash
(?-xism:print|do|while|if|else)
perldoc5.8.0
keys
#!/usr/bin/perl -w
# retrieve list of indices from a hash
perldoc5.8.0
kill
#!/usr/bin/perl -w
# send a signal to a process or process group
perldoc5.8.0
last
#!/usr/bin/perl -w
# exit a block prematurely
perldoc5.8.0
lc
#!/usr/bin/perl -w
# return lower-case version of a string
perldoc5.8.0
lcfirst
#!/usr/bin/perl -w
# return a string with just the next letter in lower case
perldoc5.8.0
length
#!/usr/bin/perl -w
# return the number of bytes in a string
perldoc5.8.0
link
#!/usr/bin/perl -w
# create a hard link in the filesytem
perldoc5.8.0
listen
#!/usr/bin/perl -w
# register your socket as a server
perldoc5.8.0
local
#!/usr/bin/perl -w
# create a temporary value for a global variable
perldoc5.8.0
localtime
#!/usr/bin/perl -w
# convert UNIX time into record or string using local time
perldoc5.8.0
lock
#!/usr/bin/perl -w
# get a thread lock on a variable, subroutine, or method
perldoc5.8.0
log
#!/usr/bin/perl -w
# retrieve the natural logarithm for a number
perldoc5.8.0
lstat
#!/usr/bin/perl -w
# stat a symbolic link
perldoc5.8.0
m
#!/usr/bin/perl -w
# match a string with a regular expression pattern
perldoc5.8.0
map
#!/usr/bin/perl -w
# apply a change to a list to get back
# a new list with the changes
my @numb = qw(1 2 3 4 5);
my %numbers = map { $_, $_**2 } @numb;
for ( sort { $a <=> $b } keys (%numbers) )
{
print "$_=>$numbers{$_} ";
}
print "\n\n";
my @ary = ( [qw(aaa 298)],[qw(bbb 59)],[qw(ccc 74)] );
print map {
$_->[1], " ", $_->[0], "\n";
} sort {
$a->[1] <=> $b->[1];
} map {
if ( $_->[1] < 100 ) {
$_->[1] += 100
}; $_;
} @ary;
OUTPUT:
1=>1 2=>4 3=>9 4=>16 5=>25
159 bbb
174 ccc
298 aaa
perldoc5.8.0
mkdir
#!/usr/bin/perl -w
# create a directory
perldoc5.8.0
msgctl
#!/usr/bin/perl -w
# SysV IPC message control operations
perldoc5.8.0
msgget
#!/usr/bin/perl -w
# get SysV IPC message queue
perldoc5.8.0
msgrcv
#!/usr/bin/perl -w
# receive a SysV IPC message from a message queue
perldoc5.8.0
msgsnd
#!/usr/bin/perl -w
# send a SysV IPC message to a message queue
perldoc5.8.0
my
#!/usr/bin/perl -w
# declare and assign a local variable
perldoc5.8.0