What is GDBM_File and Fcntl ? Please comment this piece of code

A

Ahmad

Hi ,

I'm newbie in Perl, and I have some Perl scripts that contains some
text processing.

In the piece of Perl code shown below:
What do the following lines mean? What are the "Fcntl" and
"GDBM_File" ? Finally, what is the usage of "tie" and "untie"
functions, and what type of arguments are passed to them below?!

use Fcntl;
use GDBM_File;
tie %datain001,GDBM_File,"IRD_db/mainarea_db", O_RDWR | O_CREAT, 0644;
.....
.....
untie %datain001;


Thanks and regards
Ahmad
 
R

RedGrittyBrick

Ahmad said:
Hi ,

I'm newbie in Perl, and I have some Perl scripts that contains some
text processing.

In the piece of Perl code shown below:
What do the following lines mean? What are the "Fcntl" and
"GDBM_File" ?

They are Perl modules. A bit like subroutine/class/method libraries in
other programming languages.

You can read about them by opening a command prompt and typing#
perldoc Fcntl
perldoc GDBM_File
Finally, what is the usage of "tie" and "untie"
functions, and what type of arguments are passed to them below?!

See
perldoc -f tie
perldoc -f untie
perldoc perltie
use Fcntl;
use GDBM_File;
tie %datain001,GDBM_File,"IRD_db/mainarea_db", O_RDWR | O_CREAT, 0644;
....
....
untie %datain001;

You might want to read an introductory tutorial first ...
http://www.wdvl.com/Authoring/Languages/Perl/PerlfortheWeb/tie_a_hash.html
 
X

xhoster

Ahmad said:
Hi ,

I'm newbie in Perl, and I have some Perl scripts that contains some
text processing.

As a newbie, one of the first things you should do is learn how to
use the docs.

In the piece of Perl code shown below:
What do the following lines mean? What are the "Fcntl" and
"GDBM_File" ?

On a linux command line, I type "perldoc Fcntl" and get:

NAME
Fcntl - load the C Fcntl.h defines
.....

And for "perldoc GDBM_File":

NAME
GDBM_File - Perl5 access to the gdbm library.
.....

Finally, what is the usage of "tie" and "untie"
functions, and what type of arguments are passed to them below?!

perldoc -f tie
perldoc -f untie
use Fcntl;
use GDBM_File;
tie %datain001,GDBM_File,"IRD_db/mainarea_db", O_RDWR | O_CREAT, 0644;

The overall effect is that stuff put into the hash %datain001 is now stored
on disk in the file IRD_db/mainarea_db, rather than in RAM.

The purpose of Fcntl is to define O_RDWR and O_CREAT

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
B

Ben Morrow

Quoth Ahmad said:
I'm newbie in Perl, and I have some Perl scripts that contains some
text processing.

In the piece of Perl code shown below:
What do the following lines mean? What are the "Fcntl" and
"GDBM_File" ? Finally, what is the usage of "tie" and "untie"
functions, and what type of arguments are passed to them below?!

use Fcntl;
use GDBM_File;

Fcntl and GDBM_File are modules; that is, pieces of Perl code (and
sometimes C code) that have already been written for you. 'use' is the
Perl keyword for pulling a module into your program: if you are familiar
with C, it's like a combination of '#include <foo.h>' and '-lfoo' on the
link line. Most other languages have some equivalent facility.

From your point of view, as a user of modules, all you need to know to
start with is that 'use' pulls a module in, and you can pass the module
arguments. To find out what the module does, and what arguments it
takes, you need to read 'perldoc Module'; that is, in this case, you
would type

perldoc Fcntl

at your command line and read the text it gives you. (Of course, if you
tie %datain001,GDBM_File,"IRD_db/mainarea_db", O_RDWR | O_CREAT, 0644;

'tie' attaches some 'magic' to a variable, meaning that when you read
and write its value something unusual happens. In this case, you're
using the magic provided by the GDBM_File module, which makes the
entries in the named database appear as elements in the hash.
untie %datain001;

'untie' undoes the 'tie' above; an explicit 'untie' is usually a
mistake.

Ben
 
B

Ben Morrow

Quoth RedGrittyBrick said:
You can read about them by opening a command prompt and typing#
perldoc Fcntl
perldoc GDBM_File

Except that those (particular) docs are *entirely useless* unless you
already know what the modules in question do. GDBM_File is
particularly bad: it doesn't even specify the arguments to 'tie' (and
I think the OP's original code was wrong, though maybe in a way that
happened to work).

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top