restriction on file handle

Y

Yan Wu

HI,
In the below script, "info1" is never defined but it could still run
correctly.
Is there any way to restrict the use of undefined file handle?
Thanks.
Yan


#!/p/perl/bin/perl -w
#
# Program to open the password file, read it in,
# print it, and close it again.
use strict;
use FileHandle;

my $file;
my @lines;


$file = 'myfile'; # Name the file
open(info1, $file); # Open the file
@lines = <info1>; # Read it into an array
close(info1); # Close the file
print @lines; # Print the array
 
W

Walter Roberson

: In the below script, "info1" is never defined but it could still run
:correctly.
: Is there any way to restrict the use of undefined file handle?

I don't understand?

:eek:pen(info1, $file); # Open the file

You define it right there.

Perhaps you meant to write about:

open($info1, $file) or die "$file could not be opened";
@lines = <$info1>;
close($info1);

If so then your 'use strict' would complain.
 
D

David K. Wall

Purdue, eh? I bet spaf is a good teacher.
HI,
In the below script, "info1" is never defined but it could still run
correctly.

Sure it is. You define it when you open the file.
Is there any way to restrict the use of undefined file handle?

Check to see if the open is successful. (see below)
Thanks.
Yan


#!/p/perl/bin/perl -w

For recent versions of Perl you can use the warnings pragma for more
control (if you need it).

use warnings;

instead of -w.
#
# Program to open the password file, read it in,
# print it, and close it again.
use strict;

Strictures in place. Good.
use FileHandle;

What's this for? You don't use it anywhere.
my $file;
my @lines;


$file = 'myfile'; # Name the file

You can initialize this at the same time you declare it (if you want):

my $file = 'myfile';

open(info1, $file); # Open the file

You should *always* check to see if an open() succeeded. The convention is
also to use capital letters for filehandles; it's not technically necessary
but will make it easier for other people to read your code.

open INFO1, $file or die "Cannot open $file: $!";
@lines = <info1>; # Read it into an array

There's nothing wrong with slurping in "small" files, but if you suspect
the file could be "large" you might want to process it line-by-line. These
are relative terms; they depend on what you're doing and the resources
available.
close(info1); # Close the file
print @lines; # Print the array

Since all you're doing is printing the file, you don't really even need
that @lines array. For example,

print <INFO1>;

will print the contents of the file associated with the INFO1 filehandle.

You also don't have to comment every single line of code. Comments are for
explaining *why* some code exists, or to give a high-level explanation of
what's going on. That is, they're notes to yourself or future maintenance
programmers to make fixing or modifying the code easier.
 
B

Ben Morrow

Yan Wu said:
HI,
In the below script, "info1" is never defined but it could still run
correctly.

What do you mean by 'is never defined'? The filehandle info1 is
created by the open statement.
Is there any way to restrict the use of undefined file handle?

This will only allow lexical filehandles:

use strict;
use subs qw/open/; # to override the builtin

sub open (\$;$@) {
# it is necessary to give the first arg separately
# as CORE::eek:pen has a prototype of (*;$@)
my ($fh, $first, @rest) = @_;
CORE::eek:pen $$fh, $first, @rest;
}

.. This means that these

open my $info1, $file;

my $info1;
open $info1, $file;

will succeed whereas these

open info1, $file;
open *info1, $file;
open \*info1, $file;
open "info1", $file;

will fail at compile time.

Ben
 
B

Ben Morrow

Ben Morrow said:
This will only allow lexical filehandles:

OK, there's a case I missed:

my $fh = \*FH;
open $fh, "file";

and

my $fh = *FH;
open $fh, "file";

will succeed despite using a global FH. That's pretty obscure,
though...

Ben
 
Y

Yan Wu

Thanks a lot. This is what I want.
Yan

Ben Morrow wrote:
-
- use strict;
- use subs qw/open/; # to override the builti
 
T

Tassilo v. Parseval

Also sprach Ben Morrow:
OK, there's a case I missed:

my $fh = \*FH;
open $fh, "file";

and

my $fh = *FH;
open $fh, "file";

will succeed despite using a global FH. That's pretty obscure,
though...

Not really. A filehandle in Perl is always a GLOB.

open my $fh, ...;

is just syntactic sugar and perl will internally generate a GLOB and
store a reference to it in $fh.

Tassilo
 
A

Anno Siegel

Yan Wu said:
Thanks a lot. This is what I want.
Yan

Ben Morrow wrote:
-
- use strict;
- use subs qw/open/; # to override the builti

That's a pretty radical approach.

use warnings FATAL => 'closed';

in an appropriate scope is less intrusive.

Anno
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top