Notes
Notes
Notes
1
UNIT I
INTRODUCTION
1.1.1 Principle 1:
The software source code should be available with license and that license contain permissions they are
Open Source Software
1.1.2 Principle 2:
The license should not have certain restrictions in terms of
1) Technology
2) Field
3) Hardware
1.1.2.1 Technology: Here Technology means operating system, in the computer science there are
many different operating systems available, here the software must support all kinds of operating sytems
such as windows, UNIX, Linux and Mac os.
1.1.2.2 Field: Now a days computer enter into many fields such as agriculture, medical and
Biotechnology fields. here the software must supports or works in all fields
1.1.2.3 Hardware: In this context hardware means devices such as Nokia, Samsung and celkon. Here
the software should work on or supports all kinds of devices.
The license must explicitly permit distribution of software built from modified source code. The license may
require derived works to carry a different name or version number from the original software.
1.2.5. No Discrimination against Persons or Groups
The license must not discriminate against any person or group of persons.
1.2.6. No Discrimination against Fields of Endeavor
The license must not restrict anyone from making use of the program in a specific field of endeavor. For
example, it may not restrict the program from being used in a business, or from being used for genetic
research.
1.2.7. Distribution of License
The rights attached to the program must apply to all to whom the program is redistributed without the need
for execution of an additional license by those parties.
1.2.8. License Must Not Be Specific to a Product
The rights attached to the program must not depend on the program's being part of a particular software
distribution. If the program is extracted from that distribution and used or distributed within the terms of the
program's license, all parties to whom the program is redistributed should have the same rights as those that
are granted in conjunction with the original software distribution.
1.2.9. License Must Not Restrict Other Software
The license must not place restrictions on other software that is distributed along with the licensed software.
For example, the license must not insist that all other programs distributed on the same medium must be
open-source software.
1.2.10. License Must Be Technology-Neutral
No provision of the license may be predicated on any individual technology or style of interface.
users and programmers are also invaluable resources for asking questions relating to troubleshooting and
suggesting enhancements.
1. Accounting
2. Content Management Systems
3. CRM (Customer Relationship Management)
4. Desktop Environments/ Shell replacements
5. Email Clients
6. Encoding, Conversion & Ripping Tools
7. ERP
8. Filesharing & FTP
9. Graphics-Design & Modeling Tools
10. Messengers & Communication Clients
11. Project Management
12. Reporting Tools
13. RSS
14.Web Browser
S.No
Application
Cloud management
Abiquo
Ecommerce
Avactis
Reporting Tools
Actuate
Alfresco
Bacula
Compiere
Office Productivity
Lotus Symphony
RDBMS
Ingres Database
Sun Studio
10
Ubuntu
Stability
Linux is based on the UNIX kernel. It providespreemptive multitasking and protected memory.
Community
Linux is part of the greater open-source community. This consists of thousands of developers
and
many more users world-wide whosupport open software.This user and developer base is also a support base.
Freedom
Linux is free. This means more than just costingnothing. This means that you are allowed to do
whatever you want to with the software. This is why Redhat, Mandrake, and Suse are all allowed to sell
their own distributions of Linux
The Linux kernel is an operating system kernel used by the Linux family of Unix-likeoperating
systems. It is one of the most prominent examples of free and open source software.
The Linux kernel was initially conceived and created by Finnishcomputer science student Linus
Torvalds in 1991. Linux rapidly accumulated developers and users who adopted code from other free
software projects for use with the new operating system. The Linux kernel has received contributions from
thousands of programmers. Many Linux distributions have been released based upon the Linux kernel.
1.5.2.2 Properties of Linux:
Linux Pros
A lot of the advantages of Linux are a consequence of Linux' origins, deeply rooted in UNIX, except for the
first advantage, of course:
Linux is free:
Linux is scalable:
The Linux OS and most Linux applications have very short debug-times:
1.5.2.3 Linux Cons
Linux
The majority of Linux variants are available
Price
Reliability
Software
Windows
rebooted.
Hardware
Source
Support
10
is
usually
referred
to
as
GNU/Linux.
User mode is the normal mode of operating for programs. Web browsers, calculators, etc. will all be in user
mode. They don't interact directly with the kernel, instead, they just give instructions on what needs to be
done, and the kernel takes care of the rest. Kernel mode, on the other hand, is where programs communicate
directly with the kernel. A good example of this would be device drivers. A device driver must tell the
kernel exactly how to interact with a piece of hardware, so it must be run in kernel mode. Because of this
close interaction with the kernel, the kernel is also a lot more vulnerable to programs running in this mode,
so it becomes highly crucial that drivers are properly debugged before being released to the public.
A process can run in two modes:
1.User Mode.
2.Kernel Mode.
1.6.1 User Mode:
A mode of the CPU when running a program.In this mode,the user process has no access to
thememory locations used by the kernel.When a program isrunning in User Mode, it cannot directly access
the kerneldata structures or the kernel programs.
The kernel-mode programs run in the background, making sureeverything runs smoothly - things
like printer drivers,display drivers, drivers that interface with the monitor,keyboard, mouse, etc. These
programs all run in such a waythat you don't notice them.
When the computer boots up, Windows calls the KERNEL, themain kernel-mode program that
allows all the other programsto run, even the user-mode programs.
User mode is the normal mode of operating for programs, webbrowsers etc. They don't interact
directly with the kernel,instead, they just give instructions on what needs to bedone, and the kernel takes
care of the rest. Kernel mode, onthe other hand, is where programs communicate directly withthe kernel.
A good example of this would be device drivers.A device driver must tell the kernel exactly how to
interactwith a piece of hardware, so it must be run in kernel mode.Because of this close interaction with the
kernel, thekernel is also a lot more vulnerable to programs running inthis mode, so it becomes highly crucial
that drivers areproperly debugged before being released to the public.
These are the programs that you run when you want specificprograms - e.g., MS Paint, MS Word,
and Calculator. Theseare heavily restricted, as to not crash the system. Windows
11
uses memory-protection services offered by the processor toprevent malicious programs from interfering
with the rest ofthe system and corrupting it.
In User mode, the executing code has no ability to directlyaccess hardware or reference memory.
Code running in user mode must delegate to system APIs to access hardware or memory.
Due to the protection afforded by this sort of isolation, crashes in user mode are always
recoverable. Most of the code running on your computer will execute in user mode.
1.6.2 Kernel Mode:
A mode of the CPU when running a program. In this mode, it is the kernel that is running on behalf
of the user process and directly access the kernel data structures or the kernel programs.Once the system call
returns,the CPU switches back to user mode.
The kernel-mode programs run in the background, making sureeverything runs smoothly - things
like printer drivers,display drivers, drivers that interface with the monitor,
keyboard, mouse, etc. These programs all run in such a waythat you don't notice them.
When the computer boots up, Windows calls the KERNEL, themain kernel-mode program that
allows all the other programsto run, even the user-mode programs.Kernel mode, also referred to as system
mode, is one of thetwo distinct modes of operation of the CPU in Linux. Whenthe CPU is in kernel mode, it
is assumed to be executingtrusted software, and thus it can execute any instructionsand reference any
memory addresses (i.e., locations inmemory). The kernel (which is the core of the operatingsystem and has
complete control over everything that occursin the system) is trusted software, but all other programsare
considered untrusted software.
In this mode executing code has complete and unrestricted access to the underlying hardware. It
can execute any CPU instruction and reference any memory address. Kernel mode is generally reserved for
the lowest-level, most trusted functions of the operating system. Crashes in kernel mode are catastrophic;
they will halt the entire PC.
When the CPU is in kernel mode, it is assumed to be executing trusted software, and thus it can
execute any instructions and reference any memory addresses (i.e., locations in
memory).
The kernel (which is the core of the operating system and has complete control over everything
that occurs in the system) is trusted software, but all other programs are considered
untrusted software. Thus, all user mode software must request use of the kernel by means of a system call in
order to perform privileged instructions, such as process creation or input/output operations.
Kernal mode has higher priority while user mode has lowerpriority.
Kernal mode has ability to read user mode but user mode hasnot priority to interface in
kernal mode.
When a process is in user space, its system calls are being intercepted by the tracing
thread. When it's in the kernel, it's not under system call tracing. This is the distinction
between user mode and kernel mode.
The transition from user mode to kernel mode is done by the tracing thread. When a
process executes a system call or receives a signal, the tracing thread forces the process
to run in the kernel if necessary and continues it without system call tracing.
12
LINUX [Unit-II]
13
UNIT II
with Linux. .
LINUX:
2.1Process:
Definition: A process is an executing (i.e., running) instance of a program. Process are frequently referred to
as tasks.
Process (process ID, PID): All software runs within an operating system concept known as a
process. Each program running on a system is assigned its own process ID (PID). Users can easily obtain a
process list (using Task Manager on Windows or ps on UNIX) in order to see what is running.
Processes inside out
2.1.1 Multi-user and multi-tasking:
Multiuser refers to having more than 1 person able to log into the computer and each person have
their own settings (bookmark, desktop, themes, etc) Multitasking is the ability of the computer to do more
than 1 thing (program) at a time. There was a time when you could not surf, type in word and listen to music
on your computer all at one time.
Interactive processes
Interactive processes are initialized and controlled through a terminal session. In other words, there
has to be someone connected to the system to start these processes; they are not started automatically as part
of the system functions
Running a process in the background is only useful for programs that don't need user input (via the
shell).Putting a job in the background is typically done when execution of a job is expected to take a long
time. In the example, using graphical mode, we open an extra terminal window from the existing one:
xterm &
LINUX [Unit-II]
14
Meaning
regular_command
command &
Jobs
Ctrl+Z
Ctrl+C
Bg
Fg
Kill
End a process
At times when the total system load is low enough to accept extra jobs: done using the batch
command.
2.1.2.4 Daemons
A daemon process has no controlling terminal. It cannot open /dev/tty. Most daemons tend to last a
long time, be owned by root, and do something useful. A daemon is just a process that runs in the
background, usually waiting for something to happen that it is capable of working with, like a printer
daemon is waiting for print commands.
Daemons are server processes that run continuously. Most of the time, they are initialized at system
startup and then wait in the background until their service is required.
2.1.2.5 Process attributes
A process has a series of characteristics, which can be viewed with the ps command:
Open Source Software
LINUX [Unit-II]
The process ID or PID: a unique identification number used to refer to the process.
The parent process ID or PPID: the number of the process (PID) that started this process.
Nice number: the degree of friendliness of this process toward other processes (not to be confused
15
with process priority, which is calculated based on this nice number and recent CPU usage of the
process).
User name of the real and effective user (RUID and EUID): the owner of the process.
Real and effective group owner (RGID and EGID): The real group owner of a process is the primary
group of the user who started the process.
Ending processes
When a process ends normally (it is not killed or otherwise unexpectedly interrupted), the program
returns its exit status to the parent. This exit status is a number returned by the program providing the results
of the program's execution.
2.1.2.7.3
Signals:
Processes end because they receive a signal. There are multiple signals that you can send to a
process. Use the kill command to send a signal to a process. The command kill -l shows a list of signals.
Most signals are for internal use by the system, or for programmers when they write code. As a user, you
will need the following signals:
Open Source Software
LINUX [Unit-II]
16
Signal number
SIGTERM
15
SIGINT
SIGKILL
SIGHUP
2.1.2.8
Meaning
BIOS: The Basic Input/Output System is the lowest level interface between the computer and
peripherals. The BIOS performs integrity checks on memory and seeks instructions on the Master
Boor Record (MBR) on the floppy drive or hard drive.
The MBR points to the boot loader (GRUB or LILO: Linux boot loader).
Boot loader (GRUB or LILO) will then ask for the OS label which will identify which kernel to run
and where it is located (hard drive and partition specified). The installation process requires to
creation/identification of partitions and where to install the OS. GRUB/LILO are also configured
during this process. The boot loader then loads the Linux operating system.
The first thing the kernel does is to execute init program. Init is the root/parent of all processes
executing on Linux.
Based on the appropriate run-level, scripts are executed to start various processes to run the system
and make it functional.
One of the most powerful aspects of Linux concerns its open method of starting and stopping the
operating system, where it loads specified programs using their particular configurations, permits you to
change those configurations to control the boot process, and shuts down in a graceful and organized way.
Beyond the question of controlling the boot or shutdown process, the open nature of Linux makes it
much easier to determine the exact source of most problems associated with starting up or shutting down
your system. A basic understanding of this process is quite beneficial to everybody who uses a Linux
system.A lot of Linux systems use lilo, the LInux LOader for booting operating systems. We will only
discuss GRUB, however, which is easier to use and more flexible.
2.1.2.8.2
Init:
The kernel, once it is loaded, finds init in sbin and executes it.When init starts, it becomes the
parent or grandparent of all of the processes that start up automatically on your Linux system. The first thing
init does, is reading its initialization file, /etc/inittab. This instructs init to read an initial configuration
Open Source Software
LINUX [Unit-II]
17
script for the environment, which sets the path, starts swapping, checks the file systems, and so on.
Basically, this step takes care of everything that your system needs to have done at system initialization:
setting the clock, initializing serial ports and so forth.
Then init continues to read the /etc/inittab file, which describes how the system should be set up
in each run level and sets the default run level. A run level is a configuration of processes. All UNIX-like
systems can be run in different process configurations, such as the single user mode, which is referred to as
run level 1 or run level S (or s). In this mode, only the system administrator can connect to the system. It is
used to perform maintenance tasks without risks of damaging the system or user data. Naturally, in this
configuration we don't need to offer user services, so they will all be disabled. Another run level is the
reboot run level, or run level 6, which shuts down all running services according to the appropriate
procedures and then restarts the system.
Use the who to check what your current run level is:
aravind@home:~> who -r
run-level 2 2010-10-17 23:22
2.1.2.8.3
last=S
Available run levels are generally described in /etc/inittab, which is partially shown below:
#
# inittab This file describes how the INIT process should set up
# the system in a certain run-level.
# Default run level. The run levels are:
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode
# 2 - Multiuser, without NFS# (The same as 3, if you do not have networking)
# 3 - Full multiuser mode
# 4 - unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
#
id:5:initdefault:
<--cut-->
LINUX [Unit-II]
18
Advanced Concepts
Scheduling Personalities Cloning Signals Development with Linux
2.2 Scheduling:
A Linux system can have a lot to suffer from, but it usually suffers only during office hours. Whether in an
office environment, a server room or at home, most Linux systems are just idling away during the morning, the
evening, the nights and weekends. Using this idle time can be a lot cheaper than buying those machines you'd
absolutely need if you want everything done at the same time.
There are three types of delayed execution:
Waiting a little while and then resuming job execution, using the sleep command. Execution time depends on
the system time at the moment of submission.
Running a command at a specified time, using the at command. Execution of the job(s) depends on system
time, not the time of submission.
Regularly running a command on a monthly, weekly, daily or hourly basis, using the cron facilities.
aravind@home:~> at 0237
warning: commands will be executed using (in order) a) $SHELL
b) login shell c) /bin/sh
at> cd new-programs
at> ./configure; make
at> <EOT>
job 2 at 2010-10-14 02:00
Open Source Software
LINUX [Unit-II]
19
The -m option sends mail to the user when the job is done, or explains when a job can't be done.
Thecommand atq lists jobs; perform this command before submitting jobs in order prevent them from
starting at the same time as others. With the atrm command you can remove scheduled jobs if you change
your mind.
The at command is used to schedule one or more programs for a single execution at some later time.
There are actually four client commands:
1. at: Runs commands at specified time
2. atq: Lists pending commands
3. atrm: Cancels pending jobs
4. batch: Runs commands when system load permits
The Linux at command accepts a number of time specifications, considerably extending the POSIX.2
standard.
2.2.2
when they should run from the system's and users' crontab entries. Only the root user has access to the
system crontabs, while each user should only have access to his own crontabs. On some systems (some)
users may not have access to the cron facility.
At system startup the cron daemon searches /var/spool/cron/ for crontab entries which are named
after accounts in /etc/passwd, it searches /etc/cron.d/ and it searches /etc/crontab, then uses this information
every minute to check if there is something to be done. It executes commands as the user who owns the
crontab file and mails any output of commands to the owner.
Syntax
crontab [-e] [-l] [-r] [filename]
-e
edit a copy of the current user's crontab file, or creates an empty file to edit if crontab
does not exist.
-l
-r
Filename
LINUX [Unit-II]
20
12
14
Echo hai
Options Explanation
*
*/5
2,4,6
9-17
Is treated as ever 5 minutes, hours, days, or months. Replacing the 5 with another numerical value
will change this option.
Treated as an OR, so if placed in the hours, this could mean at 2, 4, or 6 o-clock.
Treats for any value between 9 and 17. So if placed in day of month this would be days 9 through
17. Or if put in hours it would be between 9 and 5.
We change the scheduling parameters by means of the system calls illustrated in the Table
Table 10-1: System Calls Related to Scheduling
System Call
nice( )
getpriority( )
Description
Change the priority of a conventional process.
Get the maximum priority of a group of conventional
processes.
setpriority( )
sched_getscheduler( )
LINUX [Unit-II]
21
sched_setscheduler( )
sched_getparam( )
sched_setparam( )
sched_yield( )
Get the time quantum value for the Round Robin policy.
Most system calls shown in the table apply to real-time processes, thus allowing users to develop real-time
applications.
2.3 Personalities:
Linux supports different execution domains, or personalities, for each process. Among other things,
execution domains tell Linux how to map signal numbers into signal actions. The execution domain system
allows Linux to provide limited support for binaries compiled under other Unix-like operating systems.
This function will return the current personality() when persona equals 0xffffffff. Otherwise, it will
make the execution domain referenced by persona the new execution domain of the current process.
2.3.1 Name
personality - set the process execution domain
Synopsis
#include <sys/personality.h>
int personality(unsigned long persona);
2.3.2 Return Value
On success, the previous persona is returned. On error, -1 is returned, and errno is set appropriately.
2.4 Cloning:
Making an image copy of your system disk is a great way to create a backup. With the cost of
portable USB drives at all time lows, you could keep a couple around for rotation purposes. If your main
drive does crash, you could be back up and running in a matter of minutes. Now all that's left is making the
actual image copy. There are lots of ways to accomplish this task, and we'll spell a few of them out to help
you along.
The biggest issue with making an image copy of your system disk is that you must boot from another
device to get a true copy. Options include a "live" CD or bootable USB. You probably have a copy of your
favorite distribution's installation disk lying around somewhere, so that would be an obvious choice. For our
purposes we'll use the Ubuntu 10.4 distro on a USB disk. The second option would be to use an actual disk
Open Source Software
LINUX [Unit-II]
22
duplication distro like Clonezilla. This turns out to be one of the easier ways to get the job done, especially if
you're not too comfortable with the command line.
2.4.1
that hard if you don't either. The one thing you do need is the distribution ISO file.
You should be able to boot straight from the disk once you have it created. It's possible you might
have to change your BIOS setting to allow the system to boot from USB. Many newer systems (like Dell
machines) have an option to bring up a boot menu when the machine first powers up by pressing the F12
key. Once you get the system booted you're ready to make your backup image copy. You might want to run
the Disk Utility found under the System / Administration list. This will give you the opportunity to look at
the disks attached to your system and their organization.
The Disk Utility provides a number of utilities including mount / dismount and format volume. It's a
good idea to go ahead and format the drive if you're reusing an old Windows disk. GParted 0.5.1 comes
standard with the basic Ubuntu 10.04 boot disk. It includes an option to copy a partition. Instructions for
doing the work can be found on either the GParted site or on the Ubuntu forums. There's also a GParted Live
CD if you want to go that route.
Be prepared to wait a while if you choose to backup your system to an external USB drive. The total
estimated time in our case was almost four hours. One really good alternative is to use a disk caddy adapter
like the Thermaltake BlackX ST0005U. It has an eSATA connector that speeds up the data transfer process
tremendously. This is a must have if you're the type that frequently tears into systems or builds new ones.
2.4.2
now download an AMD64 version which includes support for 64-bit versions of all the applications and the
imaging of large partitions. All the applications have been updated to the latest versions along with the
2.6.32-12 version of the Linux kernel. The hardest part of using Clonezilla to image your hard drives is
making sure you know which drive is the master and which drive will be your copy. Clonezilla also takes
LINUX [Unit-II]
23
care of copying the Master Boot Record (MBR) while accomplishing the same task with the Ubuntu Live
CD method requires some command line magic.
2.4.3
2.5 Signals:
A signal is an event generated by Linux in response to some condition, which may
cause a process to take some action when it receives the signal. Raise is the term that indicates the
generation of a signal. Catch is the term to indicate the receipt of a signal. Introduced in UNIX systems to
simplify IPC. Used by the kernel to notify processes of system Events. A signal is a short message sent to a
process, or group of processes, containing the number identifying the signal.POSIX.4 defines i/f for queuing
& ordering RT signals w/ arguments. Linux supports 31 non-real-time signals.
Processes end because they receive a signal. There are multiple signals that you can send to a
process. Use the kill command to send a signal to a process. The command kill -l shows a list of signals.
Most signals are for internal use by the system or for programmers when they write code.
2.5.1
Sending signals:
A program can signal a different program using the kill() system call with prototype
int kill(pid_t pid, int sig);
This will send the signal with number sig to the process with process ID pid. Signal numbers are
small positive integers. (For the definitions on your machine, try /usr/include/bits/signum.h. Note that
these definitions depend on OS and architecture.)
A user can send a signal from the command line using the kill command. Common uses are kill 9
N to kill the process with pid N, or kill -1 N to force process N (maybe init or inetd) to reread its
configuration file.
Certain user actions will make the kernel send a signal to a process or group of processes: typing the
interrupt character (probably Ctrl-C) causes SIGINT to be sent, typing the quit character (probably Ctrl-\)
sends SIGQUIT, hanging up the phone (modem) sends SIGHUP, typing the stop character (probably Ctrl-Z)
sends SIGSTOP.
Open Source Software
LINUX [Unit-II]
24
Certain program actions will make the kernel send a signal to that process: for an illegal instruction
one gets SIGILL, for accessing non existing memory one gets SIGSEGV, for writing to a pipe while nobody
is listening anymore on the other side one gets SIGPIPE, for reading from the terminal while in the
background one gets SIGTTIN, etc.
2.5.2
Receiving signals:
When a process receives a signal, a default action happens, unless the process has arranged to handle
the signal. For the list of signals and the corresponding default actions, see signal(7). For example, by
default SIGHUP, SIGINT, SIGKILL will kill the process; SIGQUIT will kill the process and force a core
dump; SIGSTOP, SIGTTIN will stop the process; SIGCONT will continue a stopped process; SIGCHLD
will be ignored.
Traditionally, one sets up a handler for the signal using the signal system call with prototype
typedef void (*sighandler_t)(int);
sighandler_t signal(int sig, sighandler_t handler);
This sets up the routine handler() as handler for signals with number sig. The return value is (the
address of) the old handler. The special values SIG_DFL and SIG_IGN denote the default action and
ignoring, respectively.
When a signal arrives, the process is interrupted, the current registers are saved, and the signal
handler is invoked. When the signal handler returns, the interrupted activity is continued.
It is difficult to do interesting things in a signal handler, because the process can be interrupted in an
arbitrary place, data structures can be in arbitrary state, etc. The three most common things to do in a signal
handler are (i) set a flag variable and return immediately, and (ii) (messy) throw away all the program was
doing, and restart at some convenient point, perhaps the main command loop or so, and (iii) clean up and
exit.
2.5.3
Blocking signals:
Each process has a list (bitmask) of currently blocked signals. When a signal is blocked, it is not
delivered (that is, no signal handling routine is called), but remains pending.
The sigprocmask () system call serves to change the list of blocked signals.
The sigpending () system call reveals what signals are (blocked and) pending.
The sigsuspend () system call suspends the calling process until a specified signal is received.
Signal number
Meaning
SIGHUP
Hangup (POSIX)
SIGINT
LINUX [Unit-II]
25
SIGQUIT
SIGILL
SIGTRAP
SIGIOT
SIGBUS
SIGFPE
SIGKILL
Kill(can't be caught or
ignored) (POSIX)
SIGUSR1
10
Language
C/C++/FORTRAN
Linux Compiler
GNU C compiler man page (gcc)
GNU C++ compiler man page (g++)
Java
C#
Ximian: MONO
ADA
GNAT
LISP
Cobol
Pascal
LINUX [Unit-II]
26
Tool
c++filt
SWIG.org
Description
Demangle C++ symbols
Interface compiler for wrapper routines which run from test scripts for
testing "C" and "C++". Works with Perl, Python, Ruby, and Tcl and
allows scripts to invoke "C" and "C++" functions, libraries, etc.
C:
Tool
Description
bison
pccts
flex
Java:
Tool
kaffe
Description
Java virtual machine
DataBase:
Tool
gdbm
Description
The GNU database manager.
Berkeley DB
SQLite.org
Language
Eclipse.org
Linux Compiler
IBM open source JAVA and C/C++ (with CDT plug-in) IDE. Included
with FC4. Extensible IDE consortium - Borland, IBM, Red Hat, Rational.
Lots of industry backing. (Open Source)
Also see EclipsePluginCentral.com Plugins available for Subversion
LINUX [Unit-II]
27
SCM, XML documents, HEX, ...
Also see: YoLinux C++ Development
Anjuta
C, C++. Written for GTK/Gnome. Solid, simple, intuitive, bug free IDE
for C/C++ development on Linux. Search/Indexing, edit, compile and
debug. Almost no learning curve for those familiar with IDE's.
Cobol
Pascal
Language
Linux Compiler
GNU Checker
dbmalloc
fda
MCheck
C/C++ memory usage and malloc checker for x86/linux. Detects accesses
to uninitialized variables, bad pointers, double frees and memory leaks.
YAMD
28
UNIT III
MySQL:
3.1 Introduction:
MySQL is the most popular open source SQL database management system (DBMS), is developed,
distributed, and supported by MySQL AB. MySQL AB is a commercial company, founded by the MySQL
developers.
A database is a collection of data that is organized so that its contents can be easily accessed, managed
and updated. MySQL is a data storage area. In this storage area, there are small sections called Tables. A
Relational Database Management System (RDBMS) may be a DBMS in which data is stored in the form of
tables and the relationship among the data is also stored in the form of tables.
The data in MySQL is stored in database objects called tables. A table is a collection of related data
entries and it consists of columns and rows. The MySQL database has become the world's most popular
open source database because of its consistent fast performance, high reliability and ease of use.
3.1.1 Advantages:
o MySQL is Cross-Platform. It runs on more than 20 platforms including Linux, Windows, Mac OS,
Solaris, HP-UX, IBM AIX, giving you the kind of flexibility that puts you in control.
o MySQL is fast. It is used by a variety of corporations that demand performance and stability.
o MySQL is free. It is Open Source software. As such you are free to examine the source code and
make any changes you wish. As per its GPL license, you are free to redistribute those changes as
long as your software is also Open Source.
o Reliable and easy to use.
o Multi-Threaded multi-user and robust SQL Database server.
3.1.2 Disadvantages:
o
Missing Sub-selects.
29
By using statements intended for creating accounts, such as CREATE USER or GRANT. These
statements cause the server to make appropriate modifications to the grant tables.
By manipulating the MySQL grant tables directly with statements such as INSERT, UPDATE, or
DELETE.
DROPUP USER:
The DROP USER statement removes one or more MySQL accounts and their privileges.
Syntax:
DROP USER user [, user] ...
Example:
Open Source Software
30
GRANT :
The GRANT statement enables system administrators to grant privileges to MySQL
user accounts.
Syntax:
GRANT
priv_type [(column_list)]
[, priv_type [(column_list)]] ...
ON [object_type] priv_level
TO user [IDENTIFIED BY [PASSWORD] 'password']
[, user [IDENTIFIED BY [PASSWORD] 'password']] ...
[REQUIRE {NONE | ssl_option [[AND] ssl_option] ...}]
[WITH with_option ...]
Example
GRANT ALL PRIVILEGES ON *.* TO 'monty'@'localhost' WITH GRANT OPTION;
3.2.4
RENAME USER
REVOKE:
The REVOKE statement enables system administrators to revoke privileges from MySQL accounts.
Syntax:
REVOKE
priv_type [(column_list)]
[, priv_type [(column_list)]] ...
ON [object_type] priv_level
FROM user [, user] ...
REVOKE ALL PRIVILEGES, GRANT OPTION
FROM user [, user] ...
Example:
RENAME USER 'jeffrey'@'localhost' TO 'jeff'@'127.0.0.1';
Open Source Software
31
SET PASSWORD :
The SET PASSWORD statement assigns a password to an existing MySQL user.
Syntax
SET PASSWORD [FOR user] =
{
PASSWORD('some password')
| OLD_PASSWORD('some password')
| 'encrypted password'
}
Example:
SET PASSWORD FOR 'bob'@'%.loc.gov' = PASSWORD('newpass');
Login:
32
o The most commonly needed parameters are the host to connect to (the host where the MySQL server
runs), your MySQL username, and a password.
o For example:
% mysql -h localhost -p -u cbuser
Enter password: cbpass
3.3.3
Stopping MySQL:
You can stop the MySQL server by executing this command:
shell> "C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqladmin" -u
root shutdown
3.3.3.1 Logout:
To terminate a mysql session, issue a QUIT statement:
mysql> QUIT
You can also terminate the session by issuing an EXIT statement or (under Unix) by typing
Ctrl-D.
33
34
Checking what version of the server you're running or the name of the current database:
mysql> SELECT VERSION( ), DATABASE( );
+-------------+------------+
| VERSION( ) | DATABASE( )|
+-------------+------------+
| 3.23.51-log | cookbook |
+-------------+------------+
3.5.2
To display some or all of the columns from a table. Use * as a shortcut that selects all columns. Or
name the columns you want to see explicitly.
For example,
mysql> SELECT * FROM emp;
+---------------------+---------+---------+
|
name
|
age
|
exp |
+---------------------+---------+---------+
|
AnandKumar
|
45
|
12
|
+---------------------+---------+---------+
3.5.3
Whenever you retrieve a result set, MySQL gives every output column a name. (That's how the
mysql program gets the names that you see displayed as the initial row of column headers in result set
output.) MySQL assigns default names to output columns, but if the defaults are not suitable, you can use
column aliases to specify your own names.
3.5.4
Add a WHERE clause to the query that indicates to the server which rows to return.
Unless you qualify or restrict a SELECT query in some way, it retrieves every row in your
table, which may be a lot more information than you really want to see.
mysql> SELECT name,age FROM emp WHERE name=aravind;
+---------------------+---------+
|
name
|
age
|
+---------------------+---------+
|
aravind
|
45
|
+---------------------+---------+
3.5.5
Use DISTINCT a query eliminate duplicate values. Some queries produce results containing
duplicate records.
For example,
mysql> SELECT name FROM emp;
+---------+
|
name |
+---------+
| aravind |
| anand
|
| Naveen |
| siva
|
| araivnd |
| anand
|
+---------+
But that result is heavily redundant. Adding DISTINCT to the query removes the duplicate
records, producing a set of unique values:
mysql> SELECT name FROM emp;
+---------+
|
name |
+---------+
| aravind |
| anand
|
| Naveen |
| siva
|
+---------+
35
3.5.6
36
To summarizing a set of values that may include NULL values and you need to know how to
interpret the results. Most aggregate functions ignore NULL values.
mysql> SELECT subject, test, score FROM expt ORDER BY subject, test;
+---------+------+-------+
| subject | test | score |
+---------+------+-------+
| Arun
|
A |
47 |
| Chitra |
B |
50 |
| David
|
C | NULL |
| Ram
|
D | NULL |
+---------+------+-------+
mysql> SELECT subject name FROM expt WHERE score IS NULL GROUP BY subject;
+---------+
| Name
|
+---------+
| David
|
| Ram
|
+---------+
3.5.7
Add an ORDER BY clause to tell it exactly how you want things sorted. When you select rows, the
MySQL server is free to return them in any order, unless you instruct it otherwise by saying how to sort the
result. There are lots of ways to use sorting techniques.
mysql> SELECT name,age FROM emp WHERE age > 24 ORDER BY name;
+---------+------+
| name
| age |
+---------+------+
| Arun
| 35 |
| Ram
| 45 |
| David
| 34 |
| Chitra | 56 |
+---------+------+
mysql> SELECT name,age FROM emp WHERE age > 24 ORDER BY name ASC;
+---------+------+
| name
| age |
+---------+------+
| Arun
| 35 |
| Chitra | 45 |
| David
| 34 |
| Ram
| 56 |
+---------+------+
37
Strings properties:
o Strings can be case sensitive (or not), which can affect the outcome of string
operations.
o You can compare entire strings or just parts of them by extracting substrings.
o You can apply pattern-matching operations to look for strings that have a certain
structure.
3.6.2
Types of Strings
Binary data may contain bytes that lie outside the usual range of printable ASCII characters.A binary
string in MySQL is one that MySQL treats as case sensitive in comparisons.For binary strings, the
characters A and a are considered different. For non-binary strings, they're considered the same.
A binary column type is one that contains binary strings. Some of MySQL's column types are binary
(case sensitive) and others are not, as illustrated here:
Column type
Binary/case sensitive
CHAR, VARCHAR
No
Yes
TEXT
No
BLOB
Yes
38
No
A characteristic of nonbinary strings is that they have a character set. To see which character sets are
available, use this statement:
mysql> SHOW CHARACTER SET;
Charset
big5
dec8
cp850
hp8
koi8r
latin1
latin2
...
utf8
ucs2
...
Description
Big5 Traditional Chinese
DEC West European
DOS West European
HP West European
KOI8-R Relcom Russian
cp1252 West European
ISO 8859-2
Central
European
Default collation
big5_chinese_ci
dec8_swedish_ci
cp850_general_ci
hp8_english_ci
koi8r_general_ci
latin1_swedish_ci
latin2_general_ci
Maxlen
2
1
1
1
1
1
1
UTF-8 Unicode
UCS-2 Unicode
utf8_general_ci
ucs2_general_ci
3
2
3.6.3
CHAR
VARCHAR
TINYTEXT
TEXT
BLOB
MEDIUMTEXT
LONGTEXT
BINARY
VARBINARY
ENUM
SET
3.6.4
39
CHAR() :
It is a fixed length string and is mainly used when the data is not going to vary much in it's length. It
ranges from 0 to 255 characters long. While storing CHAR values they are right padded with spaces to the
specified length. When retrieving the CHAR values, trailing spaces are removed.
3.6.5
VARCHAR() :
It is a variable length string and is mainly used when the data may vary in length. It ranges from 0 to
255 characters long. VARCHAR values are not padded when they are stored.
Note:
If CHAR and VARCHAR options are used in the same table, then MySQL will automatically
change the CHAR into VARCHAR for compatability reasons. The ( ) bracket allows to enter a maximum
number
of
characters
that
will
be
used
in
the
column.
TEXT :
TEXT columns are treated as character strings(non-binary strings). It contains a maximum length of
65535 characters.
3.6.8
BLOB :
BLOB stands for Binary Large OBject. It can hold a variable amount of data. BLOB columns are
MEDIUMTEXT, MEDIUMBLOB :
It has a maximum length of 16777215 characters.
40
The VARBINARY is similar to the VARCHAR type. It stores the value as binary byte strings
instead of non-binary character strings.
3.6.13 ENUM() :
An enumeration. Each column may have one of a specified possible values. It can store only one of
the values that are declared in the specified list contained in the ( ) brackets. The ENUM list ranges up to
65535 values.
3.6.14 SET() :
A set. Each column may have more than one of the specified possible values. It contains up to 64 list
items and can store more than one choice. SET values are represented internally as integers.
3.6.15 Choosing a String Data Type
Choose the data type according to the characteristics of the information to be stored and how you
need to use it. Consider questions such as these:
o
MySQL provides several binary and nonbinary string data types. These types come in pairs as shown in the
following table.
Binary data type
Nonbinary data
type
Maximum length
BINARY
CHAR
255
VARBINARY
VARCHAR
65,535
TINYBLOB
TINYTEXT
255
BLOB
TEXT
65,535
MEDIUMBLOB
MEDIUMTEXT
16,777,215
LONGBLOB
LONGTEXT
4,294,967,295
41
DATE:
A Date. The range is 1000-01-01 to 9999-12-31. The date values are displayed in YYYY-MM-DD format.
3.7.2
TIME:
A Time. The range is -838:59:59 to 838:59:59. The time values are displayed in HH:MM:SS format.
3.7.3
DATETIME:
A Date and Time combination. The range is 1000-01-01 00:00:00 to 9999-12-31 23:59:59. The date time
values are displayed in YYYY-MM-DD HH:MM:SS format.
3.7.4
TIMESTAMP:
A Timestamp. The range is 1970-01-01 00:00:01 UTC to partway through the year 2037. A TIMESTAMP
column is useful for recording the date and time of an INSERT or UPDATE operation.
3.7.5
YEAR:
A Year. The year values are displayed either in two-digit or four-digit format. The range of values for a
four-digit is 1901 to 2155. For two-digit, the range is 70 to 69, representing years from 1970 to 2069.
For Example
42
delimiter it is in fact possible to use any character, or no character between the date and time segments. For
example, the following formats all achieve the same result:
2008-10-23 10:37:22
20081023103722
2008/10/23 10.37.22
2008*10*23*10*37*22
3.7.7
of functions that can be used to manipulate dates and times. The following table provides a list of the more
common functions available for working with times and dates in MySQL:
Function
Description
ADDDATE()
Add dates
ADDTIME()
Add time
CONVERT_TZ()
CURDATE()
CURTIME()
DATE_ADD()
DATE_FORMAT()
DATE_SUB()
DATE()
DATEDIFF()
DAYNAME()
DAYOFMONTH()
DAYOFWEEK()
DAYOFYEAR()
EXTRACT
FROM_DAYS()
HOUR()
LAST_DAY
MAKEDATE()
MAKETIME
MAKETIME()
MICROSECOND()
MINUTE()
MONTH()
MONTHNAME()
NOW()
PERIOD_ADD()
PERIOD_DIFF()
QUARTER()
SEC_TO_TIME()
SECOND()
STR_TO_DATE()
SUBTIME()
Subtract times
SYSDATE()
TIME_FORMAT()
Format as time
TIME_TO_SEC()
TIME()
TIMEDIFF()
Subtract time
TIMESTAMP()
With a single argument, this function returns the Date or the Datetime expression.
With two arguments, the sum of the arguments is returned
TIMESTAMPADD()
TIMESTAMPDIFF()
TO_DAYS()
43
UTC_TIME()
44
UTC_TIMESTAMP() Returns the current Universal Time (UTC) date and time
WEEK()
WEEKDAY()
WEEKOFYEAR()
YEAR()
YEARWEEK()
3.7.8
function, which takes two arguments: a DATE, DATETIME, or TIMESTAMP value, and a string describing
how to display the value.
Within the formatting string, you indicate what to display using special sequences of
the form %c, where c specifies which part of the date to display.
For example, %Y, %M, and %d signify the four-digit year, the month name, and the two-digit day of the
month.
The following query shows the values in the date_val table, both as MySQL displays them by default
and as reformatted with DATE_FORMAT( ):
45
Define in which direction to sort, as the order of the returned rows may not yet be meaningful. Rows
can be returned in ascending or descending order.
Use ASC or DESC. Using ASC will sort the data so that you see the smallest number first. Using
DESC will sort the data so that you see the largest number first. In this query, you are looking for customers
with the largest negative balance first. ORDER BY will return the arrays with the greatest negative number
(the smallest number) at the top.
Syntax:
Here is generic SQL syntax of SELECT command along with ORDER BY clause to sort data from
MySQL table:
SELECT field1, field2,...fieldN table_name1, table_name2...
ORDER BY field1, [field2...] [ASC [DESC]]
o
You can sort returned result on any field provided that filed is being listed out.
You can use keyword ASC or DESC to get result in ascending or descending order. By default its
ascending order.
Example:
mysql> SELECT * from tutorials_tbl ORDER BY tutorial_author ASC
+-------------+----------------+-----------------+-----------------+
| tutorial_id | tutorial_title | tutorial_author | submission_date |
+-------------+----------------+-----------------+-----------------+
|
2 | Learn MySQL
| Abdul S
| 2007-05-24
|
|
1 | Learn PHP
| John Poul
| 2007-05-24
|
|
3 | JAVA Tutorial | Sanjay
| 2007-05-06
|
+-------------+----------------+-----------------+-----------------+
3.8.2
46
3.8.3
clause that doesnt appear in the column output list. ORDER BY is not limited to sorting only those columns
named in the column output list. It can sort using values that are "hidden".
Sorting and NULL Values
To sort columns that may contain NULL values. If NULL values don't come out in the desired position
within the sort order, False them into appearing where you want. When a sorted column contains NULL
values, MySQL puts them all together in the sort order.
mysql> SELECT NULL = NULL;
+-------------+
| NULL = NULL |
+-------------+
| NULL |
+-------------+
3.8.4
Date-Based Sorting
To sort in temporal order. Sort using a date or time column type, ignoring parts of the values that are
irrelevant if necessary. Many types of information include date or time information and it's very often
necessary to sort results in temporal order. MySQL knows how to sort temporal column types, so there's no
special trick to ordering values in DATE, DATETIME, TIME, or TIMESTAMP columns. Begin with a
table that contains values for each of those types:
mysql> SELECT * FROM temporal_val;
47
+------------+---------------------+----------+----------------+
| d
|
dt
|
t
|
ts
|
+------------+---------------------+----------+----------------+
| 1970-01-01 | 1884-01-01 12:00:00 | 13:00:00 | 19800101020000 |
| 1999-01-01 | 1860-01-01 12:00:00 | 19:00:00 | 20210101030000 |
| 1981-01-01 | 1871-01-01 12:00:00 | 03:00:00 | 19750101040000 |
| 1964-01-01 | 1899-01-01 12:00:00 | 01:00:00 | 19850101050000 |
+------------+---------------------+----------+----------------+
Summarizing with COUNT( ): To count the number of rows in an entire table or that match
particular conditions, use the COUNT( ) function.
For example,
mysql> SELECT COUNT(*) FROM emp_tab;
+----------+
| COUNT(*) |
+----------+
| 30
|
+----------+
48
For example,
mysql> SELECT MIN(Sal) AS low, MAX(sal) AS high FROM emp_tab;
+--------------+
| Low
| High |
+--------------+
| 100 | 500 |
+--------------+
3.9.3
SUM( ) and AVG( ) produce the total and average (mean) of a set of values:
For Example,
mysql> SELECT SUM(rno) AS 'No-Of Emp', AVG(sal) AS 'Avg Sal' FROM
emp_tab;
+--------------_------+
| No-Of Emp | Avg Sal |
+---------------_-----+
| 23456
| 8500
|
+----------------------+
3.9.4
rows are contained in a dataset by eliminating duplicates. Do this with DISTINCT. DISTINCT is useful for
boiling down a query result, and often is combined with ORDER BY to place the values in more meaningful
order. For example, if you want to know the names of the employees listed in the emp_tab table, use the
following query:
mysql> SELECT DISTINCT emp_name FROM emp_tab ORDER BY emp_name;
+-------+
| name |
+-------+
| Arun |
| John |
| Ram |
+-------+
A query without DISTINCT produces the same names, but is not nearly as easy to
understand:
mysql> SELECT emp_name FROM emp_tab;
+-------+
| name |
+-------+
| Arun |
| Jhon |
If you want to know how many different drivers there are, use COUNT(DISTINCT):
mysql> SELECT COUNT(DISTINCT emp_name) FROM emp_tab;
+----------------------+
| COUNT(DISTINCT emp_name) |
+----------------------+
| 3
|
+----------------------+
3.9.5
To calculate a summary for each subgroup of a set of rows, not an overall summary
Value use a GROUP BY clause to arrange rows into groups.
Without using GROUP BY
mysql> SELECT COUNT(*) FROM driver_log;
+----------+
| COUNT(*) |
+----------+
| 10
|
+----------+
Using GROUP BY
mysql> SELECT name, COUNT(name) FROM driver_log GROUP BY name;
+-------+-------------+
| name
| COUNT(name) |
+-------+-------------+
| Ben
|
3
|
| Henry |
5
|
| Suzi
|
2
|
+-------+-------------+
3.9.6
To summarizing a set of values that may include NULL values and you need to know how
to interpret the results. Most aggregate functions ignore NULL values.
mysql> SELECT subject, test, score FROM expt ORDER BY subject, test;
+---------+------+-------+
| subject | test | score |
+---------+------+-------+
| Arun
|
A |
47 |
| Chitra |
B |
50 |
| David
|
C | NULL |
| Ram
|
D | NULL |
+---------+------+-------+
49
50
mysql> SELECT subject name FROM expt WHERE score IS NULL GROUP BY subject;
+---------+
| Name
|
+---------+
| David
|
| Ram
|
+---------+
3.9.7
51
Sequences have a minimum and maximum value (the defaults are minimum=0 and maximum=2631); they can be dropped, but not reset
Once a sequence returns a value, the sequence can never return that same value
While sequence values are not tied to any particular table, a sequence is usually used to generate
values for only one table
52
Description
MAXVALUE
MINVALUE
CYCLE
Specify CYCLE to indicate that when the maximum value is reached the
sequence starts over again at the start value. Specify NOCYCLE to generate
an error upon reaching the maximum value.
53
Here is a very simple HTML page that specifies a title in the page header, and a body with white
background containing a single paragraph:
<Html>
<Body>
<H1>My Heading</H1>
Welcome To My Web Page
<P>My Paragraph</P>
<body bgcolor="green">
</Body>
</Html>
3.12.2 Static Web Page: A static web page shows the required information to the viewer, but do not accept
any information from the viewer.
Open Source Software
54
3.12.3 Dynamic Web Page: A dynamic web page displays the information to the viewer and also accepts
the information from the user Railway reservation, Online shopping etc. are examples of dynamic
web page.
3.12.4 Client side scripting is a script, (ex. Javascript, VB script), that is executed by the browser (i.e.
Firefox, Internet Explorer, Safari, Opera, etc.) that resides at the user computer.
3.12.5 Server side scripting, (ex. ASP.Net, ASP, JSP, PHP, Ruby, or others), is executed by the server
(Web Server), and the page that is sent to the browser is produced by the serve-side scripting.
3.12.6 Using Apache to Run Web Scripts:
o Open-Source Web server originally based on NCSA server(National Center for Supercomputing
Applications).
o Apache is the most widely used web server software package in the world.
o Apache is highly configurable and can be setup to support technologies such as, password protection,
virtual hosting (name based and IP based), and SSL encryption.
There are typically several directories under the Apache root directory, which I'll assume hereto be
/usr/local/apache. These directories include:
bin : Contains httpdthat is, Apache itselfand other Apache-related executable programs.
conf : For configuration files, notably httpd.conf, the primary file used by Apache.
Htdocs : The root of the document tree
55
UNIT IV
OPEN SOURCE PROGRAMMING LANGUAGES
PHP: Introduction Programming in web environment variables constants Data types operators Statements
Functions Arrays OOP String Manipulation and regular expression File handling and data storage.
56
The .php extension tells the web server that it needs to process this as a PHP file. If you accidentally save it
with a .html extension the server won't process your PHP code and the browser will just output it all to the
screen.
Simple Example:
Every block of PHP code must start with <?php and end with ?>. The following example
outputs the text "PHP is easy!" to the screen:
<html>
<head>
<title>PHP Syntax Example</title>
</head>
<body>
<?php
echo "PHP is easy!";
?>
</body>
</html>
Comments
While there is only one type of comment in HTML, PHP has two types. The first type we will
discuss is the single line comment. The single line comment tells the interpreter to ignore everything that
occurs on that line to the right of the comment. To do a single line comment type "//" or "#" and all text to
the right will be ignored by PHP interpreter.
Example:
<html>
<body>
<?php
Open Source Software
57
//This is a comment
/*This isa comment
block
*/
?>
</body>
</html>
Variables: Introduction
Variables are named "containers" that allow you to store a value or Variables are nothing but
identifiers to the memory location to store data. This value can then be used by other parts of the application,
simply by referring to the variable's name. For example, the application could display the contents of the
variable to the user.
In PHP, variable names must start with a dollar sign ($). For example:
<?php
$myVariable = "PHP is easy!";
echo $myVariable;
?>
Variable Names:
When creating names for your variables, you need to ensure they comply with the following naming
rules:
o All PHP variable names must start with a letter or underscore ( _ )"
o Variable names can only contain alpha-numeric characters and underscores ( i.e. a-z, A-Z, 0-9, or
_)
o Variable names must not contain spaces. For multi-word variable names, either separate the
words with an underscore ( _ ) or use capitalization.
PHP is a Loosely Typed Language:
Open Source Software
58
PHP is a loosely type language, which means that you do not need to declare a variable before
assigning a value to it .In PHP, the variable is declared automatically when you use it.
For Example,
<?php
$txt="Hello World!";
$x=16;
?>
String Variables in PHP:
A string variable is used to store and manipulate text. String variables are used for values that contains
characters. For Example,
<?php
$txt="Hello World";
echo $txt;
?>
The Concatenation Operator:
There is only one string operator in PHP.The concatenation operator (.) is used to put two string values
together.To concatenate two string variables together, use the concatenation operator:
<?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?>
Output: Hello World! What a nice day!
The strlen() function:
Open Source Software
59
Constants: Introduction:
A PHP constant is a variable that cannot be changed after the initial value is assigned during the
execution of the script, except for magic constants, which are not really constants.
You create a constant variable using the define () function. The function accepts 2 arguments.
The first must be a string and represents the name which will be used to access the constant. A
constant name is case-sensitive by default and by convention, are always uppercase. A valid constant
name starts with a letter or underscore, followed by any combination of letters, numbers or
underscores, however, by convention only capital letters and underscore symbols are used. Constant
names must be in quotes when they are defined
The second argument is the value of the constant and can be a string or numbers and can be set
explicitly or as as the result of a function or equation.
The scope of a constant is global, which means that you can access constants anywhere in your script
without regard to scope.
PHP has many pre-defined constants that can be used in your scripts.
60
<?php
define ("SECE", "Sri Eshwar College Of Engineering");
echo SECE;
?>
Magic Constants:
Magic constants, as they are called, are not acutally constants but effectively behave like constants
and are pre-defined. Magic constants use the same naming convention as PHP constants. The full list of
magic constants can be found here.
There are 7 magic constants that change depending on where they are used. They are listed below.
name
description
__LINE__
__FILE__
The full path and filename of the file. If used inside an include, the name of
the included file is returned. Since PHP 4.0.2, __FILE__ always contains an
absolute path with symlinks resolved whereas in older versions it contained
relative path under some circumstances.
__DIR__
The directory of the file. If used inside an include, the directory of the
included file is returned. This is equivalent to dirname(__FILE__). This
directory name does not have a trailing slash unless it is the root directory.
(Added in PHP 5.3.0.)
__FUNCTION__
The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns
the function name as it was declared (case-sensitive). In PHP 4 its value is
always lowercased.
__CLASS__
The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the
class name as it was declared (case-sensitive). In PHP 4 its value is always
lowercased.
__METHOD__
61
The class method name. (Added in PHP 5.0.0) The method name is returned
as it was declared (case-sensitive).
__NAMESPACE__ The name of the current namespace (case-sensitive). This constant is defined
in compile-time (Added in PHP 5.3.0)
Data Types:
PHP scripts deal with data in one form or another, usually stored in variables. PHP can work with
different types of data. For example, a whole number is said to have an integer data type, while a string of
text has a string data type.
Description
Example values
Integer
A whole number
7, -23
float
7.68, -23.45
string
A sequence of characters
"Hello", "abc123@#$"
unicode
A sequence of Unicode
"Hello", "abc123@#$"
characters
binary
A sequence of binary
"Hello", "abc123@#$"
(non-Unicode) characters
boolean
true, false
62
Compound data can contain multiple values. PHP has 2 compound data types:
o
Object - Can hold multiple values (properties), and can also contain methods (functions) for
working on properties.
An array or object can contain multiple values, all accessed via one variable name. What's more, those
values can themselves be other arrays or objects. This allows you to create quite complex collections of data.
Operators:
Operators are used to operate on values.
Arithmetic Operators:
Description
Operator
+
Addition
Example
x=2
Result
4
x+2
-
Subtraction
x=2
5-x
*
Multiplication
x=4
20
x*5
/
++
Division
Modulus
Increment
15/5
5/2
2.5
5%2
10%8
10%2
x=5
x=6
x++
63
--
Decrement
x=5
x=4
x--
Assignment Operators:
Operator
Example
Is The Same As
x=y
x=y
+=
x+=y
x=x+y
-=
x-=y
x=x-y
*=
x*=y
x=x*y
/=
x/=y
x=x/y
.=
x.=y
x=x.y
%=
x%=y
x=x%y
Operator
Description
Example
==
is equal to
!=
is not equal
<>
is not equal
>
is greater than
<
is less than
>=
<=
Comparison Operators:
64
Operator
Description
Example
&&
and
x=6
y=3
(x < 10 && y > 1) returns
true
||
or
x=6
y=3
(x==5 || y==5) returns false
not
x=6
y=3
!(x==y) returns true
Operator Precedence:
*/%
||
Highest Precedence
And
+-.
XOR
OR
= = = != =
Lowest Precedence
&&
Statements:
1. PHP Control Statements: if, elseif, else
3. while:
if ( )
Syntax:
{}
while (condition)
elseif ( )
{}
else
4. do:
{}
do
2. Switch:
switch ( )
} while (condition)
5. for:
case condition1
break;
case condition2
break;
<?php
for ( $n = 1; $n < 10; $n++)
{
echo "$n<BR>";
}
}
?>
foreach:
Versions of PHP prior to version 4 do not support the foreach statement. The following code should list the
contents of the array.
<?php
$tree = array("trunk", "branches", "leaves");
foreach ($tree as $part)
{
66
Functions: Definition
A function is a block of code that performs a specific task. It has a name and it is reusable. To keep
the script from being executed when the page loads, you can put it into a function. A function will be
executed by a call to the function. You may call a function from anywhere within a page.
User-defined functions:
A function may be defined using syntax such as the following:
PHP function guidelines:
Open Source Software
Give the function a name that reflects what the function does
The function name can start with a letter or underscore (not a number)
Example
A simple function that writes my name when it is called:
<html>
<body>
<?php
function writeName()
{
echo "Kai Jim Refsnes";
}
echo "My name is ";
writeName();
?>
</body>
</html>
Output:
My name is Kai Jim Refsnes
PHP Functions - Adding parameters
To add more functionality to a function, we can add parameters. A parameter is just like a
variable.Parameters are specified after the function name, inside the parentheses.
Example 1
The following example will write different first names, but equal last name:
<html>
<body>
<?php
function writeName($fname)
{
echo $fname . " Refsnes.<br />";
Open Source Software
67
}
echo "My name is ";
writeName("Kai Jim");
echo "My sister's name is ";
writeName("Hege");
echo "My brother's name is ";
writeName("Stale");
?>
</body>
</html>
Output:
My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes.
My brother's name is Stale Refsnes.
PHP Functions - Return values:
To let a function return a value, use the return statement.
Example
<html>
<body>
<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
}
echo "1 + 16 = " . add(1,16);
?>
</body>
</html>
Output:
Open Source Software
68
69
1 + 16 = 17
Functions within functions:
<?php
function foo()
{
function bar()
{
echo "I don't exist until foo() is called.\n";
}
}
/* We can't call bar() yet
since it doesn't exist. */
foo();
70
foo(1, 2, 3);
?>
func_get_arg:
func_get_arg -- Return an item from the argument list
Description
mixed func_get_arg ( int arg_num )
Gets the specified argument from a user-defined function's argument list.
This function may be used in conjunction with func_get_args() and func_num_args() to allow user-defined
functions to accept variable-length argument lists.
Parameters:
arg_num
The argument offset. Function arguments are counted starting from zero.
Open Source Software
71
Return Values
Returns the specified argument, or FALSE on error.
Errors/Exceptions
Generates a warning if called from outside of a user-defined function, or if arg_num is greater than the number
of arguments actually passed.
Examples
Example 1. func_get_arg() example
<?php
function foo()
{
$numargs = func_num_args();
echo "Number of arguments: $numargs<br />\n";
if ($numargs >= 2) {
echo "Second argument is: " . func_get_arg(1) . "<br />\n";
}
}
foo (1, 2, 3);
?>
func_num_args:
func_num_args -- Returns the number of arguments passed to the function
Description
int func_num_args ( void )
Gets the number of arguments passed to the function.
This function may be used in conjunction with func_get_arg() and func_get_args() to allow user-defined
functions to accept variable-length argument lists.
Return Values
Returns the number of arguments passed into the current user-defined function.
3.7.8 Errors/Exceptions
Generates a warning if called from outside of a user-defined function.
Examples
Example 1. func_num_args() example
<?php
function foo()
{
$numargs = func_num_args();
72
foo(1, 2, 3);
?>
Variable functions
PHP supports the concept of variable functions. This means that if a variable name has
parentheses appended to it, PHP will look for a function with the same name as whatever the variable
evaluates to, and will attempt to execute it.
Example for Variable function example
<?php
function foo() {
echo "In foo()<br />\n";
}
function bar($arg = '')
{
echo "In bar(); argument was '$arg'.<br />\n";
}
// This is a wrapper function around echo
function echoit($string)
{
echo $string;
}
$func = 'foo';
$func();
$func = 'bar';
$func('test'); // This calls bar()
$func = 'echoit';
$func('test'); // This calls echoit()?>
Arrays:
What is an Array?
A variable is a storage area holding a number or text. The problem is, a variable will hold only one value.
Open Source Software
73
PHP array are useful to store multiple data using a single variable. Using index of the PHP array we can access
the stored variables. Indexes to an array element can either a number or a string. An array is a list variables.
Each item in an array is commonly known as element of the array and can be accessed directly via its index.
If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
$cars1="Saab";
$cars2="Volvo";
$cars3="BMW";
An array can hold all your variable values under a single name. And you can access the values by referring to
the array name.
Each element in the array has its own index so that it can be easily accessed.
Types :
In PHP, there are three kind of arrays:
Numeric array - An array with a numeric index
Associative array - An array where each ID key is associated with a value
Multidimensional array - An array containing one or more arrays
Numeric Arrays
A numeric array stores each array element with a numeric index.
There are two methods to create a numeric array.
1. In the following example the index are automatically assigned (the index starts at 0):
$cars=array("Saab","Volvo","BMW","Toyota");
2. In the following example we assign the index manually:
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
Open Source Software
$cars[3]="Toyota";
Example
In the following example you access the variable values by referring to the array name and index:
<?php
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
?>
The code above will output:
Saab and Volvo are Swedish cars.
Associative Arrays:
An associative array, each ID key is associated with a value.
When storing data about specific named values, a numerical array is not always the best way to do it.
With associative arrays we can use the values as keys and assign values to them.
Example 1
In this example we use an array to assign ages to the different persons:
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
Example 2
This example is the same as example 1, but shows a different way of creating the array:
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
The ID keys can be used in a script:
<?php
$ages['Peter'] = "32";
Open Source Software
74
75
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
echo "Peter is " . $ages['Peter'] . " years old.";
?>
The code above will output:
Peter is 32 years old.
Multidimensional Arrays
In a multidimensional array, each element in the main array can also be an array. And each element in the subarray can be an array, and so on.
Example
In this example we create a multidimensional array, with automatically assigned ID keys:
$families = array
(
"Griffin"=>array
(
"Peter",
"Lois",
"Megan"
),
"Quagmire"=>array
(
"Glenn"
),
"Brown"=>array
(
"Cleveland",
"Loretta",
"Junior"
)
);
The array above would look like this if written to the output:
Array
(
Open Source Software
76
OOP:
Object oriented programming is a concept that was created because of the need to overcome the problems
that were found with using structured programming techniques. While structured programming uses an
approach which is top down, OOP uses an approach which is bottom up.
Main Principal of OOPS are
1. Encapsulation : binding the data and function in a unit called class is called a data encapsulation. this
allow the user to hide the information for outside world and doesn't allow the other user to change or
modify the internal values of class.
2. Polymorphism: polymorphism is another strong feature of OOPS. it means one term in many forms.
3. Inheritance: another strong property of OOPS. this feature offers to derive a new class from an existing
one and acquire all the feature of the existing class the new class which get the feature from existing
class called derived class and other class is called base class.
Creating Classes:
Basic class definitions begin with the keyword class, followed by a class name, followed by a pair of curly
braces which enclose the definitions of the properties and methods belonging to the class.
The class name can be any valid label which is a not a PHP reserved word. A valid class name starts with a
Open Source Software
77
letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it
would be expressed thus: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.
A class may contain its own constants, variables (called "properties"), and functions (called "methods").
Example for Simple Class Definition:
<?php
class SimpleClass
{
// property declaration
public $var = 'a default value';
// method declaration
public function displayVar() {
echo $this->var;
}
}
?>
Creating Objects:
Its much easier to create objects and use them than it is to define object classes, so
before we discuss how to define classes. To create an object of a given class, use the new keyword:
$object = new Class;
Assuming that a Person class has been defined, heres how to create a Person object:
$rasmus = new Person;
Do not quote the class name, or youll get a compilation error:
$rasmus = new 'Person'; // does not work
Some classes permit you to pass arguments to the new call. The classs documentation
should say whether it accepts arguments. If it does, youll create objects like
this:
$object = new Person('Fred', 35);
The class name does not have to be hardcoded into your program. You can supply
78
79
80
10.strncpy:
Prototype: int strncmp(char *str1, const char *str2, int n)
This function compares first n characters of str1 with str2, it returns 0 if compare was successful.
11.stricmp:
Prototype: int stricmp(const char *str1, const char *str2)
It compares two strings str1 and str2 without regard to case and returns 0 on being successful.
12.strnicmp:
Prototype: int strnicmp(const char *str1, const char *str2, int n)
This function compares first n characters of str1 with str2 without regard to case.
13.strrev:
Prototype: char *strrev(char *)
This function reverses the given string and returns it.
14.stcstr:
Prototype: char *strstr(const char *str1, const char *str2)
This function returns the pointer to where the first occurrence of string str2 is found inside str1.
81
<?
$filename = "delete.htm"; // This is at root of the file using this script.
$fd = fopen ($filename, "r"); // opening the file in read mode
$contents = fread ($fd, filesize($filename)); // reading the content of the file
fclose ($fd);
// Closing the file pointer
echo $contents;
// printing the file content of the file
?>
File Write
We can write to a file by using fwrite() function PHP. We have to open the file in write mode and if
write permission is there then only we can open it in write mode. If the file does not exist then one new file
will be created.
Exmple:
<?
$body_content="This is my content"; // Store some text to enter inside the file
$file_name="test_file.txt";
// file name
$fp = fopen ($file_name, "w");
// Open the file in write mode, if file does not exist then it will be created.
fwrite ($fp,$body_content);
// entering data to the file
fclose ($fp);
// closing the file pointer
chmod($file_name,0777);
// changing the file permission.
?>
PHP File delete
using unlink function
We can delete files by giving its URL or path in PHP by using unlink command. This command will work
only if write permission is given to the folder or file. Without this the delete command will fail. Here is the
command to delete the file.
unlink($path);
Here $path is the relative path of the file calculated from the script execution. Here is an example of
deleting file by using relative path
$path="images/all11.css";
if(unlink($path)) echo "Deleted file ";
We have used if condition to check whether the file delete command is successful or not. But the command
below will not work.
$path="http://domainname/file/red.jpg";
if(unlink($path)) echo "Deleted file ";
The warning message will say unlink() [function.unlink]: HTTP does not allow unlinking.
We can suppress the warning message by adding a @ symbol before the unlink command .
Open Source Software
82
Based on the success of the file delete command we can further execute our code by using if else code block.
$path="test.html";
if(@unlink($path)) {echo "Deleted file "; }
else{echo "File can't be deleted";}
file_exists function
In our script before executing a program we need to check if file exists or not. This file check method by using
file_exists function check the presence of file from relative path we specify and then returns TRUE of False
based on the presence of the file.
We can best use file_exists function along with an if condition.
Here is a sample code.
$add="images/119.jpg";
if(file_exists($add)){
echo "Yes file is there ";
}else{echo "Sorry no file at $add ";}
Deleting all files of a directory
Now let us try to develop a function and to this function we will post directory name as parameter and the
function will use unlink command to remove files by looping through all the files of the directory.
Here is the code to this.
function EmptyDir($dir) {
$handle=opendir($dir);
while (($file = readdir($handle))!==false) {
echo "$file <br>";
@unlink($dir.'/'.$file);
}
closedir($handle);
}
EmptyDir('images');
Example:
Create an Upload-File Form:
To allow users to upload files from a form can be very useful.
Look at the following HTML form for uploading files:
<html>
<body>
Open Source Software
83
The enctype attribute of the <form> tag specifies which content-type to use when submitting the form.
"multipart/form-data" is used when a form requires binary data, like the contents of a file, to be
uploaded
The type="file" attribute of the <input> tag specifies that the input should be processed as a file. For
example, when viewed in a browser, there will be a browse-button next to the input field
Note: Allowing users to upload files is a big security risk. Only permit trusted users to perform file uploads.
PHP File Upload:
Create The Upload Script
The "upload_file.php" file contains the code for uploading a file:
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>
By using the global PHP $_FILES array you can upload files from a client computer to the remote server.
The first parameter is the form's input name and the second index can be either "name", "type", "size",
Open Source Software
84
This is a very simple way of uploading files. For security reasons, you should add restrictions on what the user
is allowed to upload.
Saving the Uploaded File:
The examples above create a temporary copy of the uploaded files in the PHP temp folder on the server.
The temporary copied files disappears when the script ends. To store the uploaded file we need to copy it to a
different location:
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
Open Source Software
}
else
{
echo "Invalid file";
}
?>
The script above checks if the file already exists, if it does not, it copies the file to the specified folder.
PHP directory listing:
Many times we have to display the list of files in a directory. We can keep the script in any location and by
using the file path we can display the files inside the directory.
// open the current directory by opendir
$handle=opendir(".");
while (($file = readdir($handle))!==false) {
echo "$file <br>";
}
closedir($handle);
85
86
UNIT V
PHP and SQL database
PHP and SQL database PHP and LDAP PHP Connectivity Sending and receiving E-mails Debugging and error
handling Security Templates.
Parameter
Description
Servername
Username
Optional. Specifies the username to log in with. Default value is the name of the user
that owns the server process
Password
Note: There are more available parameters, but the ones listed above are the most important.
Example
In the following example we store the connection in a variable ($con) for later use in the script. The "die" part
will be executed if the connection fails:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
Open Source Software
87
// some code
?>
Closing a Connection:
The connection will be closed automatically when the script ends. To close the connection before, use the
mysql_close() function:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
mysql_close($con);
?>
Create a Database:
The CREATE DATABASE statement is used to create a database in MySQL.
Syntax
CREATE DATABASE database_name
To get PHP to execute the statement above we must use the mysql_query() function. This function is used to
send a query or command to a MySQL connection.
Example
The following example creates a database called "my_db":
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_query("CREATE DATABASE my_db",$con))
{
Open Source Software
88
89
{
echo "Error creating database: " . mysql_error();
}
// Create table
mysql_select_db("my_db", $con);
$sql = "CREATE TABLE Persons
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";
// Execute query
mysql_query($sql,$con);
mysql_close($con);
?>
Insert Data into a Database Table:
The INSERT INTO statement is used to add new records to a database table.
Syntax
It is possible to write the INSERT INTO statement in two forms.
The first form doesn't specify the column names where the data will be inserted, only their values:
INSERT INTO table_name
VALUES (value1, value2, value3,...)
The second form specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
To get PHP to execute the statements above we must use the mysql_query() function. This function is used to
send a query or command to a MySQL connection.
Example
In the previous chapter we created a table named "Persons", with three columns; "Firstname", "Lastname" and
"Age". We will use the same table in this example. The following example adds two new records to the
"Persons" table:
Open Source Software
90
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin', '35')");
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Glenn', 'Quagmire', '33')");
mysql_close($con);
?>
Select Data From a Database Table
The SELECT statement is used to select data from a database.
Syntax
SELECT column_name(s)
FROM table_name
To get PHP to execute the statement above we must use the mysql_query() function. This function is used to
send a query or command to a MySQL connection.
Example
The following example selects all the data stored in the "Persons" table (The * character selects all the data in
the table):
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
91
92
configuration option when compiling PHP to enable LDAP support. DIR is the LDAP base install directory.
To enable SASL support, be sure --with-ldap-sasl[=DIR] is used, and that sasl.h exists on the system.
Predefined Constants:
The constants below are defined by this extension, and will only be available when the extension has either
been compiled into PHP or dynamically loaded at runtime.
LDAP_DEREF_NEVER (integer)
LDAP_DEREF_SEARCHING (integer)
LDAP_DEREF_FINDING (integer)
LDAP_DEREF_ALWAYS (integer)
LDAP_OPT_DEREF (integer)
Using the PHP LDAP calls:
Before you can use the LDAP calls you will need to know ..
The "base dn" of the server (the part of the world directory that is held on this server, which could be
"o=My Company,c=US")
Whether you need a password to access the server (many servers will provide read access for an
"anonymous bind" but require a password for anything else)
The typical sequence of LDAP calls you will make in an application will follow this pattern:
|
do something like search or update the directory
and display the results
|
ldap_close()
// "logout"
LDAP Functions:
ldap_8859_to_t61 Translate 8859 characters to t61 characters
ldap_add Add entries to LDAP directory
ldap_bind Bind to LDAP directory
Open Source Software
93
PHP Connectivity:
Example Program
<?php
// basic sequence with LDAP is connect, bind, search, interpret search
// result, close connection
echo "<h3>LDAP query test</h3>";
echo "Connecting ...";
$ds=ldap_connect("localhost"); // must be a valid LDAP server!
echo "connect result is " . $ds . "<br />";
if ($ds) {
echo "Binding ...";
$r=ldap_bind($ds);
94
}
?>
Description
bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]] )
Sends an email.
Parameters
to
Receiver, or receivers of the mail.
The formatting of this string must comply with RFC 2822. Some examples are:
user@example.com
user@example.com, anotheruser@example.com
User <user@example.com>
User <user@example.com>, Another User <anotheruser@example.com>
subject
Subject of the email to be sent.
Return Values
Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will
actually reach the intended destination.
PHP Code(Server side):
Example for Sending mail.
Using mail() to send a simple email:
<?php
// The message
= 'nobody@example.com';
95
96
97
type
Type of line. Tells the receiving program about what it should treat the following data as:
3.16.1.2 Debugger Error Types
PHP 3 Internal
Debugger
warning
E_WARNING
error
E_ERROR
parse
E_PARSE
notice
E_NOTICE
core-error
E_CORE_ERROR
core-warning E_CORE_WARNING
unknown
(any other)
98
99
--enable-sigchild
Enable PHP's own SIGCHLD handler.
PHP options
--enable-maintainer-mode
Enable make rules and dependencies not useful (and sometimes confusing) to the casual installer.
--with-config-file-path=PATH
Sets the path in which to look for php.ini, defaults to PREFIX/lib.
--enable-safe-mode
Enable safe mode by default.
--with-exec-dir[=DIR]
Only allow executables in DIR when in safe mode defaults to /usr/local/php/bin.
SAPI options
The following list contains the available SAPI&s (Server Application Programming Interface) for
PHP.
--with-aolserver=DIR
Specify path to the installed AOLserver.
--with-apxs[=FILE]
Build shared Apache module. FILE is the optional pathname to the Apache apxs tool; defaults to
apxs. Make sure you specify the version of apxs that is actually installed on your system and NOT
the one that is in the apache source tarball.
--with-apache[=DIR]
Build a static Apache module. DIR is the top-level Apache build directory, defaults to
/usr/local/apache.
--with-mod_charset
Enable transfer tables for mod_charset (Russian Apache).
--with-apxs2[=FILE]
Build shared Apache 2.0 module. FILE is the optional pathname to the Apache apxs tool; defaults to
apxs.
Error Handling:
Open Source Software
100
The default error handling in PHP is very simple. An error message with filename, line number and a
message describing the error is sent to the browser.
101
$file=fopen("welcome.txt","r");
}
?>
Now if the file does not exist you get an error like this:
File not found
The code above is more efficient than the earlier code, because it uses a simple error handling mechanism to
stop the script after the error.
However, simply stopping the script is not always the right way to go. Let's take a look at alternative PHP
functions for handling errors.
Creating a Custom Error Handler:
Creating a custom error handler is quite simple. We simply create a special function that can be called when
an error occurs in PHP.
This function must be able to handle a minimum of two parameters (error level and error message) but can
accept up to five parameters (optionally: file, line-number, and the error context):
Syntax
error_function(error_level,error_message,
error_file,error_line,error_context)
Parameter
Description
error_level
Required. Specifies the error report level for the user-defined error. Must be a value number.
See table below for possible error report levels
error_message
error_file
error_line
102
error_context
Optional. Specifies an array containing every variable, and their values, in use when the error
occurred
Description
E_WARNING
E_NOTICE
Run-time notices. The script found something that might be an error, but
could also happen when running a script normally
256
E_USER_ERROR
512
E_USER_WARNING
1024 E_USER_NOTICE
4096 E_RECOVERABLE_ERROR
Catchable fatal error. This is like an E_ERROR but can be caught by a user
defined handle (see also set_error_handler())
8191 E_ALL
All errors and warnings, except level E_STRICT (E_STRICT will be part of
E_ALL as of PHP 6.0)
103
The code above is a simple error handling function. When it is triggered, it gets the error level and an error
message. It then outputs the error level and message and terminates the script.
Now that we have created an error handling function we need to decide when it should be triggered.
Set Error Handler:
The default error handler for PHP is the built in error handler. We are going to make the function above the
default error handler for the duration of the script.
It is possible to change the error handler to apply for only some errors, that way the script can handle
different errors in different ways. However, in this example we are going to use our custom error handler for
all errors:
set_error_handler("customError");
Since we want our custom function to handle all errors, the set_error_handler() only needed one parameter, a
second parameter could be added to specify an error level.
Example
Testing the error handler by trying to output variable that does not exist:
<?php
//error handler function
function customError($errno, $errstr)
{
echo "<b>Error:</b> [$errno] $errstr";
}
//set error handler
set_error_handler("customError");
//trigger error
echo($test);
?>
The output of the code above should be something like this:
Error: [8] Undefined variable: test
Open Source Software
104
Trigger an Error:
In a script where users can input data it is useful to trigger errors when an illegal input occurs. In PHP, this is
done by the trigger_error() function.
Example
In this example an error occurs if the "test" variable is bigger than "1":
<?php
$test=2;
if ($test>1)
{
trigger_error("Value must be 1 or below");
}
?>
The output of the code above should be something like this:
Notice: Value must be 1 or below
in C:\webfolder\test.php on line 6
An error can be triggered anywhere you wish in a script, and by adding a second parameter, you can specify
what error level is triggered.
Possible error types:
E_USER_ERROR - Fatal user-generated run-time error. Errors that can not be recovered from.
Execution of the script is halted
E_USER_NOTICE - Default. User-generated run-time notice. The script found something that might
be an error, but could also happen when running a script normally
Example
In this example an E_USER_WARNING occurs if the "test" variable is bigger than "1". If an E_USER_WARNING occurs
we will use our custom error handler and end the script:
Open Source Software
105
<?php
//error handler function
function customError($errno, $errstr)
{
echo "<b>Error:</b> [$errno] $errstr<br />";
echo "Ending Script";
die();
}
//trigger error
$test=2;
if ($test>1)
{
trigger_error("Value must be 1 or below",E_USER_WARNING);
}
?>
The output of the code above should be something like this:
Error: [512] Value must be 1 or below
Ending Script
Security:
PHP is a powerful language and the interpreter, whether included in a web server as a module or
executed as a separate CGI binary, is able to access files, execute commands and open network connections
on the server. These properties make anything run on a web server insecure by default.
PHP is designed specifically to be a more secure language for writing CGI programs than Perl or C, and
with correct selection of compile-time and runtime configuration options, and proper coding practices, it can
give you exactly the combination of freedom and security.
Installed as CGI binary:
106
Possible attacks:
Using PHP as a CGI binary is an option for setups that for some reason do not wish to integrate PHP
as a module into server software (like Apache), or will use PHP with different kinds of CGI wrappers to
create safe chroot and setuid environments for scripts. Even if the PHP binary can be used as a standalone
interpreter, PHP is designed to prevent the attacks this setup makes possible:
Accessing system files: http://my.host/cgi-bin/php?/etc/passwd
Accessing any web document on server: http://my.host/cgi-bin/php/secret/doc.html
Case 1: only public files served
If your server does not have any content that is not restricted by password or ip based access control,
there is no need for these configuration options. If your web server does not allow you to do redirects, or the
server does not have a way to communicate to the PHP binary that the request is a safely redirected request,
you can specify the option --enable-force-cgi-redirect to the configure script.
Case 2: using cgi.force_redirect
This compile-time option prevents anyone from calling PHP directly with a URL like
http://my.host/cgi-bin/php/secretdir/script.php. Instead, PHP will only parse in this mode if it has gone
through a web server redirect rule.
Case 3: setting doc_root or user_dir
To include active content, like scripts and executables, in the web server document directories is
sometimes considered an insecure practice. If, because of some configuration mistake, the scripts are not
executed but displayed as regular HTML documents, this may result in leakage of intellectual property or
security information like passwords. Therefore many sysadmins will prefer setting up another directory
structure for scripts that are accessible only through the PHP CGI, and therefore always interpreted and not
displayed as such.
Case 4: PHP parser outside of web tree
A very secure option is to put the PHP parser binary somewhere outside of the web tree of files. In
/usr/local/bin, for example.
107
#!/usr/local/bin/php
as the first line of any file containing PHP tags.
Installed as an Apache module
When PHP is used as an Apache module it inherits Apache's user permissions (typically those of the
"nobody" user). This has several impacts on security and authorization. For example, if you are using PHP
to access a database, unless that database has built-in access control, you will have to make the database
accessible to the "nobody" user.
File system Security
PHP is subject to the security built into most server systems with respect to permissions on a file and
directory basis. This allows you to control which files in the filesystem may be read.
Consider the following script, where a user indicates that they'd like to delete a file in their home
directory. This assumes a situation where a PHP web interface is regularly used for file management, so the
Apache user is allowed to delete files in the user home directories.
unlink("$homedir/$userfile"); // "/home/../etc/passwd"
108
There are two important measures you should take to prevent these issues.
Only allow limited permissions to the PHP web user binary.
Check all variables which are submitted.
Null bytes related issues:
As PHP uses the underlying C functions for filesystem related operations, it may handle null bytes in
a quite unexpected way. As null bytes denote the end of a string in C, strings containing them won't be
considered entirely but rather only until a null byte occurs. The following example shows a vulnerable code
that demonstrates this problem:
Script vulnerable to null bytes
<?php
$file = $_GET['file']; // "../../etc/passwd\0"
if (file_exists('/home/wwwrun/'.$file.'.php')) {
// file_exists will return true as the file /home/wwwrun/../../etc/passwd exists
include '/home/wwwrun/'.$file.'.php';
// the file /etc/passwd will be included
}
?>
Database Security:
Nowadays, databases are cardinal components of any web based application by enabling websites to
provide varying dynamic content. Since very sensitive or secret information can be stored in a database, you
should strongly consider protecting your databases.
Designing Databases:
The first step is always to create the database, unless you want to use one from a third party. When a
database is created, it is assigned to an owner, who executed the creation statement. Usually, only the owner
(or a superuser) can do anything with the objects in that database, and in order to allow other users to use it,
privileges must be granted.
Applications should never connect to the database as its owner or a superuser, because these users
can execute any query at will, for example, modifying the schema (e.g. dropping tables) or deleting its entire
content
Connecting to Database:
Open Source Software
109
You may want to establish the connections over SSL to encrypt client/server communications for
increased security, or you can use ssh to encrypt the network connection between clients and the database
server.
Encrypted Storage Model:
SSL/SSH protects data travelling from the client to the server, SSL/SSH does not protect the
persistent data stored in a database. SSL is an on-the-wire protocol.
Once an attacker gains access to your database directly (bypassing the webserver), the stored
sensitive data may be exposed or misused, unless the information is protected by the database itself.
Encrypting the data is a good way to mitigate this threat, but very few databases offer this type of data
encryption.
SQL Injection:
SQL injection is a code injection technique that exploits a security vulnerability occurring in the
database layer of an application. The vulnerability is present when user input is either incorrectly filtered for
string literal escape characters embedded in SQL statements or user input is not strongly typed and thereby
unexpectedly executed.
Preventing SQL injection
To protect against SQL injection, user input must not directly be embedded in SQL statements.
Instead, parameterized statements must be used (preferred), or user input must be carefully escaped or
filtered.
Parameterized statements
With most development platforms, parameterized statements can be used that work with parameters
(sometimes called placeholders or bind variables) instead of embedding user input in the statement.
Enforcement at the database level
Currently only the H2 Database Engine supports the ability to enforce query parameterization.
Enforcement at the coding level
Using object-relational mapping libraries avoids the need to write SQL code.
Escaping
A straight-forward, though error-prone, way to prevent injections is to escape characters that have a
special meaning in SQL.
Error Reporting:
Open Source Software
110
With PHP security, there are two sides to error reporting. One is beneficial to increasing security, the
other is detrimental.
A standard attack tactic involves profiling a system by feeding it improper data, and checking for the
kinds, and contexts, of the errors which are returned.
Attacking Variables with a custom HTML page
<form method="post" action="attacktarget?username=badfoo&password=badfoo">
<input type="hidden" name="username" value="badfoo" />
<input type="hidden" name="password" value="badfoo" />
</form>
Exploiting common debugging variables
<form method="post" action="attacktarget?errors=Y&showerrors=1&debug=1">
<input type="hidden" name="errors" value="Y" />
<input type="hidden" name="showerrors" value="1" />
<input type="hidden" name="debug" value="1" />
</form>
Using Register Globals:
The register_globals setting controls how you access form, server, and environment variables. By
default this variable is set to Off, requiring you to use special arrays to access these variables.
When on, register_globals will inject your scripts with all sorts of variables, like request variables
from HTML forms. This coupled with the fact that PHP doesn't require variable initialization means writing
insecure code is that much easier. It was a difficult decision, but the PHP community decided to disable this
directive by default. When on, people use variables yet really don't know for sure where they come from and
can only assume. Internal variables that are defined in the script itself get mixed up with request data sent by
users and disabling register_globals changes this.
User Submitted Data:
111
The greatest weakness in many PHP programs is not inherent in the language itself, but merely an
issue of code not being written with security in mind. For this reason, you should always take the time to
consider the implications of a given piece of code, to ascertain the possible damage if an unexpected
variable is submitted to it.
Magic Quotes:
Magic Quotes is a process that automagically escapes incoming data to the PHP script. It's preferred
to code with magic quotes off and to instead escape the data at runtime, as needed.
What are Magic Quotes
When on, all ' (single-quote), " (double quote), \ (backslash) and NULL characters are escaped with a
backslash automatically. This is identical to what addslashes() does.
There are three magic quote directives:
magic_quotes_gpc
Affects HTTP Request data (GET, POST, and COOKIE). Cannot be set at runtime, and defaults to
on in PHP.
See also get_magic_quotes_gpc().
magic_quotes_runtime
If enabled, most functions that return data from an external source, including databases and text files,
will have quotes escaped with a backslash. Can be set at runtime, and defaults to off in PHP.
See also set_magic_quotes_runtime() and get_magic_quotes_runtime().
magic_quotes_sybase
112
113
A few simple techniques can help to hide PHP, possibly slowing down an attacker who is attempting
to discover weaknesses in your system. By setting expose_php = off in your php.ini file, you reduce the
amount of information available to them.
Another tactic is to configure web servers such as apache to parse different filetypes through PHP,
either with an .htaccess directive, or in the apache configuration file itself.
Keeping Current
PHP, like any other large system, is under constant scrutiny and improvement. Each new version will
often include both major and minor changes to enhance security and repair any flaws, configuration
mishaps, and other issues that will affect the overall security and stability of your system.
Templates:
Templating is an easy way to create large sites without much trouble in maintaining a consistent look
and feel across them.In its simplest form, a template is an outline for a website. It contains all the layout and
perhaps some CSS for that purpose. For the purpose of this tutorial, we will be using a simple html layout
with a sidebar menu and a header and a main content section.
This is quite a common layout and we will be making the main content dynamic, so that by clicking
a link in the menu, the main content will be changed by including a seperate php file.
Simple PHP Template Class:
The used shows how to implement a simple class to handle page templates.
114
function publish() {
eval("?>".$this->template."<?");
}
}
?>
<html>
<head>
<title>#title#</title>
</head>
<body>
<h3>Hello #name#!</h3>
<p>The time is: #datetime#</p>
<? echo "<p>Embedded PHP works too!</p>"; ?>
</body>
</html>
Usage (index.php)
Now we will create a script that load the template and use the class to merge the data.
<?
include "template.class.php";
$template = new Template;
$template->load("design.html");
$template->replace("title", "My Template Class");
$template->replace("name", "William");
$template->replace("datetime",
date("m/d/y"));
$template->publish();
?>
When you run the above script, index.php, it will output the following:
<html>
<head>
<title>My Template Class</title>
</head>
<body>
Open Source Software
<h3>Hello William!</h3>
<p>The time is: 03/10/04</p>
<p>Embedded PHP works too!</p>
</body>
</html>
115
116
PYTHON [Unit-VI]
UNIT VI
PYTHON
Syntax and Style Python Objects Numbers Sequences Strings Lists and
Tuples Dictionaries Conditionals and Loops.
Introduction:
Python is a general purpose interpreted, interactive, object-oriented and high-level programming
language. Python was created by Guido van Rossum in the late eighties and early nineties. Like Perl, Python
source code is now available under the GNU General Public License (GPL).Python was designed to be
highly readable which uses English keywords frequently where as other languages use punctuation and it
has fewer syntactical constructions than other languages.
Python is Interpreted: This means that it is processed at runtime by the interpreter and you do not
need to compile your program before executing it. This is similar to PERL and PHP.
Python is Interactive: This means that you can actually sit at a Python prompt and interact with the
interpreter directly to write your programs.
Python is Object-Oriented: This means that Python supports Object-Oriented style or technique of
programming that encapsulates code within objects.
Python is Beginner's Language: Python is a great language for the beginner programmers and
supports the development of a wide range of applications, from simple text processing to WWW
browsers to games.
Description
Hash mark ( # )
NEWLINE ( \n )
Backslash ( \ )
Continues a line
Semicolon ( ; )
Colon ( : )
Comments:
A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and up
to the physical line end are part of the comment, and the Python interpreter ignores them.
Open Source Software
PYTHON [Unit-VI]
117
#!/usr/bin/python
# First comment
print "Hello, Python!"; # second comment
This will produce following result:
Hello, Python!
A comment may be on the same line after a statement or expression:
name = "Madisetti" # This is again comment
You can comment multiple lines as follows:
# This is a comment.
# This is a comment, too.
# This is a comment, too.
# I said that already.
Continuation ( \ ):
In Python you normally have one instruction per line. Long instructions can span several lines using
the line-continuation character \. Some instructions, as triple quoted strings, list, tuple and dictionary
constructors or statements grouped by parentheses do not need a line-continuation character. It is possible to
write several statements on the same line, provided they are separated by semi-colons.
# check conditions
if (weather_is_hot == 1) and \
(shark_warnings == 0) :
send_goto_beach_mesg_to_pager()
118
PYTHON [Unit-VI]
The semicolon ( ; ) allows multiple statements on the single line given that neither statement starts a
new code block. Here is a sample snip using the semicolon:
Example:
import sys; x = 'foo'; sys.stdout.write(x + '\n')
Module:
A module allows you to logically organize your Python code. Grouping related code into a module
makes the code easier to understand and use.
A module is a Python object with arbitrarily named attributes that you can bind and
reference.Simply, a module is a file consisting of Python code. A module can define functions, classes, and
variables. A module can also include runnable code.
Variable Assignment
Python variables do not have to be explicitly declared to reserve memory space. The declaration
happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to
variables.
The operand to the left of the = operator is the name of the variable, and the operand to the right of
the = operator is the value stored in the variable. For example:
#!/usr/bin/python
counter = 100
# An integer assignment
miles = 1000.0
# A floating point
name = "John"
# A string
print counter
print miles
print name
Here 100, 1000.0 and "John" are the values assigned to counter, miles and name variables,
respectively. While running this program, this will produce following result:
===========
Frame
These are objects representing execution stack frames in Python. Frame objects contain
all the information the Python interpreter needs to know during a runtime execution
environment. Some of its attributes include a link to the previous stack frame, the code
object (see above) that is being executed, dictionaries for the local and global namespaces,
Open Source Software
PYTHON [Unit-VI]
119
and the current instruction. Each function call results in a new frame object, and for each
frame object, a C stack frame is created as well.
Traceback
When you make an error in Python, an exception is raised. If exceptions are not caught or
"handled," the interpreter exits with some diagnostic information similar to the output
=========
shown below:
Traceback (innermost last):
File "<stdin>", line N?, in ???
ErrorName: error reason
The traceback object is just a data item that holds the stack trace information for an
exception and is created when an exception occurs. If a handler is provided for an
exception, this handler is given access to the traceback object.
Slice Objects
Slice objects are used to represent slices when extended slice syntax is used. This is a slice using two
colons, or multiple slices or ellipses separated by commas, e.g., a[i:j:step], a[i:j, k:l], or a[...,
i:j].
120
PYTHON [Unit-VI]
Its interpretation is purely up to whatever implements the __getitem__ function and sees Ellipsis
objects there, but its main (and intended) use in in the numeric python extension, which adds a
multidementional array type. Since there are more than one dimensions, slicing becomes more complex than
just a start and stop index; it is useful to be able to slice in multiple dimentions as well. eg, given a 4x4
array, the top left area would be defined by the slice "[:2,:2]"
>>> a
array([ [ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[ 9, 10, 11, 12],
[13, 14, 15, 16] ])
Example
121
PYTHON [Unit-VI]
==
(a == b) is not true.
<>
or not, if values are not equal then condition (a <> b) is true. This is similar to != operator.
becomes true.
Checks if the value of left operand is greater
>
than the value of right operand, if yes then (a > b) is not true.
condition becomes true.
Checks if the value of left operand is less than
<
>=
<=
types, never return "equal". Some types (e.g., file objects) support only a degenerate notion of comparison
where any two objects of that type are non-equal -- equality can only be established by comparing the
contents. The <, <=, > and >= operators raise a TypeError when an operand is a complex number.
Instances of a class usually compare as non-equal unless the class defines the __cmp__() method.
Implementation note: Objects of different types (except numbers) are ordered by their type names. Objects
of the same type that don't support proper comparison are ordered by their address in memory (RAM).
Two more comparison operations, "in" and "not in", are granted the same syntactic priority but are
supported only by sequence types.
PYTHON [Unit-VI]
122
cmp()
This function compares elements of two tuples.
Syntax:
cmp(tuple1,tuple2)
Or
cmp(obj1,obj2)
Parameters:
Here is the detail of parameters:
Return Value:
If elements are of the same type, perform the compare and return the result. If elements are different types,
check to see if they are numbers.
If either element is a number, then the other element is "larger" (numbers are "smallest").
If we reached the end of one of the tuples, the longer tuple is "larger." If we exhaust both tuples and share
the same data, the result is a tie, meaning that 0 is returned.
Example:
#!/usr/bin/python
123
PYTHON [Unit-VI]
Return a string containing a nicely printable representation of an object. For strings, this returns the
string itself. The difference with repr(object) is that str(object) does not always attempt to return a
string that is acceptable to eval(); its goal is to return a printable string. If no argument is given, returns the
empty string, ''.
The str function coerces data into a string.
Every datatype can be coerced into a string.
str(1)
horsemen = ['war', 'pestilence', 'famine']
print horsemen
horsemen.append('Powerbuilder')
print str(horsemen)
Description
Result
Cmp(pbj1,obj2)
integer
repr(obj)
string representation
string
str(obj)
string representation
String
type(obj)
type object
Operator
Description
Result
<
less than
Boolean
>
greater than
Boolean
<=
Boolean
>=
Boolean
==
equal to
Boolean
!=
not equal to
Boolean
<>
not equal to
Boolean
Function
Description
Result
is
the same as
Boolean
is not
Boolean
Value Comparisons
Object Comparisons
124
PYTHON [Unit-VI]
Boolean operators
Function
Description
Result
not
logical negation
Boolean
And
logical conjuction
Boolean
or
logical disjunction
Boolean
Unsupported Types
A list of types that are not supported by Python.
Boolean
Unlike Pascal or Java, Python does not feature the Boolean type. Use integers instead.
char or byte
Python does not have a char or byte type to hold either single character or 8-bit integers.
Use strings of length one for characters and integers for 8-bit numbers.
pointer
Since Python manages memory for you, there is no need to access pointer addresses. The
closest to an address that you can get in Python is by looking at an object's identity using the id() built-in
function. Since you have no control over this value, it's a moot point.
int vs. short vs. long
Python's plain integers are the universal "standard" integer type, obviating the need for
three different integer types, i.e., C's int, short, and long. For the record, Python's
integers are implemented as C longs. For values larger in magnitude than regular integers
(usually your system architecture size, i.e., 32-bit), use Python's long integer.
float vs. double
C has both a single precision float type and double-precision double type. Python's
float type is actually a C double. Python does not support a single-precision floating
point type because its benefits are outweighed by the overhead required to support two
types of floating point types.
Numbers:
Number data types store numeric values. They are immutable data types, which means that changing
the value of a number data type results in a newly allocated object.Number objects are created when you
assign a value to them. For example:
var1 = 1
Open Source Software
PYTHON [Unit-VI]
125
var2 = 10
You can also delete the reference to a number object by using the del statement. The syntax of the del
statement is:
del var1[,var2[,var3[....,varN]]]]
You can delete a single object or multiple objects by using the del statement. For example:
del var
del var_a, var_b
How to Create and Assign Numbers (Number Objects):
Creating numbers is as simple as assigning a value to a variable:
anInt = 1
1aLong = -9999999999999999L
aFloat = 3.1415926535897932384626433832795
aComplex = 1.23 + 4.56J
How to Update Numbers:
You can "update" an existing number by (re)assigning a variable to another number. The
new value can be related to its previous value or to a completely different number
altogether.
anInt = anInt + 1
aFloat = 2.718281828
How to Remove Numbers:
Under normal circumstances, you do not really "remove" a number; you just stop using it! If you
really want to delete a reference to a number object, just use the del statement
You can no longer use the variable name, once removed, unless you assign it to a new object; otherwise, you
will cause a NameError exception to occur.
del anInt
del aLong, aFloat, aComplex
Types:
Python supports four different numerical types:
o
int (signed integers): often called just integers or ints, are positive or negative whole numbers with
no decimal point.
long (long integers ): or longs, are integers of unlimited size, written like integers and followed by
an uppercase or lowercase L.
126
PYTHON [Unit-VI]
float (floating point real values) : or floats, represent real numbers and are written with a decimal
point dividing the integer and fractional parts. Floats may also be in scientific notation, with E or e
indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).
complex (complex numbers) : are of the form a + bJ, where a and b are floats and J (or j) represents
the square root of -1 (which is an imaginary number). a is the real part of the number, and b is the
imaginary part. Complex numbers are not used much in Python programming.
Examples:
Here are some examples of numbers:
Int
long
float
complex
10
51924361L
0.0
3.14j
100
-0x19323L
15.20
45.j
-786
0122L
-21.9
9.322e-36j
080
0xDEFABCECBDAECBFBAEl
32.3+e18
.876j
-0490
535633629843L
-90.
-.6545+0J
-0x260 -052318172735L
-32.54e100
3e+26J
0x69
70.2-E12
4.53e-7j
-4721885298529L
Python allows you to use a lowercase L with long, but it is recommended that you use only an
uppercase L to avoid confusion with the number 1. Python displays long integers with an uppercase
L.
A complex number consists of an ordered pair of real floating point numbers denoted by a + bj,
where a is the real part and b is the imaginary part of the complex number.
127
PYTHON [Unit-VI]
Type complex(x) to convert x to a complex number with real part x and imaginary part zero.
Type complex(x, y) to convert x and y to a complex number with real part x and imaginary part y. x
and y are numeric expressions
Returns ( description )
abs(x)
ceil(x)
cmp(x, y)
-1 if x < y, 0 if x == y, or 1 if x > y
exp(x)
The exponential of x: ex
fabs(x)
floor(x)
log(x)
log10(x)
max(x1, x2,...)
min(x1, x2,...)
modf(x)
pow(x, y)
round(x [,n])
sqrt(x)
The fractional and integer parts of x in a two-item tuple. Both parts have the
same sign as x. The integer part is returned as a float.
The value of x**y.
x rounded to n digits from the decimal point. Python rounds away from zero as a
tie-breaker: round(0.5) is 1.0 and round(-0.5) is -1.0.
The s
128
PYTHON [Unit-VI]
seed([x])
Returns ( description )
A random item from a list, tuple, or string.
A random float r, such that 0 is less than or equal to r and r is less than 1
Sets the integer starting value used in generating random numbers. Call this
function before calling any other random module function. Returns None.
shuffle(lst)
uniform(x, y)
A random float r, such that x is less than or equal to r and r is less than y
Trigonometric Functions:
Python includes following functions that perform trigonometric calculations.
Function
Description
acos(x)
asin(x)
atan(x)
atan2(y, x)
cos(x)
hypot(x, y)
sin(x)
tan(x)
degrees(x)
129
PYTHON [Unit-VI]
radians(x)
Mathematical Constants:
The module also defines two mathematical constants:
Constant
Description
Pi
Python Sequences:
The most basic data structure in Python is the sequence. Each element of a sequence is assigned a
number - its position, or index. The first index is zero, the second index is one, and so forth.
Python has six built-in types of sequences. There are certain things you can do with all sequence types. These
operations include indexing, slicing, adding, multiplying, and checking for membership. In addition, Python has builtin functions for finding the length of a sequence, and for finding its largest and smallest elements.
Strings:
Strings are amongst the most popular types in Python. We can create them simply by enclosing
characters in quotes. Python treats single quotes the same as double quotes.
Creating strings is as simple as assigning a value to a variable. For example:
var1 = 'Hello World!'
var2 = "Python Programming"
Accessing Values in Strings:
Python does not support a character type; these are treated as strings of length one, thus also
considered a substring.
To access substrings, use the square brackets for slicing along with the index or indices to obtain your
substring:
Example:
#!/usr/bin/python
var1 = 'Hello World!'
var2 = "Python Programming"
print "var1[0]: ", var1[0]
Open Source Software
130
PYTHON [Unit-VI]
Hexadecimal
notation
character
\a
0x07
Bell or alert
\b
0x08
Backspace
\cx
Control-x
\C-x
Control-x
\e
0x1b
Escape
\f
0x0c
Formfeed
\M-\C-x
Meta-Control-x
Description
131
PYTHON [Unit-VI]
\n
0x0a
Newline
\nnn
\r
0x0d
Carriage return
\s
0x20
Space
\t
0x09
Tab
\v
0x0b
Vertical tab
\x
Character x
\xnn
[]
[:]
In
not in
Example
H in a will give 1
132
PYTHON [Unit-VI]
r/R
Conversion
%c
character
%s
%i
%d
%u
%o
octal integer
%x
133
PYTHON [Unit-VI]
%X
%e
%E
%f
%g
%G
Other supported symbols and functionality are listed in the following table:
Symbol
Functionality
left justification
<sp>
add the octal leading zero ( '0' ) or hexadecimal leading '0x' or '0X',
depending on whether 'x' or 'X' were used.
(var)
m.n.
m is the minimum total width and n is the number of digits to display after
the decimal point (if appl.)
Triple Quotes:
Python's triple quotes comes to the rescue by allowing strings to span multiple lines, including verbatim
NEWLINEs, TABs, and any other special characters.
The syntax for triple quotes consists of three consecutive single or double quotes.
PYTHON [Unit-VI]
134
#!/usr/bin/python
para_str = """this is a long string that is made up of
several lines and non-printable characters such as
TAB ( \t ) and they will show up that way when displayed.
NEWLINEs within the string, whether explicitly given like
this within the brackets [ \n ], or just a NEWLINE within
the variable assignment will also show up.
"""
print para_str;
Note how every single special character has been converted to its printed form, right down to the last
NEWLINE at the end of the string between the "up." and closing triple quotes. Also note that NEWLINEs
occur either with an explicit carriage return at the end of a line or its escape code (\n):
this is a long string that is made up of
several lines and non-printable characters such as
TAB (
print 'C:\\nowhere'
This would print following result:
C:\nowhere
Now let's make use of raw string. We would put expression in r'expression' as follows:
#!/usr/bin/python
print r'C:\\nowhere'
This would print following result:
Open Source Software
135
PYTHON [Unit-VI]
C:\\nowhere
Unicode String
Normal strings in Python are stored internally as 8-bit ASCII, while Unicode strings are stored as 16bit Unicode. This allows for a more varied set of characters, including special characters from most
languages in the world. I'll restrict my treatment of Unicode strings to the following:
#!/usr/bin/python
print u'Hello, world!'
This would print following result:
Hello, world!
As you can see, Unicode strings use the prefix u, just as raw strings use the prefix r.
Built-in String Methods
Python includes following string method:
SN
1
center(width, fillchar)
of string if starting index beg and ending index end are given
decode(encoding='UTF-8',errors='strict')
Decodes the string using the codec registered for encoding. encoding defaults to the default string
encoding.
encode(encoding='UTF-8',errors='strict')
index beg and ending index end are given) ends with suffix; Returns true if so, and false otherwise
expandtabs(tabsize=8)
spaces per tab if tabsize not provided
136
PYTHON [Unit-VI]
10
11
12
13
string if starting index beg and ending index end are given; returns index if found and -1 otherwise
index(str, beg=0, end=len(string)):Same as find(), but raises an exception if str not found
isa1num()
:Returns true if string has at least 1 character and all characters are
:Returns true if string has at least 1 character and all characters are
islower()
:Returns true if string has at least 1 cased character and all cased characters
:Returns true if a unicode string contains only numeric characters and false
otherwise
14
isspace() :Returns true if string contains only whitespace characters and false otherwise
15
istitle()
16
17
18
19
isupper() :Returns true if string has at least one cased character and all cased characters are in
uppercase and false otherwise
join(seq) :Merges (concatenates) the string representations of elements in sequence seq into a string,
with separator string
len(string)
ljust(width[, fillchar]) Returns a space-padded string with the original string left-justified to a total of
width columns
20
lower()
21
lstrip()
22
23
max(str)
137
PYTHON [Unit-VI]
24
25
min(str)
replace(old, new [, max]) Replaces all occurrences of old in string with new, or at most max
occurrences if max given
26
rfind(str, beg=0,end=len(string))
27
rindex( str, beg=0, end=len(string)) Same as index(), but search backwards in string
28
29
30
31
rjust(width,[, fillchar]) Returns a space-padded string with the original string right-justified to a total
of width columns.
rstrip()
split(str="", num=string.count(str))
and returns list of substrings; split into at most num substrings if given
splitlines( num=string.count('\n')) Splits string at all (or num) NEWLINEs and returns a list of each
line with NEWLINEs removed
startswith(str, beg=0,end=len(string))
32
index beg and ending index end are given) starts with substring str; Returns true if so, and false
otherwise
33
strip([chars])
34
swapcase()
title()
Returns "titlecased" version of string, that is, all words begin with
35
36
37
38
39
intended for numbers, zfill() retains any sign given (less one zero)
isdecimal()
Returns true if a unicode string contains only decimal characters and false
PYTHON [Unit-VI]
138
otherwise
Python Lists:
The list is a most versatile data type available in Python, which can be written as a list of commaseparated values (items) between square brackets. Good thing about a list that items in a list need not all
have the same type:
Creating a list is as simple as putting different comma-separated values between squere brackets. For
example:
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];
Like string indices, list indices start at 0, and lists can be sliced, concatenated and so on.
Accessing Values in Lists:
To access values in lists, use the square brackets for slicing along with the index or indices to obtain value
available at that index:
Example:
#!/usr/bin/python
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]
This will produce following result:
list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
Updating Lists:
You can update single or multiple elements of lists by giving the slice on the left-hand side of the
assignment operator, and you can add to elements in a list with the append() method:
Example:
#!/usr/bin/python
list1 = ['physics', 'chemistry', 1997, 2000];
print "Value available at index 2 : "
Open Source Software
139
PYTHON [Unit-VI]
print list1[2];
list1[2] = 2001;
print "New value available at index 2 : "
print list1[2];
Note: append() method is discussed in subsequent section.
This will produce following result:
Value available at index 2 :
1997
New value available at index 2 :
2001
Delete List Elements:
To remove a list element, you can use either the del statement if you know exactly which element(s) you are
deleting or the remove() method if you do not know.
Example:
#!/usr/bin/python
list1 = ['physics', 'chemistry', 1997, 2000];
print list1;
del list1[2];
print "After deleting value at index 2 : "
print list1;
This will produce following result:
['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]
Note: remove() method is discussed in subsequent section.
Basic List Operations:
Lists respond to the + and * operators much like strings; they mean concatenation and repetition here too,
except that the result is a new list, not a string.
Python Expression
Results
Description
len([1, 2, 3])
Length
140
PYTHON [Unit-VI]
[1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
Concatenation
['Hi!'] * 4
Repetition
3 in [1, 2, 3]
True
Membership
123
Iteration
Results
Description
L[2]
'SPAM!'
L[-2]
'Spam'
L[1:]
['Spam', 'SPAM!']
cmp(list1,list2)
len(list)
max(list)
min(list)
list(seq)
list.append(obj)
list.count(obj)
141
PYTHON [Unit-VI]
list.extend(seq)
list.index(obj)
list.insert(index,obj)
list.pop(obj=list[-1])
list.remove(obj)
list.reverse()
list.sort([func])
Tuples:
A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The only
difference is that tuples can't be changed ie. tuples are immutable and tuples use parentheses and lists use
square brackets.
Creating a tuple is as simple as putting different comma-separated values and optionally you can put these
comma-separated values between parentheses also. For example:
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";
The empty tuple is written as two parentheses containing nothing:
tup1 = ();
To write a tuple containing a single value you have to include a comma, even though there is only one value:
tup1 = (50,);
Like string indices, tuple indices start at 0, and tuples can be sliced, concatenated and so on.
Accessing Values in Tuples
To access values in tuple, use the square brackets for slicing along with the index or indices to obtain
value available at that index:
Example:
#!/usr/bin/python
tup1 = ('physics', 'chemistry', 1997, 2000);
Open Source Software
PYTHON [Unit-VI]
142
tup2 = (1, 2, 3, 4, 5, 6, 7 );
print "tup1[0]: ", tup1[0]
print "tup2[1:5]: ", tup2[1:5]
This will produce following result:
tup1[0]: physics
tup2[1:5]: [2, 3, 4, 5]
Updating Tuples:
Tuples are immutable which means you cannot update them or change values of tuple elements. But we able
able to take portions of an existing tuples to create a new tuples as follows:
Example:
#!/usr/bin/python
tup1 = (12, 34.56);
tup2 = ('abc', 'xyz');
# Following action is not valid for tuples
# tup1 += tup2;
# So let's create a new tuple as follows
tup3 = tup1 + tup2;
print tup3;
This will produce following result:
(12, 34.56, 'abc', 'xyz')
Delete Tuple Elements
Removing individual tuple elements is not possible. There is, of course, nothing wrong with putting
together another tuple with the undesired elements discarded. To explicitly remove an entire tuple, just use
the del statement:
Example:
#!/usr/bin/python
tup = ('physics', 'chemistry', 1997, 2000);
print tup;
del tup;
print "After deleting tup : "
print tup;
143
PYTHON [Unit-VI]
This will produce following result. Note an exception raised, this is because after del tup tuple does not
exist any more:
('physics', 'chemistry', 1997, 2000)
After deleting tup :
Traceback (most recent call last):
File "test.py", line 9, in <module>
print tup;
NameError: name 'tup' is not defined
Basic Tuples Operations:
Tuples respond to the + and * operators much like strings; they mean concatenation and repetition
here too, except that the result is a new tuple, not a string.
Python Expression
Results
Description
len((1, 2, 3))
Length
(1, 2, 3) + (4, 5, 6)
(1, 2, 3, 4, 5, 6)
Concatenation
['Hi!'] * 4
Repetition
3 in (1, 2, 3)
True
Membership
123
Iteration
Python Expression
Results
Description
L[2]
'SPAM!'
L[-2]
'Spam'
144
PYTHON [Unit-VI]
L[1:]
['Spam', 'SPAM!']
No Enclosing Delimiters
Any set of multiple objects, comma-separated, written without identifying symbols, i.e., brackets for lists,
parentheses for tuples, etc., default to tuples, as indicated in these short examples:
#!/usr/bin/python
print 'abc', -4.24e93, 18+6.6j, 'xyz';
x, y = 1, 2;
print "Value of x , y : ", x,y;
print var;
This will reduce following result:
abc -4.24e+93 (18+6.6j) xyz
Value of x , y : 1 2
Built-in Tuple Functions
Python includes following tuple functions
SN
cmp(tuple1, tuple2)
len(tuple)
max(tuple)
min(tuple)
tuple(seq)
Dictionaries:
A dictionary is mutable and is another container type that can store any number of Python objects, including
other container types. Dictionaries consist of pairs (called items) of keys and their corresponding values.
Python dictionaries are also known as associative arrays or hash tables. The general syntax of a dictionary is
as follows:
Open Source Software
PYTHON [Unit-VI]
145
146
PYTHON [Unit-VI]
Updating Dictionary
You can update a dictionary by adding a new entry or item (i.e., a key-value pair), modifying an
existing entry, or deleting an existing entry as shown below:
Example:
#!/usr/bin/python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};
dict['Age'] = 8; # update existing entry
dict['School'] = "DPS School"; # Add new entry
Sprint "dict['Age']: ", dict['Age'];
print "dict['School']: ", dict['School'];
This will produce following result:
dict['Age']: 8
dict['School']: DPS School
Delete Dictionary Elements
You can either remove individual dictionary elements or clear the entire contents of a dictionary.
You can also delete entire dictionary in a single operation.
To explicitly remove an entire dictionary, just use the del statement:
Example:
#!/usr/bin/python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};
del dict['Name']; # remove entry with key 'Name'
dict.clear();
del dict ;
147
PYTHON [Unit-VI]
148
PYTHON [Unit-VI]
len(dict)
Gives the total length of the dictionary. This would be equal to the
type(variable)
dict.clear()
dict.copy()
dict.fromkeys()
dict.get(key, default=None) For key key, returns value or default if key not in dictionary
dict.has_key(key)
dict.items()
dict.keys()
Create a new dictionary with keys from seq and values set to value.
dict.setdefault(key, default=None)
in dict
dict.update(dict2)
dict.values()
PYTHON [Unit-VI]
149
PYTHON [Unit-VI]
150
PYTHON [Unit-VI]
151
statement(s)
Note: Python does not currently support switch or case statements as in other languages.
Example:
#!/usr/bin/python
var = 100
if var == 200:
print "1 - Got a true expression value"
print var
elif var == 150:
print "2 - Got a true expression value"
print var2
elif var == 100:
print "3 - Got a true expression value"
print var
else:
print "4 - Got a false expression value"
print var
PYTHON [Unit-VI]
statement(s)
elif expression3:
statement(s)
else
statement(s)
elif expression4:
statement(s)
else:
statement(s)
Example:
#!/usr/bin/python
var = 100
if var < 200:
print "Expression value is less than 200"
if var == 150:
print "Which is 150"
elif var == 100:
print "Which is 100"
elif var == 50:
print "Which is 50"
elif var < 50:
print "Expression value is less than 50"
else:
print "Could not find true expression"
152
PYTHON [Unit-VI]
153
PYTHON [Unit-VI]
154
155
PYTHON [Unit-VI]
# First Example
# Second Example
PYTHON [Unit-VI]
156
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!
Iterating by Sequence Index:
An alternative way of iterating through each item is by index offset into the sequence itself:
Example:
#!/usr/bin/python
157
UNIT VII
raw_input
input
158
file_name: The file_name argument is a string value that contains the name of the file that you want
to access.
access_mode: The access_mode determines the mode in which the file has to be opened ie. read,
write append etc. A complete list of possible values is given below in the table. This is optional
parameter and the default file access mode is read (r)
buffering: If the buffering value is set to 0, no buffering will take place. If the buffering value is 1,
line buffering will be performed while accessing a file. If you specify the buffering value as an
integer greater than 1, then buffering action will be performed with the indicated buffer size. If
negative, the buffer size is the system default(default behavior).
Rb
Description
Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the
default mode.
Opens a file for reading only in binary format. The file pointer is placed at the beginning of the
file. This is the default mode.
r+
rb+
Wb
w+
wb+
159
Opens a file for both reading and writing. The file pointer will be at the beginning of the file.
Opens a file for both reading and writing in binary format. The file pointer will be at the beginning
of the file.
Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates
a new file for writing.
Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does
not exist, creates a new file for writing.
Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file
does not exist, creates a new file for reading and writing.
Opens a file for both writing and reading in binary format. Overwrites the existing file if the file
exists. If the file does not exist, creates a new file for reading and writing.
Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file
is in the append mode. If the file does not exist, it creates a new file for writing.
Opens a file for appending in binary format. The file pointer is at the end of the file if the file
Ab
exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for
writing.
Opens a file for both appending and reading. The file pointer is at the end of the file if the file
a+
exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading
and writing.
Opens a file for both appending and reading in binary format. The file pointer is at the end of the
ab+
file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new
file for reading and writing.
Attribute
Description
file.closed
file.mode
file.name
file.softspace
160
Example:
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
print "Closed or not : ", fo.closed
print "Opening mode : ", fo.mode
print "Softspace flag : ", fo.softspace
This would produce following result:
Name of the file: foo.txt
Closed or not : False
Opening mode : wb
Softspace flag : 0
The close() Method:
The close() method of a file object flushes any unwritten information and closes the file object, after
which no more writing can be done.Python automatically closes a file when the reference object of a file is
reassigned to another file. It is a good practice to use the close() method to close a file.
Syntax:
fileObject.close();
Example:
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
Open Source Software
161
162
Syntax:
fileObject.read([count]);
Here passed parameter is the number of bytes to be read from the opend file. This method starts
reading from the beginning of the file and if count is missing then it tries to read as much as possible, may
be until the end of file.
Example:
Let's take a file foo.txt which we have created above.
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
print "Read String is : ", str
# Close opend file
fo.close()
This would produce following result:
Read String is : Python is
File Positions
The tell() method tells you the current position within the file in other words, the next read or write
will occur at that many bytes from the beginning of the file:
The seek(offset[, from]) method changes the current file position. The offset argument indicates the
number of bytes to be moved. The from argument specifies the reference position from where the bytes are
to be moved.
If from is set to 0, it means use the beginning of the file as the reference position and 1 means use the
current position as the reference position and if it is set to 2 then the end of the file would be taken as the
reference position.
Example:
Let's take a file foo.txt which we have created above.
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
163
164
Syntax:
os.delete(file_name)
Example:
Following is the example to delete an existing file test2.txt:
#!/usr/bin/python
import os
# Delete file test2.txt
os.delete("text2.txt")
Directories in Python
All files are contained within various directories, and Python has no problem handling these too. The
os module has several methods that help you create, remove, and change directories.
The mkdir() Method:
You can use the mkdir() method of the os module to create directories in the current directory. You
need to supply an argument to this method, which contains the name of the directory to be created.
Syntax:
os.mkdir("newdir")
Example:
Following is the example to create a directory test in the current directory:
#!/usr/bin/python
import os
# Create a directory "test"
os.mkdir("test")
The chdir() Method:
You can use the chdir() method to change the current directory. The chdir() method takes an argument,
which is the name of the directory that you want to make the current directory.
Syntax:
os.chdir("newdir")
Example:
Following is the example to go into "/home/newdir" directory:
165
#!/usr/bin/python
import os
# Changing a directory to "/home/newdir"
os.chdir("/home/newdir")
The getcwd() Method:
The getcwd() method displays the current working directory.
Syntax:
os.getcwd()
Example:
Following is the example to give current directory:
#!/usr/bin/python
import os
# This would give location of the current directory
os.getcwd()
The rmdir() Method:
The rmdir() method deletes the directory, which is passed as an argument in the method.
Before removing a directory, all the contents in it should be removed.
Syntax:
os.rmdir('dirname')
Example:
Following is the example to remove "/tmp/test" directory. It is required to give fully qualified name of the
directory otherwise it would search for that directory in the current directory.
#!/usr/bin/python
import os
# This would remove "/tmp/test" directory.
os.rmdir( "/tmp/test" )
File & Directory Related Methods
There are three important sources which provide a wide range of utility methods to handle and
manipulate files & directories on Windows and Unix operating systems. They are as follows:
o
File Object Methods: The file object provides functions to manipulate files.
166
What is Exception?
An exception is an event, which occurs during the execution of a program that disrupts the normal flow of
the program's instructions.
In general, when a Python script encounters a situation that it can't cope with, it raises an exception. An
exception is a Python object that represents an error.
When a Python script raises an exception, it must either handle the exception immediately otherwise it
would terminate and come out.
Handling an exception
If you have some suspicious code that may raise an exception, you can defend your program by placing the
suspicious code in a try: block. After the try: block, include an except: statement, followed by a block of
code which handles the problem as elegantly as possible.
Syntax:
Here is simple syntax of try....except...else blocks:
try:
You do your operations here;
......................
except ExceptionI:
If there is ExceptionI, then execute this block.
except ExceptionII:
If there is ExceptionII, then execute this block.
......................
else:
If there is no exception then execute this block.
Here are few important points above the above mentioned syntax:
A single try statement can have multiple except statements. This is useful when the try block
contains statements that may throw different types of exceptions.
You can also provide a generic except clause, which handles any exception.
167
After the except clause(s), you can include an else-clause. The code in the else-block executes if the
code in the try: block does not raise an exception.
The else-block is a good place for code that does not need the try: block's protection.
Example:
Here is simple example which opens a file and writes the content in the file and comes out gracefully
because there is no problem at all:
#!/usr/bin/python
try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
except IOError:
print "Error: can\'t find file or read data"
else:
print "Written content in the file successfully"
fh.close()
This will produce following result:
Written content in the file successfully
The except clause with no exceptions
You can also use the except statement with no exceptions defined as follows:
try:
You do your operations here;
......................
except:
If there is any exception, then execute this block.
......................
else:
If there is no exception then execute this block.
This kind of a try-except statement catches all the exceptions that occur. Using this kind of try-except
statement is not considered a good programming practice, though, because it catches all exceptions but does
not make the programmer identify the root cause of the problem that may occur.
The except clause with multiple exceptions
You can also use the same except statement to handle multiple exceptions as follows:
Open Source Software
168
try:
You do your operations here;
......................
except(Exception1[, Exception2[,...ExceptionN]]]):
If there is any exception from the given exception list,
then execute this block.
......................
else:
If there is no exception then execute this block.
The try-finally clause
You can use a finally: block along with a try: block. The finally block is a place to put any code that must
execute, whether the try-block raised an exception or not. The syntax of the try-finally statement is this:
try:
You do your operations here;
......................
Due to any exception, this may be skipped.
finally:
This would always be executed.
......................
Note that you can provide except clause(s), or a finally clause, but not both. You can not use else clause as
well along with a finally clause.
Example:
#!/usr/bin/python
try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
finally:
print "Error: can\'t find file or read data"
If you do not have permission to open the file in writing mode then this will produce following result:
Error: can't find file or read data
Same example can be written more cleanly as follows:
Open Source Software
169
#!/usr/bin/python
try:
fh = open("testfile", "w")
try:
fh.write("This is my test file for exception handling!!")
finally:
fh.close()
except IOError:
print "Error: can\'t find file or read data"
When an exception is thrown in the try block, the execution immediately passes to the finally block. After all
the statements in the finally block are executed, the exception is raised again and is handled in the except
statements if present in the next higher layer of the try-except statement.
Argument of an Exception:
An exception can have an argument, which is a value that gives additional information about the problem.
The contents of the argument vary by exception. You capture an exception's argument by supplying a
variable in the except clause as follows:
try:
You do your operations here;
......................
except ExceptionType, Argument:
You can print value of Argument here...
If you are writing the code to handle a single exception, you can have a variable follow the name of the
exception in the except statement. If you are trapping multiple exceptions, you can have a variable follow
the tuple of the exception.
This variable will receive the value of the exception mostly containing the cause of the exception. The
variable can receive a single value or multiple values in the form of a tuple. This tuple usually contains the
error string, the error number, and an error location.
Example:
Following is an example for a single exception:
#!/usr/bin/python
170
def temp_convert(var):
try:
return int(var)
except ValueError, Argument:
print "The argument does not contain numbers\n", Argument
171
Here is an example related to RuntimeError. Here a class is created that is subclassed from RuntimeError.
This is useful when you need to display more specific information when an exception is caught.
In the try block, the user-defined exception is raised and caught in the except block. The variable e is used to
create an instance of the class Networkerror.
class Networkerror(RuntimeError):
def __init__(self, arg):
self.args = arg
So once you defined above class, you can raise your exception as follows:
try:
raise Networkerror("Bad hostname")
except Networkerror,e:
print e.args
Functions:
A function is a block of organized, reusable code that is used to perform a single, related action.
Functions provides better modularity for your application and a high degree of code reusing.
Defining a Function:
You can define functions to provide the required functionality. Here are simple rules to define a function
in Python:
o Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
o Any input parameters or arguments should be placed within these parentheses. You can also define
parameters inside these parentheses.
o The first statement of a function can be an optional statement - the documentation string of the
function or docstring.
o The code block within every function starts with a colon (:) and is indented.
o The statement return [expression] exits a function, optionally passing back an expression to the
caller. A return statement with no arguments is the same as return None.
Syntax:
def functionname( parameters ):
"function_docstring"
function_suite
return [expression]
172
By default, parameters have a positional behavior, and you need to inform them in the same order that they
were defined.
Example:
Here is the simplest form of a Python function. This function takes a string as input parameter and prints it
on standard screen.
def printme( str ):
"This prints a passed string into this function"
print str
return
Calling a Function:
Defining a function only gives it a name, specifies the parameters that are to be included in the
function, and structures the blocks of code.Once the basic structure of a function is finalized, you can
execute it by calling it from another function or directly from the Python prompt.
Following is the example to call printme() function:
#!/usr/bin/python
# Function definition is here
def printme( str ):
"This prints a passed string into this function"
print str;
return;
# Now you can call printme function
printme("I'm first call to user defined function!");
printme("Again second call to the same function");
This would produce following result:
I'm first call to user defined function!
Again second call to the same function
Pass by reference vs value:
All parameters (arguments) in the Python language are passed by reference. It means if you change
what a parameter refers to within a function, the change also reflects back in the calling function. For
example:
#!/usr/bin/python
173
174
Required arguments
Keyword arguments
Default arguments
Variable-length arguments
Required arguments:
Required arguments are the arguments passed to a function in correct positional order. Here the number of
arguments in the function call should match exactly with the function definition.
To call the function printme() you definitely need to pass one argument otherwise it would give a syntax
error as follows:
#!/usr/bin/python
# Function definition is here
def printme( str ):
"This prints a passed string into this function"
print str;
return;
# Now you can call printme function
printme();
This would produce following result:
Traceback (most recent call last):
File "test.py", line 11, in <module>
printme();
TypeError: printme() takes exactly 1 argument (0 given)
Keyword arguments:
Keyword arguments are related to the function calls. When you use keyword arguments in a function
call, the caller identifies the arguments by the parameter name. This allows you to skip arguments or place
them out of order because the Python interpreter is able to use the keywords provided to match the values
with parameters. You can also make keyword calls to the printme() function in the following ways:
#!/usr/bin/python
175
176
177
Lambda forms can take any number of arguments but return just one value in the form of an
expression. They cannot contain commands or multiple expressions.
An anonymous function cannot be a direct call to print because lambda requires an expression.
Lambda functions have their own local namespace and cannot access variables other than those in
their parameter list and those in the global namespace.
Although it appears that lambda's are a one-line version of a function, they are not equivalent to
inline statements in C or C++, whose purpose is by passing function stack allocation during
invocation for performance reasons.
Syntax:
The syntax of lambda functions contains only a single statement, which is as follows:
lambda [arg1 [,arg2,.....argn]]:expression
Example:
Following is the example to show how lembda form of function works:
#!/usr/bin/python
# Function definition is here
sum = lambda arg1, arg2: arg1 + arg2;
# Now you can call sum as a function
print "Value of total : ", sum( 10, 20 )
178
Global variables
Local variables
179
This means that local variables can be accessed only inside the function in which they are declared whereas
global variables can be accessed throughout the program body by all functions. When you call a function,
the variables declared inside it are brought into scope.
Example:
#!/usr/bin/python
total = 0; # This is global variable.
# Function definition is here
def sum( arg1, arg2 ):
# Add both the parameters and return them."
total = arg1 + arg2; # Here total is local variable.
print "Inside the function local total : ", total
return total;
# Now you can call sum function
sum( 10, 20 );
print "Outside the function global total : ", total
This would produce following result:
Inside the function local total : 30
Outside the function global total : 0
Modules:
A module allows you to logically organize your Python code. Grouping related code into a module
makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes
that you can bind and reference. Simply, a module is a file consisting of Python code. A module can define
functions, classes, and variables. A module can also include runnable code.
Example:
The Python code for a module named aname normally resides in a file named aname.py. Here's an
example of a simple module, hello.py
def print_func( par ):
print "Hello : ", par
return
The import Statement
You can use any Python source file as a module by executing an import statement in some other
Python source file. import has the following syntax:
Open Source Software
180
181
This provides an easy way to import all the items from a module into the current namespace; however, this
statement should be used sparingly.
Locating Modules
When you import a module, the Python interpreter searches for the module in the following sequences:
If the module isn't found, Python then searches each directory in the shell variable PYTHONPATH.
If all else fails, Python checks the default path. On UNIX, this default path is normally
/usr/local/lib/python/.
The module search path is stored in the system module sys as the sys.path variable. The sys.path variable
contains the current directory, PYTHONPATH, and the installation-dependent default.
182
If globals() is called from within a function, it will return all the names that can be accessed globally
from that function.The return type of both these functions is dictionary. Therefore, names can be extracted
using the keys() function.
The reload() Function:
When the module is imported into a script, the code in the top-level portion of a module is executed
only once.
Therefore, if you want to reexecute the top-level code in a module, you can use the reload() function. The
reload() function imports a previously imported module again.
Syntax:
The syntax of the reload() function is this:
reload(module_name)
Packages in Python
A package is a hierarchical file directory structure that defines a single Python application
environment that consists of modules and subpackages and sub-subpackages, and so on.
Example:
Consider a file Pots.py available in Phone directory. This file has following line of source code:
#!/usr/bin/python
def Pots():
print "I'm Pots Phone"
Similar way we have another two files having different functions with the same name as above:
o Phone/Isdn.py file having function Isdn()
o Phone/G3.py file having function G3()
o Now create one more file __init__.py in Phone directory :
o Phone/__init__.py
To make all of your functions available when you've imported Phone, you need to put explicit import
statements in __init__.py as follows:
from Pots import Pots
from Isdn import Isdn
from G3 import G3
After you've added these lines to __init__.py, you have all of these classes available when you've imported
the Phone package:
183
#!/usr/bin/python
# Now import your Phone Package.
import Phone
Phone.Pots()
Phone.Isdn()
Phone.G3()
This would produce following result:
I'm Pots Phone
I'm 3G Phone
I'm ISDN Phone
Note: In the above example, we have taken example of a single functions in each file, but you can keep
multiple functions in your files. You can also define different Python classes in those files and then you can
create your packages out of those classes.
184
Instance: An individual object of a certain class. An object obj that belongs to a class Circle, for
example, is an instance of the class Circle.
Instantiation : The creation of an instance of a class.
Method : A special kind of function that is defined in a class definition.
Object : A unique instance of a data structure that's defined by its class. An object comprises both
data members (class variables and instance variables) and methods.
Operator overloading: The assignment of more than one function to a particular operator.
Creating Classes:
The class statement creates a new class definition. The name of the class immediately follows the
keyword class followed by a colon as follows:
class ClassName:
'Optional class documentation string'
class_suite
o
The class has a documentation string which can be access via ClassName.__doc__.
The class_suite consists of all the component statements, defining class members, data attributes, and
functions.
Example:
Following is the example of a simple Python class:
class Employee:
'Common base class for all employees'
empCount = 0
def displayCount(self):
print "Total Employee %d" % Employee.empCount
def displayEmployee(self):
print "Name : ", self.name, ", Salary: ", self.salary
185
The variable empCount is a class variable whose value would be shared among all instances of a this
class. This can be accessed as Employee.empCount from inside the class or outside the class.
The first method __init__() is a special method which is called class constructor or initialization
method that Python calls when you create a new instance of this class.
You declare other class methods like normal functions with the exception that the first argument to
each method is self. Python adds the self argument to the list for you; you don't need to include it
when you call the methods.
def displayCount(self):
Open Source Software
186
def displayEmployee(self):
print "Name : ", self.name, ", Salary: ", self.salary
The setattr(obj,name,value) : to set an attribute. If attribute does not exist then it would be created.
187
__module__: Module name in which the class is defined. This attribute is "__main__" in interactive
mode.
__bases__ : A possibly empty tuple containing the base classes, in the order of their occurrence in
the base class list.
For the above class let's try to access all these attributes:
print "Employee.__doc__:", Employee.__doc__
print "Employee.__name__:", Employee.__name__
print "Employee.__module__:", Employee.__module__
print "Employee.__bases__:", Employee.__bases__
print "Employee.__dict__:", Employee.__dict__
This would produce following result:
Employee.__doc__: Common base class for all employees
Employee.__name__: Employee
Employee.__module__: __main__
Employee.__bases__: ()
Employee.__dict__: {'__module__': '__main__', 'displayCount':
<function displayCount at 0xb7c84994>, 'empCount': 2,
'displayEmployee': <function displayEmployee at 0xb7c8441c>,
'__doc__': 'Common base class for all employees',
'__init__': <function __init__ at 0xb7c846bc>}
Destroying Objects (Garbage Collection)
Python deletes unneeded objects (built-in types or class instances) automatically to free memory
space. The process by which Python periodically reclaims blocks of memory that no longer are in use is
termed garbage collection.
Python's garbage collector runs during program execution and is triggered when an object's reference count
reaches zero. An object's reference count changes as the number of aliases that point to it changes:
An object's reference count increases when it's assigned a new name or placed in a container (list,
tuple, or dictionary). The object's reference count decreases when it's deleted with del, its reference is
reassigned, or its reference goes out of scope. When an object's reference count reaches zero, Python collects
it automatically.
Open Source Software
a = 40
b=a
c = [b]
del a
b = 100
188
class Point:
def __init( self, x=0, y=0):
self.x = x
self.y = y
def __del__(self):
class_name = self.__class__.__name__
print class_name, "destroyed"
pt1 = Point()
pt2 = pt1
pt3 = pt1
print id(pt1), id(pt2), id(pt3) # prints the ids of the obejcts
del pt1
del pt2
del pt3
This would produce following result:
3083401324 3083401324 3083401324
Open Source Software
189
Point destroyed
Class Inheritance
Instead of starting from scratch, you can create a class by deriving it from a preexisting class by
listing the parent class in parentheses after the new class name.
The child class inherits the attributes of its parent class, and you can use those attributes as if they
were defined in the child class. A child class can also override data members and methods from the parent.
Syntax:
Derived classes are declared much like their parent class; however, a list of base classes to inherit from are
given after the class name:
class SubClassName (ParentClass1[, ParentClass2, ...]):
'Optional class documentation string'
class_suite
Example:
#!/usr/bin/python
class Parent:
parentAttr = 100
def __init__(self):
print "Calling parent constructor"
def parentMethod(self):
print 'Calling parent method'
def setAttr(self, attr):
Parent.parentAttr = attr
def getAttr(self):
print "Parent attribute :", Parent.parentAttr
class Child(Parent): # define child class
def __init__(self):
print "Calling child constructor"
def childMethod(self):
print 'Calling child method'
c = Child()
c.childMethod()
Open Source Software
# instance of child
# child calls its method
c.parentMethod()
c.setAttr(200)
c.getAttr()
190
.....
class B:
.....
The issubclass(sub, sup) boolean function returns true if the given subclass sub is indeed a subclass
of the superclass sup.
The isinstance(obj, Class) boolean function returns true if obj is an instance of class Class or is an
instance of a subclass of Class
Overriding Methods
You can always override your parent class methods. One reason for overriding parent's methods is because
you may want special or different functionality in your subclass.
Example:
#!/usr/bin/python
class Parent:
def myMethod(self):
print 'Calling parent method'
191
# instance of child
c.myMethod()
__del__( self )
__repr__( self )
__str__( self )
__cmp__ ( self, x )
Overloading Operators
Suppose you've created a Vector class to represent two-dimensional vectors. What happens when you use
the plus operator to add them? Most likely Python will yell at you.
You could, however, define the __add__ method in your class to perform vector addition, and then the plus
operator would behave as per expectation:
Example:
#!/usr/bin/python
class Vector:
def __init__(self, a, b):
self.a = a
self.b = b
def __str__(self):
return 'Vector (%d, %d)' % (self.a, self.b)
Open Source Software
192
def __add__(self,other):
return Vector(self.a + other.a, self.b + other.b)
v1 = Vector(2,10)
v2 = Vector(5,-2)
print v1 + v2
This would produce following result:
Vector(7,8)
Data Hiding
An object's attributes may or may not be visible outside the class definition. For these cases, you can
name attributes with a double underscore prefix, and those attributes will not be directly visible to outsiders:
Example:
#!/usr/bin/python
class JustCounter:
__secretCount = 0
def count(self):
self.__secretCount += 1
print self.__secretCount
counter = JustCounter()
counter.count()
counter.count()
print counter.__secretCount
This would produce following result:
1
2
Traceback (most recent call last):
File "test.py", line 12, in <module>
print counter.__secretCount
AttributeError: JustCounter instance has no attribute '__secretCount'
Python protects those members by internally changing the name to include the class name. You can access
such attributes as object._className__attrName.
If you would replace your last line as following, then it would work for you:
Open Source Software
193
.........................
print counter._JustCounter__secretCount
This would produce following result:
1
2
2
Execution Environment:
Callable Objects:
A number of Python objects are what we describe as "callable," meaning any object
which can be invoked with the function operator "()". The function operator is placed
immediately following the name of the callable to invoke it.
Functions
There are three types of different function objects, the first being the Python built-in functions.
Built-in Functions (BIFs)
BIFs are generally written as extensions in C or C++, compiled into the Python
interpreter, and loaded into the system as part of the first (built-in) namespace.
BIF
Attribute Description
bif.__doc__
documentation string
bif.__name__
bif.__self__
UIF
Attribute Description
udf.__doc__
udf.__name__
udf.func_code
udf.func_defaults
194
udf.func_globals
Description
bim.__doc__
documentation string
bim.__name__
bim.__self__
udf.func_defaults
udf.func_globals
UDM Attribute
Description
udm.__doc__
documentation string
udm.__name__
udm.im_class
udm.im_func
udm.im_self
Classes
The callable property of classes allows instances to be created. "Invoking" a class has the
effect of creating an instance, better known as instantiation.
Open Source Software
195
Class instances
Python provides the __call__() special method for classes which allows a programmer
to create objects (instances) which are callable. By default, the __call__() method is not
implemented, meaning that most instances are not callable.
Code Objects
Code objects represent byte-compiled executable Python code, or bytecode. The difference between a
code object and a function object is that the function object contains an explicit reference to the function's
globals (the module in which it was defined), while a code object contains no context; also the default
argument values are stored in the function object, not in the code object (because they represent values
calculated at run-time). Unlike function objects, code objects are immutable and contain no references
(directly or indirectly) to mutable objects.
Code objects are used by the implementation to represent ``pseudo-compiled'' executable Python
code such as a function body. They differ from function objects because they don't contain a reference to
their global execution environment. Code objects are returned by the built-in compile() function and can be
extracted from function objects through their func_code attribute.
A code object can be executed or evaluated by passing it (instead of a source string) to the exec
statement or the built-in eval() function.
Executable Object Statements and Built-in Functions
Python provides a number of built-in functions supporting callables and executable
objects, including the exec statement.
Built-in Function or Statement
callable(obj)
Description
determines if obj is callable; returns 1 if so, 0
otherwise
compile(string, file,
type)
file is
where the code originates from (usually set to
?)
eval(obj,
globals=globals(),
compiled into
locals=locals())
196
input(prompt='')
equivalent to eval(raw_input(prompt=''))
intern(string)
callable()
callable() is a Boolean function which determines if an object type can be invoked via
the function operator ( ( ) ). It returns 1 if the object is callable and 0 otherwise. Here are
some sample objects and what callable returns for each type:
>>> callable(dir) # built-in function
1
>>> callable(1) # integer
0
>>> def foo(): pass
197
The filename argument should give the file from which the code was read; pass some recognizable
value if it wasnt read from a file ('<string>' is commonly used).
The mode argument specifies what kind of code must be compiled; it can be 'exec' if source consists
of a sequence of statements, 'eval' if it consists of a single expression, or 'single' if it consists of a single
interactive statement (in the latter case, expression statements that evaluate to something other than None
will be printed).
The optional arguments flags and dont_inherit control which future affect the compilation of source.
If neither is present (or both are zero) the code is compiled with those future statements that are in effect in
the code that is calling compile. If the flags argument is given and dont_inherit is not (or is zero) then the
future statements specified by the flags argument are used in addition to those that would be used anyway. If
dont_inherit is a non-zero integer then the flags argument is it the future statements in effect around the
call to compile are ignored.
Future statements are specified by bits which can be bitwise ORed together to specify multiple
statements. The bit field required to specify a given feature can be found as the compiler_flag attribute on
the _Feature instance in the __future__ module.
This function raises SyntaxError if the compiled source is invalid, and TypeError if the source contains null
bytes.
eval()
The arguments are a string and optional globals and locals. If provided, globals must be a dictionary.
If provided, locals can be any mapping object.
The expression argument is parsed and evaluated as a Python expression (technically speaking, a
condition list) using the globals and locals dictionaries as global and local namespace. If the globals
dictionary is present and lacks __builtins__, the current globals are copied into globals before expression is
parsed. This means that expression normally has full access to the standard __builtin__ module and
restricted environments are propagated. If the locals dictionary is omitted it defaults to the globals
dictionary. If both dictionaries are omitted, the expression is executed in the environment where eval() is
called. The return value is the result of the evaluated expression. Syntax errors are reported as exceptions.
Example:
>>> x = 1
>>> print eval('x+1')
2
This function can also be used to execute arbitrary code objects (such as those created by compile()).
In this case pass a code object instead of a string. If the code object has been compiled with 'exec' as the
mode argument, eval()s return value will be None.
Open Source Software
198
PERL [Unit-VIII]
UNIT VIII
PERL
Perl backgrounder
Control structures Subroutines, Packages, and Modules- Working with Files Data Manipulation.
Speed of development. You edit a text file, and just run it. You can develop programs very quickly
like this. No separate compiler needed. I find Perl runs a program quicker than Java, let alone
compare the complete modify-compile-run-oh-no-forgot-that-semicolon sequence.
Power. Perl's regular expressions are some of the best available. You can work with objects,
sockets...everything a systems administrator could want. And that's just the standard distribution.
Add the wealth of modules available on CPAN and you have it all. Don't equate scripting languages
with toy languages.
Usuability. All that power and capability can be learnt in easy stages. If you can write a batch file
you can program Perl. You don't have to learn object oriented programming, but you can write OO
programs in Perl. If autoincrementing non-existent variables scares you, make perl refuse to let you.
There is always more than one way to do it in Perl. You decide your style of programming, and Perl
will accommodate you.
Portability. On the Superhighway to the Portability Panacea, Perl's Porsche powers past Java's jaded
jalopy. Many people develop Perl scripts on NT, or Win95, then just FTP them to a Unix server
where they run. No modification necessary.
Editing tools You don't need the latest Integrated Development Environment for Perl. You can
develop Perl scripts with any text editor. Notepad, vi, MS Word 97, or even direct off the console. Of
course, you can make things easy and use one of the many freeware or shareware programmer's file
editors.
PERL [Unit-VIII]
199
Price. Yes, 0 guilders, pounds, dmarks, dollars or whatever. And the peer to peer support is also free,
and often far better than you'd ever get by paying some company to answer the phone and tell you to
do what you just tried several times already, then look up the same reference books you already own.
User account creation. If you have a text file with the user's names in it, that is all you need. Create
usernames automatically, generate a unique password for each one and create the account, plus
create and share the home directory, and set the permissions.
Event log munging. NT has great Event Logging. Not so great Event Reading. You can use Perl to
create reports on the event logs from multiple NT servers.
Anything else that you would have used a batch file for, or wished that you could automate
somehow. Now you can.
200
PERL [Unit-VIII]
Perl is not type-safe and this can cause confusion and errors. Use a limited prefix notation for such common
basic types as array, hash, FileHandle.
o
8.1.1.2 Functions
o
8.1.1.4 Classes
As for modules but with 'C' prefix: CStopwatch, CWindowPanel, Pdb::CResidue
8.1.1.5 Instance methods
o
Date
Perl 0
Version Details
Introduced Perl to Larry Walls office associates
Perl 1
Jan 1988
Perl 2
Jun 1988
201
PERL [Unit-VIII]
Perl 3
Oct 1989
Perl 4
Mar 1991
Schwartz;
OReilly & Associates). The book drove the name change, just so it
could refer to Perl 4, instead of Perl 3.
Perl 4.036
Feb 1993
Perl 5
Oct 1994
Perl 5.005_02
Aug 1998
Perl 5.005_03
Mar 1999
Perl 5.6
Mar 2000
PERL [Unit-VIII]
202
PERL [Unit-VIII]
203
PERL [Unit-VIII]
204
205
PERL [Unit-VIII]
http://www.activestate.com/ (http://www.activestate.com/)
3. Double-click the ActivePerl installer.
4. After the installer confirms the version of ActivePerl that it is going to be installed, click Next.
5. If you agree with the terms of the license agreement, click I accept the terms in the license
agreement,
and
then
click
Next.
Click Cancel if you do not accept the license agreement. If you do so, you cannot continue the
installation.
6. To install the whole ActivePerl distribution package (this step is recommended), click Next to
continue
the
installation.
Add Perl to the PATH environment variable: Click this setting if you want to use Perl in a
command prompt without requiring the full path to the Perl interpreter.
Create Perl file extension association: Click this setting if you want to allow Perl scripts to
be automatically run when you use a file that has the Perl file name extension (.pl) as a
command name.
Create IIS script mapping for Perl: Click this setting to configure IIS to identify Perl
scripts as executable CGI programs according to their file name extension.
Create IIS script mapping for Perl ISAPI: Click this setting to use Perl scripts as an ISAPI
filter.
PERL [Unit-VIII]
206
A specific language feature is that variables have a non-alphabetical prefix that fashion somewhat
cryptic the language.
scalar variables starting with $
array variables starting with @
hashes or associative arrays indicated by %
The $, @ and % characters actually predefine the variable type in Perl. Perl language also offers some builtin predefined variables that facilitate and shorten the programming code.
8.2.5.2 Operators
The operators work with numbers and strings and manipulate data objects called operands. We found
the operators in expressions which we need to evaluate.
8.2.5.3 Statements
The statements are one of the most important topics in the Perl language, actually for any
programming language. We use statements in order to process or evaluate the expressions. Perl uses the
values returned by statements to evaluate or process other statements.
A Perl statement ends with the semicolon character (;) which is used to tell interpreter that the
statement was complete.
8.2.5.4 Subroutines (Functions)
Definition: Subroutine (Function) is a block of source code which does one or some tasks with specified
purpose.
Advantages:
i) It reduces the Complexity in a program by reducing the code.
ii) It also reduces the Time to run a program.In other way,Its directly proportional to Complexity.
iii) Its easy to find-out the errors due to the blocks made as function definition outside the main
function.
Modules:
A Perl module is a discrete component of software for the Perl programming language. Technically,
it is a particular set of conventions for using Perl's package mechanism that has become universally adopted.
A module defines its source code to be in a package (much like a Java package), the Perl mechanism
for defining namespaces, e.g. CGI or Net::FTP or XML::Parser; the file structure mirrors the namespace
structure (e.g. the source code for Net::FTP is in Net/FTP.pm). A collection of modules, with accompanying
documentation, build scripts, and usually a test suite, compose a distribution.
PERL [Unit-VIII]
Bare words Character strings that are not identified as valid terms
Precedence Once the individual items are identified, the parser processes
the statements according to the precedence rules, which apply to all operators
and terms
Context What is the context of the statement, are we expecting a list or scalar,
a number or a string, and so on. This actually happens during the evaluation
of individual elements of a line, which is why we can nest functions such as
sort, reverse, and keys into a single statement line
Logic Syntax For logic operations, the parser must treat different values,
whether constant- or variable-based, as true or false values
All of these present some fairly basic and fundamental rules about how Perl looks at
an entire script.
Lines must start with a token that does not expect a left operand
Lines must be terminated with a semicolon, except when its the last line of a
207
PERL [Unit-VIII]
White space is only required between tokens that would otherwise be confusing,
so spaces, tabs, newlines, and comments (which Perl treats as white space) are
ignored. The line
sub menu{print"menu"}
works as it would if it were more neatly spaced.
Lines may be split at any point, providing the split is logically between two
tokens.
Variables can start with a letter, number, or underscore, providing they follow
a suitable variable character, such as $, @, or %.
Variables that start with a letter or underscore can contain any further
combination of letters, numbers, and underscore characters.
Variables that start with a number can only consist of further numbersbe
wary of using variable names starting with digits. The variables such as $0
through to $9 are used for group matches in regular expressions.
Subroutines can only start with an underscore or letter, but can then
contain any combination of letters, numbers, and underscore characters.
Each of the three main variable types have their own name space$var, @var,
and %var are all separate variables.
208
209
PERL [Unit-VIII]
Description
Addition operator
Subtraction operator
Multiplication operator
Division operator
Modulus operator
**
Exponentiation operator
The operators +, -, *, / take two operands and return the sum, difference, product and quotient respectively.
Perl does an floating point division not an integral division. To get the integral quotient one has to use int()
function. Say if you divide "int(5/2)" the result will be 2, to get the exact result use the code below.
b)Assignment Operator
Operator
Function
Normal Assignment
+=
-=
*=
/=
%=
**=
Exponent and Assign
Everyone knows how to use the assignment operator (=). There are other operators, when used with "="
gives a different result.
c) Increment/Decrement Operators
The following are the auto increment, decrement operators in Perl.
Operator
Description
++
Autoincrement operator
-Autodecrement operator
The usage of auto increment operators are same as in C Language. In prefix decrement/increment first the
value is increased or decreased then the new value is returned eg: "++$a", "--$a".
210
PERL [Unit-VIII]
The vice versa of the above, is post decrement/increment operators. First the old value is returned then
incremented or decremented to give the result. eg: "$a++", "$a--"
d) Comparison Operator
Operator
Function
==
eq
Equal to Operator
!=
ne
<
lt
>
gt
<=
le
>=
ge
e) Logical Operators
The following are the Logical Operators used in Perl.
Operator
Function
&&
and
||
or
not
f) Bitwise Operators
Operator
Function
<<
>>
&
Bitwise AND
Bitwise OR
Bitwise XOR
~
Bitwise NOT
The basic criterias for the bitwise shift operators is that the operands should be numerals, but these are
represented as binary internally.
g)Bitwise Shift Right & Left:
The bitwise shift right operator shifts the specified bits to the right or left. First we convert both operands to
binary then shift bits to right or left.The binary digits equivalent decimal is the result.
211
PERL [Unit-VIII]
8.3.4.2 Precedence
a) Operator Precedence
The following table displays the Operator Precedence in Perl.
Associativity
left
left
nonassoc
right
right
left
left
left
left
nonassoc
nonassoc
nonassoc
left
left
left
left
nonassoc
right
right
left
nonassoc
right
left
left
b) Contexts
Operators
Terms and list operators (leftward)
->
++ -**
! ~ n + - (unary)
=~ !~
*/%x
+-.
<< >>
named unary operators
< > <= >= lt gt le ge
== != <=> eq ne cmp
&
|^
&&
||
.. ...
?:
= += -= *= etc. (assignment operators)
, =>
List operators (rightward)
not
and
or xor
212
PERL [Unit-VIII]
In a numerical context, this variable returns the numerical error number of the last error that
occurred, and in a string context, the associated message. The interpreter uses this context as the basis for
the conversion of values into the internal integer, floating point, or string values that the scalar value is
divided into.
e) Boolean Context
Boolean context is where an expression is used solely to identify a true or false value.
See the Logical Values section, later in the chapter, to see how Perl treats individual
values and constants in a logical context.
1) Void Context
2) Interpolative Context
3) Logical Values
Value
Logical Value
Negative
number True
False
Positive number
True
Empty string
False
Non-empty string
True
Undefined value
False
Empty list
False
True
Variable names can start with a letter, a number, or an underscore, although they normally begin
with a letter and can then be composed of any combination of letters, numbers, and the underscore
character.
Variables can start with a number, but they must be entirely composed of that number; for example,
$123 is valid, but $1var is not.
213
PERL [Unit-VIII]
Variable names that start with anything other than a letter, digit, or underscore are generally reserved
for special use by Perl (see Special Variables later in this chapter).
Variable names are case sensitive; $foo, $FOO, and $fOo are all separate variables as far as Perl is
concerned.
As an unwritten (and therefore unenforced) rule, names all in uppercase are constants.
All scalar values start with $, including those accessed from an array of hash, for example $array[0]
or $hash{key}.
All array values start with @, including arrays or hashes accessed in slices, for example
@array[3..5,7,9] or @hash{bob, alice}.
Namespaces are separate for each variable typethe variables $var, @var, and %var are all different
variables in their own right.
In situations where a variable name might get confused with other data (such as when embedded
within a string), you can use braces to quote the name. For example, ${name}, or %{hash}.
8.4.3 Literals :
Literal is a value that is represented "as is" or hard-coded in your source code. When you see the four
characters 45.5 in programs it really refers to a value of forty-five and a half. Perl uses four types of literals.
Here is a quick glimpse at them:
Numbers - This is the most basic data type.
Open Source Software
214
PERL [Unit-VIII]
'can\'t'
'hi\there'
'blah\\blah'
ii)Double-Quoted-Strings:
Double quoted strings act more like strings in C or C++ the backslash allows you to represent control
characters. Another nice feature Double-Quoted strings offers is variable interpolation this substitutes the
value of a variable into the string.
Some of the things you can put in a Double-Quoted String
Representation
What it Means
\a
Bell
\b
Backspace
\e
Escape
\f
Formfeed
\n
Newline
\r
Return
\t
Tab
\\
Backslash
\"
Double quote
\007
octal ascii value this time 007 or the bell
\x07
hex ascii value this time 007 or the bell
Open Source Software
215
PERL [Unit-VIII]
\cD
\l
\u
\L
\U
\Q
\E
b) Quotes:
It is important to note that DOUBLE quotes " and SINGLE quotes ' can have a very different effect
in PERL. Single quotes are literal. They will pass on exactly what is inside of them. Conents are not
analyzed for variables.
c) Interpolation of Array Values :
When you embed an array into a string, the elements of the array are included in order
separated by the contents of the $ special variable, which by default is a space:
@list = ('hello', 'world');
print "@list\n"; # Outputs 'hello world'
Perl will determine whether the name you have supplied is correct, and itll raise an
error if youve tried to interpolate an array that doesnt exist. This can lead to problems:
print "mc@mcslp.com";
In this instance, Perl is expecting to find an array called @mcslp, and it will obviously
fail because we havent defined such an array. Generally, Perl will warn you of this
error during compilation and tell you to escape the @ sign:
print "mc\@mcslp.com@;
i)
Arrays:
An array is just a set of scalars. It's made up of a list of individual scalars that are stored within a single
variable. You can refer to each scalar within that list using a numerical index.
ii)
Array Creation:
Array variables are prefixed with the @ sign and are populated using either parentheses or the qw
operator. For example:
@array = (1, 2, 'Hello');
@array = qw/This is an array/;
The second line uses the qw// operator, which returns a list of strings, separating the delimited string by
white space.
Open Source Software
216
PERL [Unit-VIII]
@days = qw/Monday
Tuesday
...
Sunday/;
We can also populate an array by assigning each value individually:
$array[0] = 'Monday';
...
$array[6] = 'Sunday';
iii)
When extracting individual elements from an array, you must prefix the variable with a dollar sign and then
append the element index within square brackets after the name. For example:
#!/usr/bin/perl
PERL offers a shortcut for sequential numbers and letters. Rather than typing out each element when
counting to 100 for example, we can do something like this:
#!/usr/bin/perl
@10 = (1 .. 10);
@100 = (1 .. 100;
Open Source Software
217
PERL [Unit-VIII]
Array Size:
The size of an array can be determined using scalar context on the array - the returned value will be the
number of elements in the array:
@array = (1,2,3);
print "Size: ",scalar @array,"\n";
The value returned will always be the physical size of the array, not the number of valid elements. You can
demonstrate this, and the difference between scalar @array and $#array, using this fragment:
#!/uer/bin/perl
@array = (1,2,3);
$array[50] = 4;
print "Size: ",scalar @array,"\n";
print "Max Index: ", $#array,"\n";
This will return
Size: 51
Max Index: 50
There are only four elements in the array that contain information, but the array is 51 elements long, with a
highest index of 50.
vi)
When adding elements using push() or shift() you must specify two arguments, first the array name and
second the name of the element to add. Removing an element with pop() or shift() only requires that you
send the array as an argument.
Open Source Software
218
PERL [Unit-VIII]
#!/usr/bin/perl
# Define an array
@coins = ("Quarter","Dime","Nickel");
print "First Statement : @coins";
print "\n";
# Add one element at the end of the array
push(@coins, "Penny");
print "Second Statement : @coins";
print "\n";
# Add one element at the beginning of the array
unshift(@coins, "Dollar");
print "Third Statement : @coins";
print "\n";
You can also extract a "slice" from an array - that is, you can select more than one item from an array in
order to produce another array.
@weekdays = @shortdays[0,1,2,3,4];
Open Source Software
219
PERL [Unit-VIII]
The specification for a slice must a list of valid indices, either positive or negative, each separated by a
comma. For speed, you can also use the .. range operator:
@weekdays = @shortdays[0..4];
Ranges also work in lists:
@weekdays = @shortdays[0..2,6,7];
viii)
Replacing elements is possible with the splice() function. Splice() requires a handful of arguments and the
formula reads:
splice(@array,first-element,sequential_length, new elements)
Essentially, you send PERL an array to splice, then direct it to the starting element, count through how many
elements to replace, and then fill in the missing elements with new information.
#!/usr/bin/perl
@nums = (1..20);
splice(@nums, 5,5,21..25);
print "@nums";
Here actual replacement begins after the 5th element, starting with the number 6. Five elements are then
replaced from 6-10 with the numbers 21-25
ix)
With the split function, it is possible to transform a string into an array. To do this simply define an array
and set it equal to a split function. The split function requires two arguments, first the character of which to
split and also the string variable.
#!/usr/bin/perl
# Define Strings
$astring = "Rain-Drops-On-Roses-And-Whiskers-On-Kittens";
$namelist = "Larry,David,Roger,Ken,Michael,Tom";
# Strings are now arrays. Here '-' and ',' works as delimeter
@array = split('-',$astring);
@names = split(',',$namelist);
print $array[3]; # This will print Roses
print "\n";
220
PERL [Unit-VIII]
x)
Sorting Arrays:
The sort() function sorts each element of an array according to ASCII Numeric standards.
#!/usr/bin/perl
# Define an array
@foods = qw(pizza steak chicken burgers);
print "Before sorting: @foods\n";
# Sort this array
@foods = sort(@foods);
print "After sorting: @foods\n";
This will produce following result
Before sorting: pizza steak chicken burgers
After sorting: burgers chicken pizza steak
c) The $[ Special Variable:$[ is a special variable. This particular variable is a scalar containing the first index of all arrays. because
Perl arrays have zero-based indexing, $[ will almost always be 0. But if you set $[ to 1 then all your arrays
will use on-based indexing. It is recommended not to use any other indexing
d) Hashes:Hashes are an advanced form of array. One of the limitations of an array is that the information
contained within it can be difficult to get to. For example, imagine that you have a list of people and their
ages.
The hash solves this problem very neatly by allowing us to access that @ages array not by an index,
but by a scalar key. For example to use age of different people we can use thier names as key to define a
hash.
%ages = ('Martin' => 28,
'Sharon' => 35,
'Rikke' => 29,);
221
PERL [Unit-VIII]
e) Creation of Hash:Hashes are created in one of two ways. In the first, you assign a value to a named key on a one-by-one basis:
$ages{Martin} = 28;
In the second case, you use a list, which is converted by taking individual pairs from the list: the first
element of the pair is used as the key, and the second, as the value. For example,
%hash = ('Fred' , 'Flintstone', 'Barney', 'Rubble');
For clarity, you can use => as an alias for , to indicate the key/value pairs:
%hash = ('Fred' => 'Flintstone',
'Barney' => 'Rubble');
i)
Sorting/Ordering Hashes:
There is no way to simply guarantee that the order in which a list of keys, values, or key/value pairs will
always be the same. In fact, it's best not even to rely on the order between two sequential evaluations:
#!/usr/bin/perl
print(join(', ',keys %hash),"\n");
print(join(', ',keys %hash),"\n");
If you want to guarantee the order, use sort, as, for example:
print(join(', ',sort keys %hash),"\n");
If you are accessing a hash a number of times and want to use the same order, consider creating a single
array to hold the sorted sequence, and then use the array (which will remain in sorted order) to iterate over
the hash. For example:
my @sortorder = sort keys %hash;
foreach my $key (@sortorder)
ii)
Hash Size
You get the size - that is, the number of elements - from a hash by using scalar context on either keys or
values:
#!/usr/bin/perl
%ages = ('Martin' => 28, 'Sharon' => 35, 'Rikke' => 29);
print "Hash size: ",scalar keys %ages,"\n";
222
PERL [Unit-VIII]
Adding a new key/value pair can be done with one line of code using simple assignment operator. But to
remove an element from the hash you need to use delete function.
#!/usr/bin/perl
%ages = ('Martin' => 28, 'Sharon' => 35, 'Rikke' => 29);
The Lists:
Lists are really a special type of array - .essentially, a list is a temporary construct that holds a series of
values. The list can be "hand" generated using parentheses and the comma operator,
@array = (1,2,3);
or it can be the value returned by a function or variable when evaluated in list context:
print join(',' @array);
Here, the @array is being evaluated in list context because the join function is expecting a list.
ii)
Because a list is just a comma-separated sequence of values, you can combine lists together:
@numbers = (1,3,(4,5,6));
The embedded list just becomes part of the main list.this also means that we can combine arrays together:
@numbers = (@odd,@even);
Functions that return lists can also be embedded to produce a single, final list:
@numbers = (primes(),squares());
223
PERL [Unit-VIII]
iii)
The list notation is identical to that for arrays - .you can extract an element from an array by appending
square brackets to the list and giving one or more indices:
#!/usr/bin/perl
$one = (5,4,3,2,1)[4];
224
PERL [Unit-VIII]
h) Typeglobs:
Typeglobs are immensely useful. They allow us to efficiently create aliases of symbols, which is
the basis for a very important module called Exporter that is used in a large number of freely available
modules
Typeglobs can also be aliased to ordinary references in such a way that you don't have to use the
dereferencing syntax; this is not only easier on the eye, it is faster too.
Closely related to typeglobs and symbol tables is the subject of dynamic versus lexical scoping
(using local versus my ). There are a couple of useful idioms that arise from these differences.
PERL [Unit-VIII]
225
Syntax
defined EXPR
defined
Example
#!/usr/bin/perl
$var1 = "This is defined";
if( defined($var1) ){
print "$var1\n";
}
if( defined($var2) ){
print "var2 is also defined\n";
}else{
Open Source Software
226
PERL [Unit-VIII]
Value
_ _LINE_ _
_ _FILE_ _
__PACKAGE__
__END__
__DATA__
PERL [Unit-VIII]
227
structure. The above code example is a valid piece of Perl code that can appear just about anywhere in a Perl
program. Of course, it is only particularly useful for those functions and structures that use blocks.
PERL [Unit-VIII]
228
}
The fourth form allows for additional tests if the first expression does not return true. The elsif can be
repeated an infinite number of times to test as many different alternatives as are required:
if ($date == $today)
{
print "Happy Birthday!\n";
}
elsif ($date == $christmas)
{
print "Happy Christmas!\n";
}
The fifth form allows for both additional tests and a final exception if all the other tests fail:
if ($date == $today)
{
print "Happy Birthday!\n";
}
elsif ($date == $christmas)
{
print "Happy Christmas!\n";
}else
{
print "Happy Unbirthday!\n";
}
The unless statement automatically implies the logical opposite of if, so unless the EXPR is true, execute
the block. This means that the statement
print "Happy Unbirthday!\n" unless ($date == $today);
is equivalent to
print "Happy Unbirthday!\n" if ($date != $today);
For example, the following is a less elegant solution to the preceding if...else. Although it achieves the same
result, example:
unless ($date != $today)
PERL [Unit-VIII]
229
{
print "Happy Unbirthday!\n";
}
else
{
print "Happy Birthday!\n";
}
The final conditional statement is actually an operator.the conditional operator. It is synonymous with the
if...else conditional statement but is shorter and more compact. The format for the operator is:
(expression) ? (statement if true) : (statement if false)
For example, we can emulate the previous example as follows:
($date == $today) ? print "Happy B.Day!\n" : print "Happy Day!\n";
8.5.3 Loops:
Perl supports four main loop types:
1. while
2. for
3. until
4. foreach
In each case, the execution of the loop continues until the evaluation of the supplied expression changes.
In the case of a while loop execution continues while the expression evaluates to true.
The until loop executes while the loop expression is false and only stops when the expression
evaluates to a true value.
The list forms of the for and foreach loop are special cases. They continue until the end of the
supplied list is reached.
while Loops
PERL [Unit-VIII]
230
This is equivalent to
do
{
$calc += ($fact*$ivalue);
} while $calc <100;
Open Source Software
PERL [Unit-VIII]
231
for Loops
A for loop is basically a while loop with an additional expression used to reevaluate the original conditional
expression. The basic format is:
LABEL for (EXPR; EXPR; EXPR) BLOCK
The first EXPR is the initialization - the value of the variables before the loop starts iterating. The second is
the expression to be executed for each iteration of the loop as a test. The third expression is executed for
each iteration and should be a modifier for the loop variables.
Thus, you can write a loop to iterate 100 times like this:
for ($i=0;$i<100;$i++)
{
...
}
You can place multiple variables into the expressions using the standard list operator (the comma):
for ($i=0, $j=0;$i<100;$i++,$j++)
You can create an infinite loop like this:
for(;;)
{
...
}
foreach Loops
he last loop type is the foreach loop, which has a format like this:
LABEL foreach VAR (LIST) BLOCK
LABEL foreach VAR (LIST) BLOCK continue BLOCK
Using a for loop, you can iterate through the list using:
for ($index=0;$index<=@months;$index++)
{
print "$months[$index]\n";
}
This is messy, because you.re manually selecting the individual elements from the array and using an
additional variable, $index, to extract the information. Using a foreach loop, you can simplify the process:
Open Source Software
PERL [Unit-VIII]
232
foreach (@months)
{
print "$_\n";
}
The foreach loop can even be used to iterate through a hash, providing you return the list of values or keys
from the hash as the list:
foreach $key (keys %monthstonum)
{
print "Month $monthstonum{$key} is $key\n";
}
This is equivalent to
for (my $i = 0; $i < 100; $i++)
{ ... }
8.5.5 Labels:
Labels can be applied to any block, but they make the most sense on loops. By giving your loop a name, you
allow the loop control keywords to specify which loop their operation should be applied to. The format for a
labeled loop is:
Open Source Software
PERL [Unit-VIII]
233
PERL [Unit-VIII]
234
if (s#\\$#)
{
$_ .= <DATA>;
redo;
}
}
Here is an example showing how labels are used in inner and outer loops
OUTER:
while(<DATA>)
{
chomp;
@linearray=split;
foreach $word (@linearray)
{
next OUTER if ($word =~ /next/i)
}
}
goto
There are three basic forms: goto LABEL, goto EXPR, and goto &NAME. In each case, execution is
moved from the current location to the destination.
In the case of goto LABEL, execution stops at the current point and resumes at the point of the label
specified.
The goto &NAME statement is more complex. It allows you to replace the currently executing subroutine
with a call to the specified subroutine instead.
a) Functions:
Definition:
Function is a block of source code which does one or some tasks with specified purpose.
Advantages:
It reduces the Complexity in a program by reducing the code.
It also reduces the Time to run a program.In other way,Its directly proportional to Complexity.
Open Source Software
PERL [Unit-VIII]
235
Its easy to find-out the errors due to the blocks made as function definition outside the main function.
Subroutines, like variables, can be declared and defined.
Various forms:
sub NAME
sub NAME PROTO
sub NAME ATTRS
sub NAME PROTO ATTRS
where as
PROTO Prototype, ATTRS Attributes
Examples:
sub message
{
print "Hello!\n";
}
To call the subroutine and get a result,
add(1,2);
b) Arguments:
In perl we do not declare arguments a function takes while we defining a function, we still may pass
arguments to a Perl function. All arguments are passed into a function through the special array @_. Thus,
we can send as many arguments as we want and also function may receive different number of arguments.
Example 1:
simple function adds two numbers and prints the result:
sub add
{
$result = $_[0] + $_[1];
print "The result was: $result\n";
}
To call the subroutine and get a result,
add(1,2);
Example 2:
submy_args
{
my $i;
print "My arguments:\n";
for($i=0;$i<=$#_;$i++){
Open Source Software
PERL [Unit-VIII]
236
237
PERL [Unit-VIII]
{
return($a,$b);
}
$a = 5;
$b = 6;
@c = list_of_a_and_b();
# @c gets (5,6)
The last expression evaluated really means the last expression evaluated, rather than the last expression
defined in the body of the subroutine. For example, this subroutine returns
$a if $a >0 ; otherwise it returns $b :
d) Error Notification:
The easiest way to report an error within a subroutine is to return the undefined value
undef function.
i)
undef:
Undefines the value of EXPR. Use on a scalar, list, hash, function, or typeglob. Use on a hash with a
statement such as undef $hash{$key}; actually sets the value of the specified key to an undefined value. If
you want to delete the element from the hash, use the delete function.
Syntax
undef EXPR
undef
Example
#!/usr/bin/perl -w
$scalar = 10;
@arrary = (1,2);
print "1 - Value of Scalar is $scalar\n";
print "1 - Value of Array is @array\n";
undef( $scalar );
undef( @array );
print "2 - Value of Scalar is $scalar\n";
print "2 - Value of Array is @array\n";
238
PERL [Unit-VIII]
1 - Value of Scalar is 10
1 - Value of Array is
Use of uninitialized value in concatenation (.) or
string at test.pl line 13.
2 - Value of Scalar is
2 - Value of Array is
e) Context :
If you write a Perl program, you refer to lists with an "@" symbol in front of the list variable name. But
depending on the context of how you write it, the program may interpret it as
The length of the list (i.e. the element count) if that's the only thing that makes sense.
A space separated string with all the items in the list joined together (if it's in double quotes).
For example:
@salad = ("apple","banana","cherry");
$salad[3] = "tomato";
$salad[8] = "fig";
print @salad."\n"; # scalar context
print "@salad.\n"; # double quote context
f) will display:
applebananacherrytomatofig
9
apple banana cherry tomato
fig.
g) Attributes:
Attributes allow you to add extra semantics to any Perl subroutine or variable.Currently Perl supports only
three attributes: locked, method, and lvalue
i)
239
PERL [Unit-VIII]
one also marked with the method attribute), Perl ensures that any invocation of it implicitly locks its first
argument (the object) before execution.
Semantics of this lock are the same as using the lock operator on the subroutine as the first statement
in that routine.
ii)
subxyzzy : 5x5;
subplugh : Y2::north;
subsnurt : foo + bar;
The attribute list is passed as a list of constant strings to the code that associates them with the
subroutine. Exactly how this works (or doesn't) is highly experimental. Check attributes(3) for current
details on attribute lists and their manipulation.
iii)
Using lvalue, a subroutine can be used as a modifiablescalar value. For example, you can do this:
mysub() = 5;
This is particularly useful in situations where you want to use a method on an object toaccept a setting,
instead of setting the value on the object directly.
For example:
submysub : lvalue
{
Open Source Software
PERL [Unit-VIII]
240
$val;
}
h) Prototypes:
Perl's prototypes are not the prototypes in other languages. Other languages use prototypes to define,
name and type the arguments to a subroutine. Perl prototypes are about altering the context in which the
subroutine's arguments are evaluated, instead of the normal list context. Perl prototypes should not be used
merely as a check that the right arguments have been passed in. For that use a module such as
I)Params::Validate.
The prototype of a function can be queried by calling prototype(). Prefix built-in functions with
"CORE::". prototype("CORE::abs").
The prototype function Returns a string containing the prototype of the function or reference
specified by EXPR, or undef if the function has no prototype.
A subroutine's prototype is defined in its declaration, and enforces constraints on the interpretation of
arguments passed to it. For example:
Calling a function with a leading & (&foo) disables prototypes. But you shouldn't be calling
functions like that.
8.6.2 Packages:
A package is a collection of code which lives in its own namespace. A namespace is a named collection
of unique variable names (also called a symbol table).
Namespaces prevent variable name collisions between packages. Packages enable the construction of
modules which, when used, won't clobber variables and functions outside of the modules own namespace.
PERL [Unit-VIII]
241
PERL [Unit-VIII]
242
c) Special Blocks
BEGIN and END Blocks
You may define any number of code blocks named BEGIN and END which act as constructors and
destructors respectively.
BEGIN
END {
BEGIN
END {
{ ... }
... }
{ ... }
... }
Every BEGIN block is executed after the perl script is loaded and compiled but before any other
statement is executed.Every END block is executed just before the perl interpreter exits.
The BEGIN and END blocks are particularly useful when creating Perl modules.
8.6.3 Modules:
Modules are the loadable libraries of the Perl world. A Perl module is a reusable package defined in
a library file whose name is the same as the name of the package (with a .pm on the end). A Perl module is a
self-contained piece of [Perl] code that can be used by a Perl program.
A Perl module file called "Foo.pm" might contain statements like this.
#!/usr/bin/perl
package Foo;
sub bar
{
print "Hello $_[0]\n"
}
sub blat {
print "World $_[0]\n"
}
1;
a) Few noteable points about modules
Both use the list of search paths in @INC to find the module (you may modify it!)
The 1; at the bottom causes eval to evaluate to TRUE (and thus not fail)
243
PERL [Unit-VIII]
b) Creating Modules:
There are two ways for a module to make its interface available to your program: by exporting
symbols or by allowing method calls. We'll show you an example of the first style here; the second style is
for object-oriented modules and is described in the next chapter. (Object-oriented modules should export
nothing, since the whole idea of methods is that Perl finds them for you automatically, based on the type of
the object.)
To construct a module called Bestiary, create a file called Bestiary.pm that looks like this:
package
require
Bestiary;
Exporter;
our @ISA
= qw(Exporter);
# Version number
@EXPORT and @EXPORT_OK are the two main variables used during export operation.
@EXPORT contains list of symbols (subroutines and variables) of the module to be exported into the caller
namespace.
PERL [Unit-VIII]
Example:
Let us use the following sample program to understand Perl exporter.
package Arithmetic;
use Exporter;
# base class of this(Arithmetic) module
@ISA = qw(Exporter);
# Exporting the add and subtract routine
@EXPORT = qw(add subtract);
# Exporting the multiply and divide routine on demand basis.
@EXPORT_OK = qw(multiply divide);
sub add
{
my ($no1,$no2) = @_;
my $result;
$result = $no1+$no2;
return $result;
}
d) Comparing use and require:
When you import a module, you can use one of two keywords: use or require.
Use :
1. The method is used only for the modules(only to include .pm type file)
2. The included objects are varified at the time of compilation.
3. No Need to give file extension.
Syntax:
use Module;
and
use Module LIST;
Example:
use MyMathLib qw/add square/;
Require:
1. The method is used for both libraries and modules.
2. The included objects are varified at the run time.
3. Need to give file Extension.
Open Source Software
244
PERL [Unit-VIII]
245
Syntax:
require Module;
Example:
require 'Fcntl.pl';
require 5.003;
no:
If MODULE supports it, then no calls the unimport function defined in MODULE to unimport all
symbols from the current package, or only the symbols referred to by LIST.
It can be said that no is opposite of import.
Return Value
Nothing
Syntax
no Module VERSION LIST
no Module VERSION
no MODULE LIST
no MODULE
do:
When supplied a block, do executes as if BLOCK were a function, returning the value of the last
statement evaluated in the block.
When supplied with EXPR, do executes the file specified by EXPR as if it were another Perl script.
If supplied a subroutine, SUB, do executes the subroutine using LIST as the arguments, raising an
exception if SUB hasn.t been defined
Syntax
do BLOCK
do EXPR
do SUB(LIST)
Example
Following are the usage...
eval `cat stat.pl`;
is equivalent to
do 'stat.pl';
Open Source Software
PERL [Unit-VIII]
246
e) Scope:
Scope refers to the visibility of variables. In other words, which parts of your program can see or use
it. Normally, every variable has a global scope. Once defined, every part of your program can access a
variable.
It is very useful to be able to limit a variable's scope to a single function. In other words, the variable wil
have a limited scope. This way, changes inside the function can't affect the main program in unexpected
ways, func7.pl
firstSub("AAAAA", "BBBBB");
sub firstSub{
local ($firstVar) = $_[0];
my($secondVar) = $_[1];
print("firstSub: firstVar = $firstVar\n");
print("firstSub: secondVar = $secondVar\n\n");
secondSub();
print("firstSub: firstVar = $firstVar\n");
print("firstSub: secondVar = $secondVar\n\n");
}
sub secondSub{
print("secondSub: firstVar = $firstVar\n");
print("secondSub: secondVar = $secondVar\n\n");
$firstVar = "ccccC";
$secondVar = "DDDDD";
print("secondSub: firstVar = $firstVar\n");
print("secondSub: secondVar = $secondVar\n\n");
}
Open Source Software
PERL [Unit-VIII]
247
sub myfunction
{
Open Source Software
248
PERL [Unit-VIII]
local $foo;
Effects of our:
Defines the variables specified in LIST as being global within the enclosing block, file, or eval statement. It
Open Source Software
PERL [Unit-VIII]
249
is effectively the opposite of my.it declares a variable to be global within the entire scope, rather than
creating a new private variable of the same name. All other options are identical to my;
An our declaration declares a global variable that will be visible across its entire lexical scope, even across
package boundaries. The package in which the variable is entered is determined at the point of the
declaration, not at the point of use.
If more than one value is listed, the list must be placed in parentheses.
Return Value
nothing
Syntax
our EXPR
our EXPR TYPE
our EXPR : ATTRS
our TYPE EXPR : ATTRS
Example
Try out following example:
#!/usr/bin/perl -w
sub myfunction
{
our $string = "We are the function";
print "$string\n";
}
It will produce following results:
We are the world
We are the function
We are the function
PERL [Unit-VIII]
250
PERL [Unit-VIII]
251
my $constant = $AUTOLOAD;
$constant =~ s/.*:://;
return $constants{"$constant"};
}
PERL [Unit-VIII]
252
Here DATA is the file handle which will be used to read the file. Here is the example which will open a file
and will print its content over the screen.
#!/usr/bin/perl
open(DATA, "<file.txt");
while(<DATA>)
{
print "$_";
}
8.7.3 Open Function
Following is the syntax to open file.txt in writing mode. Here less than >signe indicates that file has
to be opend in writing mode
open(DATA, >file.txt);
This example actually truncates (empties) the file before opening it for writing, which may not be the
desired effect. If you want to open a file for reading and writing, you can put a plus sign before the > or <
characters.
For example, to open a file for updating without truncating it:
open(DATA, +file.txt);
To truncate the file first:
open DATA, "+>file.txt" or die Couldn't open file file.txt, $!;
You can open a file in append mode. In this mode writing point will be set to the end of the file
open(DATA,">>file.txt") || die "Couldn't open file file.txt, $!";
A double >> opens the file for appending, placing the file pointer at the end, so that you can immediately
start appending information. However, you can.t read from it unless you also place a plus sign in front of it:
open(DATA,"+>>file.txt") || die "Couldn't open file file.txt, $!";
Following is the table which gives possible values of different modes
Entities
Definition
< or r
> or w
>> or a
+< or r+
+> or w+
253
PERL [Unit-VIII]
+>> or a+
Definition
O_RDWR
O_EXCL
O_NONBLOCK
Non-Blocking usability
PERL [Unit-VIII]
254
PERL [Unit-VIII]
255
PERL [Unit-VIII]
256
257
PERL [Unit-VIII]
eof:
Returns 1 if the next read on FILEHANDLE will return end of file, or if FILEHANDLE is not open. An eof
without an argument uses the last file read. Using eof() with empty parentheses is very different. It refers to
the pseudo file formed from the files listed on the command line and accessed via the <> operator.
Return Value
undef if FILEHANDLE is not at end of file
1 if FILEHANDLE will report end of file on next read
Syntax
eof FILEHANDLE
eof()
eof
Example
# insert dashes just before last line of last file
while (<>) {
if (eof()) {
print "--------------\n";
}
print;
last if eof(); # needed if we're reading from a terminal
}
fileno:
Returns the file descriptor number (as used by C and POSIX functions) of the specified FILEHANDLE.
This is generally useful only for using the select function and any low-level tty functions.
Return Value
File descriptor (numeric) of FILEHANDLE
undef on failure
Syntax
fileno FILEHANDLE
Example
You can use this to find out whether two handles refer to the same underlying descriptor:
if (fileno(THIS) == fileno(THAT)) {
print "THIS and THAT are dups\n"; }
Open Source Software
PERL [Unit-VIII]
258
selece:
Sets the default filehandle for output to FILEHANDLE, setting the filehandle used by functions such as
print and write if no filehandle is specified. If FILEHANDLE is not specified, then it returns the name of the
current default filehandle.
select (RBITS, WBITS, EBITS, TIMEOUT )Calls the system function select( ) using the bits specified. The
select function sets the controls for handling non-blocking I/O requests. Returns the number of filehandles
awaiting I/O in scalar context, or the number of waiting filehandles and the time remaining in a list context.
Return Value
Previous default filehandle if FILEHANDLE specified
Current default filehandle if FILEHANDLE is not specified
Syntax
select FILEHANDLE
select
select RBITS, WBITS, EBITS, TIMEOUT
Example
#!/usr/bin/perl -w
open(FILE,">/tmp/t.out");
$oldHandle = select(FILE);
print("This is sent to /tmp/t.out.\n");
select($oldHandle);
print("This is sent to STDOUT.\n");
truncate:
Truncates (reduces) the size of the file specified by FILEHANDLE to the specified LENGTH (in bytes).
Produces a fatal error if the function is not implemented on your system.
Return Value
undef if the operation failed
1 on success
Syntax
truncate FILEHANDLE, LENGTH
Example
Following example will truncate file "test.txt" to zero length.
PERL [Unit-VIII]
259
#!/usr/bin/perl -w
open( FILE, "</tmp/test.txt" ) || die "Enable to open test file";
truncate( FILE, 0 );
close(FILE);
8.7 a)File Management
File Information
You can test certain features very quickly within Perl using a series of test operators known collectively as X tests.
For example, to perform a quick test of the various permissions on a file, you might use a script like this:
#/usr/bin/perl
my (@description,$size);
if (-e $file)
{
push @description, 'binary' if (-B _);
push @description, 'a socket' if (-S _);
push @description, 'a text file' if (-T _);
push @description, 'a block special file' if (-b _);
push @description, 'a character special file' if (-c _);
push @description, 'a directory' if (-d _);
push @description, 'executable' if (-x _);
push @description, (($size = -s _)) ? "$size bytes" : 'empty';
print "$file is ", join(', ',@description),"\n";
}
Here is the list of features which you can check for a file
Operator Description
-A
Age of file (at script startup) in days since modification.
-B
Is it a binary file?
-C
Age of file (at script startup) in days since modification.
-M
Age of file (at script startup) in days since modification.
-O
Is the file owned by the real user ID?
-R
Is the file readable by the real user ID or real group?
-S
Is the file a socket?
-T
Is it a text file?
-W
Is the file writable by the real user ID or real group?
-X
Is the file executable by the real user ID or real group?
-b
Is it a block special file?
-c
Is it a character special file?
-d
Is the file a directory?
-e
Does the file exist?
-f
Is it a plain file?
Open Source Software
PERL [Unit-VIII]
-g
-k
-l
-o
-p
-r
-s
-t
-u
-w
-x
-z
260
link:
Creates a new file name, NEWFILE, linked to the file OLDFILE. The function creates a hard link; if you
want a symbolic link, use the symlink function.
Return Value
0 on failure and 1 on success
PERL [Unit-VIII]
261
Syntax
link OLDFILE,NEWFILE
Example
This will create new file using existing file.
#!/usr/bin/perl
$existing_file = "/uer/home/test1";
$new_file = "/usr/home/test2";
$retval = link $existing_file, $new_file ;
if( $retval == 1 ){
print"Link created successfully\n";
}else{
print"Error in creating link $!\n";
}
symlink:
Creates a symbolic link between OLDFILE and NEWFILE. On systems that don't support symbolic links,
causes a fatal error.
Return Value
0 on failure
1 on success
Syntax
symlink ( OLDFILE, NEWFILE )
Example
First create one file test.txt in /tmp directory and then try out following example it will create a symbolic
link in the same directory:
#!/usr/bin/perl -w
symlink("/tmp/text.txt", "/tmp/symlink_to_text.txt");
unlink:
Deletes the files specified by LIST, or the file specified by $_ otherwise. Be careful while using this function
because there is no recovering once a file gets deleted.
Return Value
Number of files deleted.
Syntax: unlink LIST
Open Source Software
PERL [Unit-VIII]
262
unlink
Example
Create two files t1.txt and t2.txt in /tmp directory and then use the following program to delete these two
files.
#!/usr/bin/perl -w
263
PERL [Unit-VIII]
Example
$cnt = chmod 0755, 'foo', 'bar';
chmod 0755, @executables;
$mode = '0644'; chmod $mode, 'foo';
# --w----r-T
$mode = '0644'; chmodoct($mode), 'foo'; # this is better
$mode = 0644; chmod $mode, 'foo';
# this is best
chown:
Changes the owner (and group) of a list of files. The first two elements of the list must be the numeric uid
and gid, in that order. This funcation call works in similar way as unix command chown. Thus you should
have sufficient privilege to change the permission of the file.
Return Value
Returns the number of files successfully changed.
Syntax
chown USERID, GROUPID, LIST
Example
Here is an example which will change ownership for the given number of files.
#!/usr/bin/perl
$cnt = chown $uid, $gid, 'foo', 'bar';
chown $uid, $gid, @filenames;
utime:
Sets the access and modification times specified by ATIME and MTIME for the list of files in LIST. The
values of ATIME and MTIME must be numerical. The inode modification time is set to the current time.
The time must be in the numeric format (for example, seconds since January 1, 1970).
Return Value
Number of files updated
Syntax: utime ATIME, MTIME, LIST
Example
Create a file t.txt in /tmp directory and use the following program to set its modification and access time to
the current time.
Open Source Software
PERL [Unit-VIII]
264
#!/usr/bin/perl w
utime(time(), time(), "/tmp/t.txt");
umask:
Sets the umask (default mask applied when creating files and directories) for the current process. Value of
EXPR must be an octal number. If EXPR is omitted, simply returns the previous value.
Return Value
Previous umask value
Syntax
umask EXPR
umask
Example
#!/usr/bin/perl -w
print("The current umask is: ", umask(), "\n");
It will produce following results, you can get different result on your computer based on your setting.
The current umask is: 18
PERL [Unit-VIII]
265
mkdir:
You can make a new directory using the mkdirfunction.Makes a directory with the name and path EXPR
using the mode specified by MODE, which should be supplied as an octal value for clarity.
Return Value
Open Source Software
PERL [Unit-VIII]
266
0 on failure
1 on success
Syntax
mkdir EXPR,MODE
Example
#!/usr/bin/perl -w
$dirname ="/tmp/testdir";
mkdir $dirname, 0755;
It will produce following results in /tmp directory:
drwxr-xr-x 2 root root 4096 Mar 19 11:55 testdir
rmdir:
Deletes the directory specified by EXPR, or $_ if omitted. Only deletes the directory if the directory
is empty.
Return Value
0 on failure
1 on success
Syntax
rmdir EXPR
rmdir
Example
Create one directory testdir inside /tmp and Try out following example:
#!/usr/bin/perl -w
rmdir ("/tmp/testdir") || die ("error in deleting directory: $?");
It will produce following results:
If directory is empty then it will be deleted
otherwise error message will be generated.
File Control with fcntl:
Performs the function specified by FUNCTION, using SCALAR on FILEHANDLE. SCALAR
either contains a value to be used by the function or is the location of any returned information.
Return Value
0 but true if the return value from the fcntl()is 0 Value returned by system.
Open Source Software
267
PERL [Unit-VIII]
undef on failure
Syntax:
fcntl FILEHANDLE, FUNCTION, SCALAR
Example
use Fcntl;
fcntl($filehandle, F_GETFL, $packed_return_buffer)
or die "can't fcntl F_GETFL: $!";
I/O Control with ioctl:
The ioctl function is similar in principle to the fcntl function. It too is a Perl version of
the operating system equivalent ioctl() function.
The ioctl function is typically used to set options on devices and data streams,
usually relating directly to the operation of the terminal.
Return Value
undef on failure otherwise 0 but true if the return value from the ioctl( )is 0
Syntax
ioctl FILEHANDLE, FUNCTION, SCALAR
File Locking:
File locking is a way of ensuring the integrity of files. It allows many people (actually, processes) to
share a file in a safe way, without stepping on each other's toes.
Supports file locking on the specified FILEHANDLE using the system flock( ), fcntl( ) locking, or
lockf( ). The exact implementation used is dependent on what your system supports. OPERATION is one of
the static values defined here...
Operation
Result
LOCK_SH
LOCK_EX
LOCK_UN
LONG_NB
Return Value
0 on failure to set/unset lock
1 on success to set/unset lock
Syntax
flock FILEHANDLE, OPERATION
Example
Open Source Software
PERL [Unit-VIII]
268
PERL [Unit-VIII]
269
Returns the integer element of EXPR, or $_ if omitted. The int function does not do rounding. If you need to
round a value up to an integer, you should use sprintf.
Return Value:
Integer part of EXPR.
Example:
printint abs(-1.295476);
This should print a value of 1. The only problem with the int function is that it strictly
removes the fractional component of a number; no rounding of any sort is done. If you
want to return a number that has been rounded to a number of decimal places, use the
printf or sprintf function:
printf("%.2f",abs(-1.295476));
This will round the number to two decimal places-a value of 1.30 in this example.
Note that the 0 is appended in the output to show the two decimal places.
d) exp-Raising e to the Power
To perform a normal exponentiation operation on a number, you use the ** operator:
$square - 4**2;
This returns 16, or 4 raised to the power of 2. If you want to raise the natural base
number e to the power, you need to use the exp function:
exp EXPR
exp
If you do not supply an EXPR argument, exp uses the value of the $_variable as the
exponent. For example, to find the square of e:
$square -exp(2);
e) sqrt-the Square Root
To get the square root of a number, use the built-in sqrt function:
$var-sqrt(16384);
To calculate the nth root of a number, use the ** operator with a fractional number.
For example, the following line
$var- 16384**(1/2);
is identical to
$var-sqrt(16384);
To find the cube root of 16,777,216, you might use
$var- 16777216**(1/3);
which should return a value of 256.
Open Source Software
PERL [Unit-VIII]
5log-the Logarithm
To find the logarithm (base e) of a number, you need to use the log function:
$log - log 1.43;
Trigonometric Functions
There are three built-in trigonometric functions for calculating the arctangent squared
(atan2), cosine (cos), and sine (sin) of a value:
atan2 X,Y
cos EXPR
sin EXPR
If you need access to the arcsine, arccosine, and tangent, then use the POSIX
module, which supplies the corresponding acos, asin, and tan functions.
Unless you are doing trigonometric calculations, there is little use for these
functions in everyday life. However, you can use the sin function to calculate your
biorhythms using the simple script shown next, assuming you know the number
of days you have been alive:
my ($phys_step, $emot_step, $inte_step) - (23, 28, 33);
use Math::Complex;
print "Enter the number of days you been alive:\n";
my $alive -<STDIN>;
$phys-int(sin(((pi*($alive%$phys_step))/($phys_step/2)))*100);
$emot-int(sin(((pi*($alive%$emot_step))/($emot_step/2)))*100);
$inte-int(sin(((pi*($alive%$inte_step))/($inte_step/2)))*100);
print "Your Physical is $phys%, Emotional $emot%, Intellectual
$inte%\n";
Conversion between Bases
Perl provides automatic conversion to decimal for numerical literals specified in
binary, octal, and hexadecimal. However, the translation is not automatic on values
contained within strings, either those defined using string literals or from strings
imported from the outside world (files, user input, etc.).
To convert a string-based literal, use the oct or hex functions. The hex function
converts only hexadecimal numbers supplied with or without the 0x prefix. For
example, the decimal value of the hexadecimal string ff47ace3 (42,828,873,954) can
be displayed with either of the following statements:
print hex("ff47ace3");
Open Source Software
270
PERL [Unit-VIII]
print hex("0xff47ace3");
The hex function doesnt work with other number formats, so for strings that start
with 0, 0b, or 0x, you are better off using the oct function. By default, the oct function
interprets a string without a prefix as an octal string and raises an error if it doesnt see
it. So this
printoct("755");
is valid, but this
printoct("aef");
will fail.
If you supply a string using one of the literal formats that provides the necessary
prefix, oct will convert it, so all of the following are valid:
printoct("0755");
printoct("0x7f");
printoct("0b00100001");
Both oct and hex default to using the $_ variable if you fail to supply an argument.
To print out a decimal value in hexadecimal, binary, or octal, use printf, or use
sprintf to print a formatted base number to a string:
printf ("%lb %lo %lx", oct("0b00010001"), oct("0755"), oct("0x7f"));
See printf in Chapter 7 for more information.
Conversion Between Characters and Numbers:
If you want to insert a specific character into a string by its numerical value, you can
use the \0 or \x character escapes:
print "\007";
print "\x07";
These examples print the octal and hexadecimal values; in this case the bell
character. Often, though, it is useful to be able to specify a character by its decimal
number and to convert the character back to its decimal equivalent in the ASCII table.
The chr function returns the character matching the value of EXPR, or $_if EXPR is
not specified. The value is matched against the current ASCII table for the operating
system, so it could reveal different values on different platforms for characters with an
ASCII value of 128 or higher. This may or may not be useful.
The ord function returns the numeric value of the first character of EXPR, or $_ if
EXPR is not specified. The value is returned according to the ASCII table and is always
unsigned.
Open Source Software
271
PERL [Unit-VIII]
272
PERL [Unit-VIII]
273
PERL [Unit-VIII]
274
When that little script is run through the Perl interpreter it will print the number 17, which is the length of
that string (i.e., the number of characters in the Perl string).
Case Modifications
The four basic functions are lc, uc, lcfirst, and ucfirst. They convert a stringto all lowercase, all
uppercase, or only the first character of the string to lowercase oruppercase, respectively.
For Example:
$string = "The Cat Sat on the Mat";
printlc($string) # Outputs 'the cat sat on the mat'
printlcfirst($string) # Outputs 'the Cat Sat on the Mat'
printuc($string) # Outputs 'THE CAT SAT ON THE MAT'
printucfirst($string) # Outputs 'The Cat Sat on the Mat'
These functions can be useful for normalizing a string into an all uppercase or
lowercase formatuseful when combining and de-duping lists when using hashes.
PERL [Unit-VIII]
275
Instead of looping through every occurrence of a letter in a string to find the last one, you can use the
rindex() function. This works exactly the same as index() but it starts at the end of the string. The index
value returned is string from the start of the string though.
Example:
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'perlmeme.org';
my $char = 'e';
my $result = rindex($string, $char);
print "Result: $result\n"
Extracting Substrings
The substr() function is used to return a substring from the expression supplied as it's first argument.
The function is a good illustration of some of the ways in which Perl is different from other languages you
may have used. substr() has a variable number of arguments, it can be told to start at an offset from either
end of the expression, you can supply a replacement string so that it replaces part of the expression as well
as returning it, and it can be assigned to!.
Example:
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'Now is the time for all good people to come to the aid of their party';
my $fragment = substr $string, 4;
print " string: <$string>\n";
print "fragment: <$fragment>\n";
Stacks
One of the most basic uses for an array is as a stack. If you consider that an array is a
list of individual scalars, it should be possible to treat it as if it were a stack of papers.Index 0 of the array is
the bottom of the stack, and the last element is the top. You canput new pieces of paper on the top of the
stack (push), or put them at the bottom(unshift). You can also take papers off the top (pop) or bottom (shift)
of the stack.
Push and Pop
In perl stacks are easy to be implemented. Stacks which follows LIFO (Last In First Out) data
structure can be handled in perl with 'POP' and 'PUSH' keywords.
Open Source Software
PERL [Unit-VIII]
276
POP Example:
my $strTestPOP(@arrTest);
print "$strTest";
In this example the Last element present in @arrTest will be poped and will be assigned to variable $strTest
(LIFO). Suppose the array contains value "STAR" at it's last position thus the next line of the code will
populate STAR.
PUSH Example:
PUSH(@arrTest $strTest) if ($strTest);
In this example we are pushing $strTest variable to the array @arrTest. After the PUSH statement the array
size will be increased by one and the last element of the array will be the value contained in variable
$strTest.
Shift And Unshift:
The shift function returns the first value in an array, deleting it and shifting theelements of the array
list to the left by one.
Splicing Arrays
Perl's splice() function is used to cut out and return a chunk or portion of an array. The portion that is
cut out starts at the OFFSET element of the array and continues for LENGTH elements. If the LENGTH is
not specified, it will cut to the end of the array.
Example:
@myNames = ('Jacob', 'Michael', 'Joshua', 'Matthew', 'Ethan', 'Andrew');
@someNames = splice(@myNames, 1, 3);
Join:
Perl's join() function is used to connect all the elements of a specific list or array into a single string
with the JOINER expression. The list is concatenated into one string with the element contained in the
JOINER variable in between each item.
Syntax:
$STRING = join(JOINER, @LIST);
Example:
@myNames = ('Jacob', 'Michael', 'Joshua', 'Matthew');
$someNames = join(', ', @myNames);
Think of the @myNames array as a row of numbered boxes, each with a name in it. The join()
function would take all of the content in those boxes in the @myNames array and connect them together
Open Source Software
PERL [Unit-VIII]
277
with a comma and a space. The value of @someNames then becomes "Jacob, Michael, Joshua, Matthew".
The list of names is concatenated, or joined, by commas.
split:
Perl's split() function is used to break a string into an array on a specific pattern. The PATTERN is a
regular expression that can be as simple as a single character. By default the STRING is split on every
instance of the PATTERN, but you can LIMIT it to a specific number of instances.
Syntax:
@LIST = split(/PATTERN/, STRING, LIMIT);
Example:
$myNames = "Jacob,Michael,Joshua,Matthew,Ethan,Andrew";
@nameList = split(/,/, $myNames);
In the above example, the split function takes a plain string of names, separated by commas. It then
breaks the string apart on each instance of the comma, putting the results into the array @nameList. The
value of @nameList would then be:
@myNames = ('Jacob', 'Michael', 'Joshua', 'Matthew', 'Ethan', 'Andrew');
By using the LIMIT option, you can split the array into a specific size. For example:
$myNames = "Jacob,Michael,Joshua,Matthew,Ethan,Andrew";
@nameList = split(/,/, $myNames, 3);
This would split the array into 3 chunks, so the value of @nameList would then be:
@myNames = ('Jacob', 'Michael', 'Joshua,Matthew,Ethan,Andrew');
grep:
Perl's grep() function runs a regular expression on each element of an array, and returns only the
elements that evaluate to true. Using regular expressions can be extremely powerful and complex. You can
find out more about them in the regular expressions topic.
Syntax:
@LIST = grep(EXPRESSION, @ARRAY);
Example:
@myNames = ('Jacob', 'Michael', 'Joshua', 'Matthew', 'Alexander', 'Andrew');
@grepNames = grep(/^A/, @myNames);
Think of the @myNames array as a row of numbered boxes, going from left to right, numbered
starting with a zero. The grep() function goes through each of the elements (boxes) in the array, and
compares their contents to the regular expression. If the result is true, the contents are then added to the new
@grepNames array.
Open Source Software
PERL [Unit-VIII]
278
In the above example, the regular expression is looking for any value that starts with a capital A.
After sifting through the contents of the @myNames array, the value of @grepNames becomes ('Alexander',
'Andrew').
One quick way to make this particular function more powerful is to reverse the regular expression
with the NOT operator. The regular expression then looks for elements that evaluate to false and moves
them into the new array.
@myNames = ('Jacob', 'Michael', 'Joshua', 'Matthew', 'Alexander', 'Andrew');
@grepNames = grep(!/^A/, @myNames);
In the above example, the regular expression is looking for any value that does not start with a
capital A. After sifting through the contents of the @myNames array, the value of @grepNames becomes
('Jacob', 'Michael', 'Joshua', 'Matthew').
map:
Perl's map() function runs an expression on each element of an array, and returns a new array with
the results.
Syntax:
@LIST = map(EXPRESSION, @ARRAY);
Example
@myNames = ('jacob', 'alexander', 'ethan', 'andrew');
@ucNames = map(ucfirst, @myNames);
Think of the @myNames array as a row of numbered boxes, going from left to right, numbered
starting with a zero. The map() function goes through each of the elements (boxes) in the array, and runs the
EXPRESSION on their contents. The altered contents are then added to the new @ucNames array.
In the above example, EXPRESSION is the built-in function ucfirst that makes the first letter of the
word upper case. After running the expression on each element of the @myNames array, the value of
@ucNames becomes ('Jacob', 'Alexander', 'Ethan', 'Andrew').
sort:
With any list, it can be useful to sort the contents. Doing this manually is a complex process, so Perl
provides a built-in function that takes a list and returns a lexically sorted version.
Syntax:
sort @array;
Example:
Or to specify an explicit lexical subroutine:
sort { $a cmp $b } @array;
reverse:
Open Source Software
PERL [Unit-VIII]
279
Perl's reverse() function is used to reverse the order of an array. It should be noted that the function
returns a reversed array but does not actually reverse the array passed into the function.
Syntax:
@REVERSED_LIST = reverse(@ARRAY);
Example:
@myNames = ('Jacob', 'Michael', 'Ethan', 'Andrew');
@reversedNames = reverse(@myNames);
Think of the @myNames array as a row of numbered boxes, going from left to right, numbered
starting with a zero. The reverse() function would copy the contents of the boxes from the @myNames array
into the @reversedNames array, while reversing their order. The value of @reversedNames then becomes
('Andrew', 'Ethan', 'Michael', 'Jacob'), and @myNames remains the same.
Regular Expressions
Regular expressions are very powerful tools for matching, searching, and replacing text.
Unfortunately, they are also very obtuse. It does not help that most explanations of regular expressions start
from the specification.
A regular expression is a string of characters that define the pattern or patterns you are viewing. The
syntax of regular expressions in Perl is very similar to what you will find within other regular expression.
Pattern Modifiers
Pattern Modifiers change the behavior of the individual operator.
Modifier
/i
Meaning
Ignore alphabetic case distinctions (case insensitive).
/s
/m
/x
/o
The /i modifier says to match both upper- and lowercase (and title case, under Unicode).
The /s and /m modifiers don't involve anything kinky. Rather, they affect how Perl treats matches against a
string that contains newlines. But they aren't about whether your string actually contains newlines.
The /m modifier changes the interpretation of the ^ and $ metacharacters by letting them match next to
newlines within the string instead of considering only the ends of the string.
The /o modifier controls pattern recompilation.
The /x is the expressive modifier: it allows you to exploit whitespace and explanatory comments in order to
expand your pattern's legibility, even extending the pattern across newline boundaries.
Open Source Software
PERL [Unit-VIII]
280
PERL [Unit-VIII]
281
#!/usr/bin/perl
@list = qw/food foosball subeo footnote terfootcanicfootbrdige/;
foreach (@list)
{
$first = $1 if ?(foo.*)?;
$last = $1 if /(foo.*)/;
}
PERL [Unit-VIII]
282
Translation
Translation is similar, but not identical, to the principles of substitution, but unlike substitution,
translation (or transliteration) does not use regular expressions for its search on replacement values. The
translation operators are:
tr/SEARCHLIST/REPLACEMENTLIST/cds
y/SEARCHLIST/REPLACEMENTLIST/cds
The translation replaces all occurrences of the characters in SEARCHLIST with the corresponding
characters in REPLACEMENTLIST. For example, using the "The cat sat on the mat." string we have been
using in this chapter:
#/user/bin/perl
$string = 'The cat sat on the mat';
$string =~ tr/a/o/;
print "$string\n";
This will produce following result
The cot sot on the mot.
Standard Perl ranges can also be used, allowing you to specify ranges of characters either by letter or
numerical value. To change the case of the string, you might use following syntax in place of the uc
function.
$string =~ tr/a-z/A-Z/;
Translation Operator Modifiers
Following is the list of operators related to translation
Modifier Description
c
Complement SEARCHLIST.
d
Delete found but unreplaced characters.
s
Squash duplicate replaced characters.
PERL [Unit-VIII]
283
The /d modifier deletes the characters matching SEARCHLIST that do not have a corresponding entry in
REPLACEMENTLIST. For example:
#!/usr/bin/perl
$string = 'the cat sat on the mat.';
$string =~ tr/a-z/b/d;
print "$string\n";
This will produce following result
b bb.
The last modifier, /s, removes the duplicate sequences of characters that were replaced, so:
#!/usr/bin/perl
$string = 'food';
$string = 'food';
$string =~ tr/a-z/a-z/s;
print $string;
This will produce following result
Fod
Regular Expression Elements
Regular expressions are their own little language nestled inside of Perl.For all their power and expressivity,
patterns in Perl recognize the same 12 traditional metacharacters (the Dirty Dozen, as it were) found in many
other regular expression packages:
\|()[{^$*+?.
Some simple metacharacters stand by themselves, like .and ^ and $. They don't directly affect
anything around them. Some metacharacters work like prefix operators, governing what follows them, like \.
Others work like postfix operators, governing what immediately precedes them, like *, +, and ?. One
metacharacter, |, acts like an infix operator, standing between the operands it governs. There are even
bracketing metacharacters that work like circumfix operators, governing something contained inside them,
like (...) and [...]. Parentheses are particularly important, because they specify the bounds of | on the inside,
and of *, +, and ?on the outside.
Metasymbol Tables
In the following tables, the Atomic column says "yes" if the given metasymbol is quantifiable (if it
can match something with width, more or less). Also, we've used "..." to represent "something else". Please
see the later discussion to find out what "..." means, if it is not clear from the one-line gloss in the table.)
In this table shows the basic traditional metasymbols. The first four of these are the structural
metasymbols we mentioned earlier, while the last three are simple metacharacters. The .metacharacter is an
example of an atom because it matches something with width (the width of a character, in this case); ^ and $
Open Source Software
284
PERL [Unit-VIII]
are examples of assertions, because they match something of zero width, and because they are only
evaluated to see if they're true or not.
General Regex Metacharacters
Symbol Atomic
Meaning
\...
Varies De-meta next nonalphanumeric character, meta next alphanumeric character (maybe).
...|...
No
(...)
Yes
[...]
Yes
No
Yes
No
285
PERL [Unit-VIII]
For example, heres a regular expression match using the (?{code}) assertion:
use re 'eval';
$_ = '<A href="/index.shtml">';
m<
(?{ $cnt = 0 })
\<A.*"
(.(?{ local $cnt = $cnt + 1;}))*
"\>
(?{ $res = $cnt })
>x;
print $res," words\n";
It counts the number of letters between the double quotes in the HTML referencespecified in $_.
Assertion
Meaning
(?#text)
(?:pattern)
(?imsx:pattern)
(?=pattern)
(?!pattern)
286
PERL [Unit-VIII]
would be incredibly time consuming, because for each line in FILE, we have to
recompile each of the regular expressions in @expressions, even though the contents
of @expressions dont change between each line.
The qr// operator takes a regular expression and compiles it as normal, returning a
regular expression object such that
$regex = qr/[a-z]+/is;
s/$regex/nothing/;
is the same as
s/[a-z]+/nothing/is;
Because the returned object is a compiled regular expression, we can solve the earlier
problem by precompiling all the expressions before we enter the loop.
That means we could change the preceding example to
@regexes = map { qr/$_/ } @expressions;
while(<FILE>)
{
foreach $regex (@regexes)
{
print if /$regex/;
}
}
Now, because the patterns are precompiled, the regular expressions are executed
immediately within the main loop without requiring a new compilation.
The return value of the qr// operator can also be embedded into other expressions:
print if /start${regex}end/;
PERL [Unit-VIII]
287
pos EXPR
pos
Return Value
In list context, then positions of all the matches within the regular expression
Example
#!/usr/bin/perl -w
$name = "This is alpha beta gamma";
$name =~ m/alpha/g;
print("pos() ", pos($name), "\n");
It will produce following results:
pos() 13
quotemeta:
Escapes all meta-characters in EXPR. For example, quotemeta("AB*..C") returns "'AB\*\.\.C".
Syntax
quotemeta EXPR
Return Value
Example
#!/usr/bin/perl -w
printquotemeta("AB*\n[.]*");
It will produce following results:
AB\*\
\[\.\]\*
study:
Takes extra time to study EXPR in order to improve the performance on regular expressions
conducted on EXPR. If EXPR is omitted, uses $_. The actual speed gains may be very small, depending on
the number of times you expect to search the string. You can only study one expression or scalar at any one
time.
Syntax:
study EXPR
study
Open Source Software