web programming Unit VI PPT by Bhavsingh Maloth

Post on 04-Dec-2014

345 views 0 download

description

WEB PROGRAMMING PPT

Transcript of web programming Unit VI PPT by Bhavsingh Maloth

UNIT VI

Perl file system

Perl file system

• Perl File Handling: open, read, write and close files• Opening files:• Observation : here File Handles is associate with file, u can use the

filehandle to read from the file. If the file doesn't exist - or you cannot read it for any

other reason - then the script will die with the appropriate error message stored in the $! variable.

• if you wanted to modify the file• Then you'd have to specify the appropriate

mode using the three-argument form of open.

Notice, how both +< and +> open the file in read/write mode

Reading files

• If you want to read a text file line-by-line then you can do it as such:

Writing files

File Close

Eval

• Perl eval built-in function is very powerful• The general form of perl eval expects a

expression or a block of code as an argument.

Return ValueThis function returns value of last evaluated statement in EXPR or BLOCK

Advantages of Eval

• trapping errors using eval• create dynamic code using eval• insert a code from a file/sub-routine using

eval Perl eval can’t catch following errors:• Uncaught signal• Running out of memory• Syntax errors

Perl Eval Error Handling – Trapping Errors

• Eval is used to trap the errors. During the execution of the subroutine the program might die because of errors, or external calling of die function.

In the above, $count contains the value as 0. When we run the code without the eval block, the program gets exit.

During this time, if the block of perl code is executed inside the eval, then program continues to run even after the die or errors, and it also captures the errors or dieing words.

Perl 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

The Package Statement

• package statement switches the current naming context to a specified namespace (symbol table)

• If the named package does not exists, a new namespace is first created.

• The package stays in effect until either another package statement is invoked, or until the end of the end of the current block or file.

• You can explicitly refer to variables within a package using the :: package qualifier

BEGIN and END Blocks• BEGIN and END Blocks• You may define any number of code blocks

named BEGIN and END which act as constructors and destructors respectively.

• 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.

Perl Modules

• Modules : library functions

• A module is a .pm file that defines a library of related functions

• Modules are conceptually similar to old-fashioned Perl libraries (.pl files), but have a cleaner implementation

– Selective collection of namespace– simpler function invocation

How to use a Module

Module and main program

package Hello1;

sub greet { return "Hello, World!"; } 1;

Hello1.pm test1.pl

#!/usr/bin/perl

use Hello1;

print Hello1::greet();

Module structure

package Hello1;

sub greet { return "Hello, World!\n"; } 1;

Declare a package; file must be saved as Hello.pm

Contents of the package:functions, and variables.

Return a true value at end

Object Oriented in Perl

• Object Basics• There are three main terms, they are object,

class, and method.

Objects

• an object is only a reference to a data type that knows what class it belongs to.

• The object is stored as a reference in a scalar variable. Because a scalar only contains a reference to the object, the same scalar can hold different objects in different classes.

class• A class within Perl is a package that contains the

corresponding methods required to create and manipulate objects.

• Methods:• A method within Perl is a subroutine, defined with

the package. The first argument to the method is an object reference or a package name, depending on whether the method affects the current object or the class.

• Perl provides a bless() function which is used to return a reference and which becomes an object.

Defining a Class

• a class is corresponds to a Package.• To create a class in Perl, we first build a package. • A package is a self-contained unit of user-defined

variables and subroutines, which can be re-used over and over again.

• They provide a separate namespace within a Perl program that keeps subroutines and variables from conflicting with those in other packages.

• To declare a class named Person in Perl we do:

package Person;Note:

The scope of the package definition extends to the end of the file, or until another package

keyword is encountered.

Creating and Using Objects

• To create an object, we need object constructor.

• This constructor is a method defined within the package.

• Most programmers choose to name this object constructor method new, but in Perl one can use any name.

methods

• A method is a means by which an object's data is accessed or modified.

• In Perl, a method is just a subroutine defined within a particular package.

• So to define a method to print our Person object, we do:

• The subroutine print is now associated with the package Person.

• To call the method print on a Person object, we use the Perl "arrow" notation.

Object creation

• To create an object,we need an object constructor

• This constructor is a method defined within the package.

• What have we done? We created a subroutine called new associated with the package Person.

• The entries of the hash reference $self become the attributes of our object.

• We then use the bless function on the hash reference.

• The bless function takes two arguments: a reference to the variable to be marked and a string containing the name of the class.

• This indicates that the variable now belongs to the class Person.

Interfacing to the operating system

• The Perl interface allows you to create Perl scripts that can read the accounting files produced by the exacct framework.

• You can also create Perl scripts that write exacct files.

Creating internet ware applications

• Internet provides a global open infrastructure for exchanging and sharing of various resources for the people all over the world.

• The rapid development and the wide application of Internet make it become a new mainstream platform for software to be used, developed, deployed and executed. With the vision of “Internet as computer”,

• many application styles such as pervasive computing, grid computing, service computing and cloud computing occur on this open and dynam

• in order to adapt the software system to such a new environment, its structure model should be autonomous, cooperative, situational, evolvable, emergent, trustworthy,ic environment.

• Internetware system is able to identify the changes of open and dynamic environment such as Internet, respond to the changes in the way of architectural transformation, and exhibit context-aware, adaptive and trustworthy behaviors in the open and dynamic environment in order to meet its flexible design objectives.

• Internetware challenges many aspects of software technologies, from operating platforms, programming models, to engineering approaches