Perl Programming

CSE3395




Modules and Databases in perl


E-mail this post



Remember me (?)



All personal information that you provide here will be governed by the Privacy Policy of Blogger.com. More...



Up until now, you have only seen stand alone perl.

You might like to use a bit of someone else's functionality.

Much of perl's functionality is contained in modules. There's a whole stack of modules (take a look at CPAN), and you can make your own ones as well.

This means that you don't have to write everything from scratch if you just want to 'knock something up'.

When you want to import a module, you use the

use

statement.

When you write a module (not something we would do in this subject)

package Dog;
ensures that Dog's variables and function don't interfere with anyone else's methods.

Databases are a collection of (consistent!) data.

Persistent Data
We would like to have a mechanism of setting up a database in perl. The hash is a pretty good basis for doing this. The problem with hash is that they are not persistent outside the running of the program. What we would really like is a persistent hash, and that's how we begin with the database.

With a little bit of magic, you can 'tie' a database to a hash, and just access it as though it was a hash. It works in the background, by redefining what the compiler is doing, but you don't need to see any of that. You just need to know how to use hash.

use Data::Dumper

DBM files are common to UNIX,
almost all UNIX implementations have them.
Data is stored in a binary file.
Roughly equivalent to a hash.

When you want to open a database:
Use a dbmopen or tie function.

Perl provides a simple interface to DBM

To tie a hash to a file
dbmopen %hash, "file", 0600

[] learn about UNIX file permissions !

So that's all pretty cool. The problem is that you can't store much in them!
So we have to pack and unpack things.

Hashes work really well when the index is an integer.

You can even do low level stuff by sequentially ordering a binary file and to just use seek to find an arbitrary offset from the start of the file, and then read it in using unpack. It's low level, but very efficient.

Client / server databases.
You can interact with these with DBI interface. This allows you to access DBs on other machines!

DBI insulates you and your perl from the direct DB implementation!
And it's very different to DBM where the DB is on the machine where we are.
You've got a huge range of techniques available to you.

Reccomend that you use the DBM databases for assignment 2.


0 Responses to “Modules and Databases in perl”

Leave a Reply

      Convert to boldConvert to italicConvert to link

 


Previous posts

Archives

Links