web programming Unit VI PPT by Bhavsingh Maloth

41
UNIT VI Perl file system

description

WEB PROGRAMMING PPT

Transcript of web programming Unit VI PPT by Bhavsingh Maloth

Page 1: web programming Unit VI PPT  by Bhavsingh Maloth

UNIT VI

Perl file system

Page 2: web programming Unit VI PPT  by Bhavsingh Maloth

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.

Page 3: web programming Unit VI PPT  by Bhavsingh Maloth

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

mode using the three-argument form of open.

Page 4: web programming Unit VI PPT  by Bhavsingh Maloth

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

Page 5: web programming Unit VI PPT  by Bhavsingh Maloth

Reading files

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

Page 6: web programming Unit VI PPT  by Bhavsingh Maloth

Writing files

Page 7: web programming Unit VI PPT  by Bhavsingh Maloth

File Close

Page 8: web programming Unit VI PPT  by Bhavsingh Maloth

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

Page 9: web programming Unit VI PPT  by Bhavsingh Maloth
Page 10: web programming Unit VI PPT  by Bhavsingh Maloth

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

Page 11: web programming Unit VI PPT  by Bhavsingh Maloth

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.

Page 12: web programming Unit VI PPT  by Bhavsingh Maloth

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.

Page 13: web programming Unit VI PPT  by Bhavsingh Maloth
Page 14: web programming Unit VI PPT  by Bhavsingh Maloth

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

Page 15: web programming Unit VI PPT  by Bhavsingh Maloth

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.

Page 16: web programming Unit VI PPT  by Bhavsingh Maloth

• 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

Page 17: web programming Unit VI PPT  by Bhavsingh Maloth

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.

Page 18: web programming Unit VI PPT  by Bhavsingh Maloth

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

Page 19: web programming Unit VI PPT  by Bhavsingh Maloth

How to use a Module

Page 20: web programming Unit VI PPT  by Bhavsingh Maloth

Module and main program

package Hello1;

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

Hello1.pm test1.pl

#!/usr/bin/perl

use Hello1;

print Hello1::greet();

Page 21: web programming Unit VI PPT  by Bhavsingh Maloth

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

Page 22: web programming Unit VI PPT  by Bhavsingh Maloth
Page 23: web programming Unit VI PPT  by Bhavsingh Maloth
Page 24: web programming Unit VI PPT  by Bhavsingh Maloth

Object Oriented in Perl

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

class, and method.

Page 25: web programming Unit VI PPT  by Bhavsingh Maloth

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.

Page 26: web programming Unit VI PPT  by Bhavsingh Maloth

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.

Page 27: web programming Unit VI PPT  by Bhavsingh Maloth

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.

Page 28: web programming Unit VI PPT  by Bhavsingh Maloth

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

Page 29: web programming Unit VI PPT  by Bhavsingh Maloth

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.

Page 30: web programming Unit VI PPT  by Bhavsingh Maloth

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:

Page 31: web programming Unit VI PPT  by Bhavsingh Maloth

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

Page 32: web programming Unit VI PPT  by Bhavsingh Maloth

Object creation

• To create an object,we need an object constructor

• This constructor is a method defined within the package.

Page 33: web programming Unit VI PPT  by Bhavsingh Maloth

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

Page 34: web programming Unit VI PPT  by Bhavsingh Maloth

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.

Page 35: web programming Unit VI PPT  by Bhavsingh Maloth

Creating internet ware applications

Page 36: web programming Unit VI PPT  by Bhavsingh Maloth

• 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”,

Page 37: web programming Unit VI PPT  by Bhavsingh Maloth

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

Page 38: web programming Unit VI PPT  by Bhavsingh Maloth

• 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

Page 39: web programming Unit VI PPT  by Bhavsingh Maloth
Page 40: web programming Unit VI PPT  by Bhavsingh Maloth
Page 41: web programming Unit VI PPT  by Bhavsingh Maloth