Re: RFC 51 (v1) Angle brackets should accept filenames a

[email protected] (Jon Ericson) Mon, 07 Aug 2000 14:31:59 -0700
Newsgroups perl.perl6.language.io
Message-ID <[email protected]>
Monty Taylor wrote:
> open(FOO,'<foo.txt');
> @LIST=['FOO','.bashrc'];
> while (<@LIST>) {
>   process;
> }
> 
> and a directory containing the files FOO, foo.txt, and .bashrc
> 
> Does <@LIST> treat FOO as a filehandle or a filename? Or maybe this
> would be clearer...

I am assuming that bareword filehandles will go away (see discussion of
RFC 10 and RFC 14).  The files './FOO' and './.bashrc' will be
processed.  'foo.txt' will not as ref 'FOO' ne 'GLOB'.

> open(LOG,'>/tmp/output.log');
> opendir(FOO,'.');
> @dir=readdir FOO;
> while (<@dir> ) {
>   process;
> }
> 
> and a directory having files 'FOO','STDOUT','STDIN' and 'LOG'. Do we
> treat as filehandles or files?

Only the files in '.' will be processed.  Try out the code I posted in
the IMPLEMENTATION section - it works under perl 5.6.

> I agree, it's streching for an example, but I hope the question is
> clear?

In v2 of RFC 51, I intend to point out that filehandles can be
completely hidden from the user:

    my $file = '/path/to/file';
    print $file;	# prints filename
    print <$file>;	# prints contents of the file

I will also propose extending the syntax to include directories:

    my @homefiles = grep (-f, <'~'>);	# list of plain files
    print <@homefiles>;			# cat ~/*

Under this scheme, _all_ scalars are potential filehandles.  Think of
them as symlinks.  As soon as you apply the <> operator to a scalar,
perl should open it in a DWIM fashion and give you back the next line
(in scalar context) or a list of lines (in list context) from the file. 
The next time you use <> on that scalar, perl should give you the next
line or undef if there are no more lines to read.  Most scripts won't
need to explicitly call open() any more.

    my $file1 = '/path/to/file';
    <$file1>;		# read and discard one line 
    my $file2 = $file1;	# copy the filehandle to $file2
    print <$file2>;	# print all but the first line
    print <$file1>;	# print nothing (end of file)
    $file2 = scalar $file3; # copy filename
    print <$file2>;	# print entire file (new filehandle)

Jon
-- 
Knowledge is that which remains when what is
learned is forgotten. - Mr. King