UNIT VI
Perl file system
Perl file system
• Perl File Handling: open, read, write and close files
• Opening files:
• Observation :
 here File Hand...
• 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 cod...
Advantages of Eval
• trapping errors using eval
• create dynamic code using eval
• insert a code from a file/sub-routine u...
Perl Eval Error Handling – Trapping
Errors
• Eval is used to trap the errors. During the
execution of the subroutine the p...
During this time, if the block of perl code is executed inside the eval, then program
continues to run even after the die ...
Perl Packages
• A package is a collection of code which lives in
its own namespace
• A namespace is a named collection of ...
The Package Statement
• package statement switches the current
naming context to a specified namespace
(symbol table)
• If...
• The package stays in effect until either
another package statement is invoked, or until
the end of the end of the curren...
BEGIN and END Blocks
• BEGIN and END Blocks
• You may define any number of code blocks
named BEGIN and END which act as co...
Perl Modules
• Modules : library functions

• A module is a .pm file that defines a library of
related functions
• Modules...
How to use a Module
Module and main program
Hello1.pm
package Hello1;
sub greet {
return "Hello, World!";
}
1;

test1.pl
#!/usr/bin/perl
use H...
Module structure
package Hello1;
sub greet {
return "Hello, World!n";
}
1;

Declare a package; file must
be saved as Hello...
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 re...
class

• A class within Perl is a package that contains the
corresponding methods required to create and
manipulate object...
Defining a Class
• a class is corresponds to a Package.
• To create a class in Perl, we first build a package.
• A package...
• To declare a class named Person in Perl we
do:
package Person;
Note:
The scope of the package definition extends to
the ...
Creating and Using Objects
• To create an object, we need object
constructor.
• This constructor is a method defined withi...
methods
• A method is a means by which an object's
data is accessed or modified.
• In Perl, a method is just a subroutine ...
• The subroutine print is now associated with
the package Person.
• To call the method print on a Person object,
we use th...
Object creation
• To create an object,we need an object
constructor
• This constructor is a method defined within
the pack...
• What have we done? We created a subroutine
called new associated with the package Person.
• The entries of the hash refe...
Interfacing to the operating system
• The Perl interface allows you to create Perl
scripts that can read the accounting fi...
Creating internet ware applications
• Internet provides a global open infrastructure
for exchanging and sharing of various
resources for the people all over t...
• many application styles such as pervasive
computing, grid computing, service computing
and cloud computing occur on this...
• Internetware system is able to identify the changes
of open and dynamic environment such as Internet,
respond to the cha...
web programming Unit VI PPT  by Bhavsingh Maloth
web programming Unit VI PPT  by Bhavsingh Maloth
web programming Unit VI PPT  by Bhavsingh Maloth
web programming Unit VI PPT  by Bhavsingh Maloth
web programming Unit VI PPT  by Bhavsingh Maloth
web programming Unit VI PPT  by Bhavsingh Maloth
web programming Unit VI PPT  by Bhavsingh Maloth
Upcoming SlideShare
Loading in …5
×

web programming Unit VI PPT by Bhavsingh Maloth

723 views

Published on

WEB PROGRAMMING PPT

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

web programming Unit VI PPT by Bhavsingh Maloth

  1. 1. UNIT VI Perl file system
  2. 2. 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.
  3. 3. • if you wanted to modify the file • Then you'd have to specify the appropriate mode using the three-argument form of open.
  4. 4. Notice, how both +< and +> open the file in read/write mode
  5. 5. Reading files • If you want to read a text file line-by-line then you can do it as such:
  6. 6. Writing files
  7. 7. File Close
  8. 8. 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 Value This function returns value of last evaluated statement in EXPR or BLOCK
  9. 9. 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
  10. 10. 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.
  11. 11. 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.
  12. 12. 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
  13. 13. 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.
  14. 14. • 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
  15. 15. 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.
  16. 16. Perl Modules • Modules : library functions • A module is a .pm file that defines a library of related functions • Modules are conceptually similar to oldfashioned Perl libraries (.pl files), but have a cleaner implementation – Selective collection of namespace – simpler function invocation
  17. 17. How to use a Module
  18. 18. Module and main program Hello1.pm package Hello1; sub greet { return "Hello, World!"; } 1; test1.pl #!/usr/bin/perl use Hello1; print Hello1::greet();
  19. 19. 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
  20. 20. Object Oriented in Perl • Object Basics • There are three main terms, they are object, class, and method.
  21. 21. 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.
  22. 22. 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.
  23. 23. 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.
  24. 24. • 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.
  25. 25. 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.
  26. 26. 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:
  27. 27. • 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.
  28. 28. Object creation • To create an object,we need an object constructor • This constructor is a method defined within the package.
  29. 29. • 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.
  30. 30. 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.
  31. 31. Creating internet ware applications
  32. 32. • 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”,
  33. 33. • 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.
  34. 34. • 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

×