dirk
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 $!. 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; Bompa
cool, will try.
dirk
For opendir the two argument form is still used.
Below is an example of opening a sub directory. #!/usr/bin/ perluse 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: #!/usr/bin/ perluse 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. #!/usr/bin/ perluse 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. perkiset
Dirk that is by far the most readable and easy
PERLI'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 PERLprogrammingand the various ways that experts avoid typing...? After a stronger understanding of the language that would probably assist hugely as well.Awesome stuff, /p dirk
Ed, yes, later I will write something about the weird stuff: "write once, read never".
Specially I like complex hash and array structures perkiset
quote author=dirk link=topic=139.msg897#msg897 date=1178239854 "write once, read never" WORN code I love it! |
Thread Categories
Best of The Cache Home | ||
Search The Cache |
- Ajax
- Apache & mod_rewrite
- BlackHat SEO & Web Stuff
- C/++/#, Pascal etc.
- Database Stuff
- General & Non-Technical Discussion
- General programming, learning to code
- Javascript Discussions & Code
- Linux Related
- Mac, iPhone & OS-X Stuff
- Miscellaneous
- MS Windows Related
- PERL & Python Related
- PHP: Questions & Discussion
- PHP: Techniques, Classes & Examples
- Regular Expressions
- Uncategorized Threads