1 / 13

Advance Topics

Advance Topics. Topics. Packages Launching Programs Sockets Using Unix DBM. Packages. Every program has a main package. A program can use other packages use <name>; The programmer can also define packages. package <name>; The rest of the file is the body of the package.

enye
Télécharger la présentation

Advance Topics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advance Topics

  2. Topics • Packages • Launching Programs • Sockets • Using Unix DBM

  3. Packages • Every program has a main package. • A program can use other packages • use <name>; • The programmer can also define packages. • package <name>; • The rest of the file is the body of the package. • The name of the file is <name>.pm

  4. Packages (cont.) • Each package has its own namespace. • Reduces the risk of name conflicts. • Functions: <name>::<function name> • e.g. String::length • Global vars: <var symbol><name>::<var> • e.g. $String::foo • my can be used to prevent access: Package private; my $private_var;

  5. Launching Programs • Almost all programming languages allow their programs to call subprograms. • Perl is no exception. • Perl has several ways to launch a program. • Two most common are: • backquote (`) • system • Perl also includes fork and exec.

  6. backquote • Place the name of the subprogram in (`). • Synchronous execution. • Return value is the output of the subprogram. • For example, $date = `date`; print “$date \n”;

  7. system • Pass subprogram’s name to system. • Synchronous execution. • Return value is the execution status of the subprogram. • Subprograms output is directed to STDOUT. • For example: system(“date”);

  8. Socket Communication • Provides a powerful way to communicate between computers, programs, etc. • Based on the notion of clients and servers. • Usage is similar to files: • Programs read from and write to sockets. • Supported by Perl’s Socket module. • use Socket;

  9. A Client/Server Example

  10. Unix DBM • Unix has a simple DataBase Management (DBM) system. • A DBM database is a list of key/value pairs. • Structure similar to Perl’s hash. • DBM databases are connected to hash. • DBM provides persistent hashes.

  11. DBM (cont.) • A DBM database is stored in two files: • <name>.pag: Stores the data. • <name>.dir: Index into the .pag file. • dbmopen is used to create and open DBM databases. • dbmclose breaks connection between hash and database.

  12. dbmopen/dbmclose • dbmopen (<hash>, <dbname>, <mode>) • e.g. dbmopen (%salaries, salary_db, 0666); • e.g. dbmopen (%salaries, salary_db, undef); • dbmclose (<hash>) • e.g. dbmclose (%salaries);

  13. An Example dbmopen (%salaries, salary_db, 0666) || die “Could not open salary_db $!”; print “Enter an employee’s name: ”; while ($name = <STDIN>) { chomp $name; print “Enter the salary: ”; $salaries{$name} = <STDIN>; print “Enter an employee’s name: ”; } dbmclose (%salaries);

More Related