[Ilugc] [TIP] perl tutorial VI file I/O

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Thu, 8 Dec 2011 06:38:48 +0530

We saw the power of regex and also that regexes form the core
strength of perl. How easily it melds with the perl idiom and the
 perl way of doing things makes regex very commonly and
 powerfully used in perl.

In fact most of the time perl is used because of the flexibility of regexes.

It slows down things a bit but perl was never built for performance.
It will never
 be faster than C since all languages are written in C and it is an
interpreted language.

At least it would be faster than Java. ;)

Anyway I was also talking OO and God knows how I could forget python.

Python is a language built with OO in mind ground up. And it is very
pleasant to do OO in
 python and it is a clear OO language.

In my opinion perl OO is more natural and smooth as opposed to Python
because you
 can do OO in perl without even realizing the complexity or OO concepts.

That I will argue is a power of perl than its weakness. It is not
perfect and it is ugly but
 you can get your job done without any hassle. Hence the argument in
LUG few days ago
 that perl OO does not have private variables. In fact javascript is a
prototype language.

It means that there is no much difference between class and object.
Perl also has that
 idiom though it is not strictly so. One needs to instantiate a class
as an object to use it
 in most cases.

Also python has many functional programming concepts. The for()
iterator we saw in perl
 is a crude example. In perl you have very few. Python and LISP are full of it.

Anyway let us get to work now.

File I/O involves reading and writing files.

All languages have OS bindings to do this. Javascript does not have;
it runs inside a browser and
 a browser cannot have security flaws.

Anyway file I/O in perl can be done in many ways.

In the simplest case:

open FH, "/etc/passwd";
while(<FH>) {
      print;
}
close FH;

will do a file input.

A simple file output would be:

open FH, ">/tmp/test";
print FH "Writing a test file\n";
close FH;

Test these two programs.

It is easy.

The <> (diamond) operator takes a file descriptor/handle as input.

It is called a typeglob in perl I think(I don't like academic stuff).
It is something,
 not a scalar, not an array, not a reference.

Anyway <FH> reads from the file handle typeglob. <> reads from standards input
 file descriptor.

The diamond operator is as much a hit amongst the perl geeks as the
regex // operator.

It significantly simplifies file input.

As to file output, print is the most useful command to write to a file
descriptor.

print "What is this?";

prints to stdout.

print FH "what is this";

prints to the file handle.

There are other commands like sysopen(), sysread() etc. but I never use them.

$ perldoc -f sysopen

The perl's standard documentation installed in the perl distribution
is not always
very readable. Some pages are very badly written.

Ironically the perl modules found in CPAN have very nice man pages and
plenty of code samples.

In fact you can simply use the mouse to copy paste code and you can
begin perl hacking.

-Girish

-- 
G3 Tech
Networking appliance company
web: http://g3tech.in ?mail: girish at g3tech.in

Other related posts:

  • » [Ilugc] [TIP] perl tutorial VI file I/O - Girish Venkatachalam