The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 19, 2019, 05:21:37 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Opening files - three-argument form  (Read 4120 times)
dirk
Global Moderator
Expert
*****
Offline Offline

Posts: 416


View Profile
« on: April 28, 2007, 02:29:07 PM »

Here are some examples of open statements using the three-argument form,
the module Carp for error handling and the module English.

Carp - warn of errors (from perspective of caller)
English - use nice English (or awk) names for ugly punctuation variables

When the modul English is loaded we can use the variable $OS_ERROR instead of $!.

Code:
use Carp;
use English;

my $filename = 'myfile.txt';

# read
open my $INPUT, '<', $filename or croak "Can't open '$filename': $OS_ERROR";
...
close $INPUT;

# write
open my $OUTPUT, '>', $filename or croak "Can't open '$filename': $OS_ERROR";
...
close $OUTPUT;

# append
open my $OUTPUT, '>>', $filename or croak "Can't open '$filename': $OS_ERROR";
...
close $OUTPUT;

Logged
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #1 on: April 28, 2007, 10:37:39 PM »

cool, will try.
Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
dirk
Global Moderator
Expert
*****
Offline Offline

Posts: 416


View Profile
« Reply #2 on: May 03, 2007, 01:58:54 PM »

For opendir the two argument form is still used.

Below is an example of opening a sub directory.

Code:
#!/usr/bin/perl

use strict;
use warnings;
use Carp;
use English;

my $dir    = 'stats/';
my $subdir = '2007/';

opendir my $DIR, $dir . $subdir
    or croak "Can't open directory: $OS_ERROR";

# do something

close $DIR;

exit;

The next version is also working:

Code:
#!/usr/bin/perl

use strict;
use warnings;
use Carp;
use English;

my $dir    = 'stats';
my $subdir = '2007';

opendir my $DIR, $dir . '/' . $subdir
    or croak "Can't open directory: $OS_ERROR";

# do something

close $DIR;

exit;

In the third example the variables $dir and $subdir are empty.
One would  assume that this triggers the error message.

But the script is running without any error message. It simply
opens the "/" directory (the root directory) which is available
on every server.

Code:
#!/usr/bin/perl

use strict;
use warnings;
use Carp;
use English;

my $dir    = '';
my $subdir = '';

opendir my $DIR, $dir . '/' . $subdir
    or croak "Can't open directory: $OS_ERROR";

# do something

close $DIR;

exit;

Ok, why should someone define empty variables? Normally that's not the case.
But if the variables are assigned dynamically in a complex script this can happen.

So it's better to use the first version with the trailing slash.
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: May 03, 2007, 04:03:23 PM »

Dirk that is by far the most readable and easy PERL I've ever seen.

I love the direction you're going because it's a great place to start - may I ask that MUCH LATER perhaps you do a little something on idiosyncratic PERL programming and the various ways that experts avoid typing...? After a stronger understanding of the language that would probably assist hugely as well.

Awesome stuff,
/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
dirk
Global Moderator
Expert
*****
Offline Offline

Posts: 416


View Profile
« Reply #4 on: May 03, 2007, 05:50:54 PM »

Ed, yes, later I will write something about the weird stuff: "write once, read never".

Specially I like complex hash and array structures Wink
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #5 on: May 03, 2007, 05:59:22 PM »

"write once, read never"

WORN code ROFLMAO I love it!
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!