Larry Wall skribis 2004-07-13 14:04 (-0700): > The combined form is definitely problematic in various ways, and we haven't > really redesigned open yet, since we haven't got to A29 yet. :-)
Well, open being much like IO::All::io would really make me happy. That is: my $fh = open 'foo.txt'; $fh.print 'hello!'; Should be possible, as well as: my $fh = open 'foo.txt'; print $fh.readline; Modifiers could be used to force something into a separate mode. rw and ro are used elsewhere in Perl 6 already, but obviously r without w would also be handy. my $fh = open 'foo.txt', :ro; $fh.print 'hello!'; # Not possible Of course, those who want the mode first can use my $fh = open :ro, 'foo.txt'; Which isn't too different from open my $fh, '<', 'foo.txt'; Or perhaps, for open an exception should be made, and r should be used instead of ro. > my $fh = open $filename :excl; > my $fh = append $filename :excl; > my $fh = creat $filename :excl; > ...er, I mean, > my $fh = create $filename :excl; That I would very much dislike. Let's not introduce keywords when the same thing can be done by modifiers, unless something is used EXTREMELY often (my mind has now accepted "say", but only because print "...\n" is extremely common). > Of course, append and create might just be shorthand for a normal open > with a :create or :append arguments, I suppose. I hope those keywords can be introduced with a pragma. "use shorthands;". That pragma then does need a single letter command line switch, because it would be very useful for one-liners. > @lines = <open $infile>; > print (append $outfile): @lines; > Or equivalently: > <open $infile> ==> > print (append $outfile): I'd prefer (open $outfile :append).print slurp $infile; Or, for the shorthand junkies :) (append $outfile).print slurp $infile; > But that might be a little too concise for good error messages. Assuming $infile ne $outfile and that the default error message (which perhaps is only there if enabled with a Fatal.pm-like pragma) includes the filename (it should, imo), two things being on one line shouldn't be a problem. > And they might lead people to write things like > while <open $file> {...} I've never seen while (IO::File->new($file)->readline) { ... } either. Of course, while-open is a good candidate for a warning. I don't think this "problem" needs a solution. I hope or-cache will still be an often used idiom in Perl 6: while <my $fh //= open $file> { ... } Although I'm not sure what exactly "my" would mean here. > But :rw is more or less orthogonal to < and > in UnixThink. There's one thing feel I must say about < and >: They're not different enough, visually. This works well on the command line and for delimiters, but open '<', $foo; open '>', $foo; is much harder to read than open 'r', $foo; open 'w', $foo; For the same reason, I prefer unified diff format to the </> format that my copy uses by default. > The fact that you want to both read and write a file says nothing > about whether you initially want to use, clobber, or append to an > existing file. It's okay to have defaults, I think. r use w clobber a append rw use This can without too much trouble be solved with :append and :clobber. Juerd