> I have a wish for Perl 6. I would like if the open-funktion > opens only a file if it doesn't exist. > Of course, I can first test if the file exist.
> if (-e $filename) > { print "file already exists!"; } > else > { open (FH, ">$filename") }
> My suggestion is to have a character for the open-mode-character that > indicate that the file should not already exist.
Like O_EXCL, yeah, that'd be nice.
But in Perl 6, you don't have to specify things like that through the mode string: you can specify them through named parameters:
On Tue, 13 Jul 2004 perl6-language-return-17800-blazar=pcteor1.mi.infn...@perl.org wrote:
> I have a wish for Perl 6. I would like if the open-funktion > opens only a file if it doesn't exist. > Of course, I can first test if the file exist.
I rather have a much "bigger" wish for an open-like operator that to be fair I would like to see *also* in Perl5: nothing that one can do in well more than one way in any case (also including creating a module that will do exactly what I write below...), but that indeed would come handy occasionally.
I'm thinking of an operator that returns a "magical" FH working like the ARGV one (with authomatic error handling etc. - obviously under Perl6 the beahviour could be tunable by means of modifiers, etc.), e.g.
openmany my $fh, [list]; while (<$fh>) { # ... }
Michele --
+++ wrote: > Idiot.
top-poster - Robin Chapman in sci.math, "Re: This Week's Finds in Mathematical Physics (Week 204)"
On Tue, Jul 13, 2004 at 03:41:54PM +0200, Michele Dondi wrote: > On Tue, 13 Jul 2004 perl6-language-return-17800-blazar=pcteor1.mi.infn...@perl.org wrote:
> > I have a wish for Perl 6. I would like if the open-funktion > > opens only a file if it doesn't exist. > > Of course, I can first test if the file exist.
> I rather have a much "bigger" wish for an open-like operator that to be > fair I would like to see *also* in Perl5: nothing that one can do in well > more than one way in any case (also including creating a module that will > do exactly what I write below...), but that indeed would come handy > occasionally.
> I'm thinking of an operator that returns a "magical" FH working like the > ARGV one (with authomatic error handling etc. - obviously under Perl6 > the beahviour could be tunable by means of modifiers, etc.), e.g.
> openmany my $fh, [list]; > while (<$fh>) { > # ... > }
From your description, it sounds like this is what you want:
{ local @ARGV = qw(foo bar baz and some other files); while (<>) { # ... ARGV and friends are at your disposal }
}
I suppose the perl6 equivalent would be:
{ temp $*ARGS = <<foo bar baz and some other files>>; while (<$ARGS>) { # ... }
On Tue, 13 Jul 2004, Jonathan Scott Duff wrote: > > I rather have a much "bigger" wish for an open-like operator that to be > > fair I would like to see *also* in Perl5: nothing that one can do in well > > more than one way in any case (also including creating a module that will
^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^
> > do exactly what I write below...), but that indeed would come handy > > occasionally.
^^^^^^^^^^^^ ^^^^^^^^^^^^
> From your description, it sounds like this is what you want:
> { > local @ARGV = qw(foo bar baz and some other files); > while (<>) { > # ... ARGV and friends are at your disposal > } > }
Yes, in fact this is One Way To Do It(TM), and one that indeed I have used, when I felt I needed it.
However, there are *admittedly* rare cases in which it plainly won't do:
while (<$fh>) { # $fh just as magic as ARGV my $l=<>; # ... }
And once one has such a function, then the default *ARGV beahviour may be based upon it. Also, as I already said, in Perl6 users could be allowed further degrees of customizability.
Michele --
>> try sleeping on it, that usually works. > I think you're right. Usually it works every time. ;-)
I don't know about that. I tried sleeping on a big big problem and we're now divorsed. - "Tralfaz" on sci.math, "Re: About a big big problem" (edited)
On Tue, Jul 13, 2004 at 07:24:55AM -0600, Luke Palmer wrote:
: But in Perl 6, you don't have to specify things like that through the : mode string: you can specify them through named parameters: : : my $fh = open ">$filename" :excl;
While that probably works, I think better style would be to use a comma:
my $fh = open ">$filename", :excl;
That explicitly passes :excl to open as a term in a list rather than relying on the magical properties of :foo to find the preceding operator adverbially when used where an operator is expected. We may have to tighten up the definition of operator :foo a bit, since it's not always going to be clear which preceding operator is intended. Even quotes are operators if you squint. Arguably, someone could look at
my $fh = open ">$filename" :excl;
and legitimately wonder which predicate the :excl attaches to:
my $fh = open (circumfix:""('>$filename', :excl)); # apply to quote my $fh = open(">$filename", :excl); # apply to open infix:=(my $fh, open(">$filename"), :excl); # apply to assignment
I see several directions we could go to clarify this, and I don't entirely like any of them. One could look at the set of available signatures (at either compile time or run time) and see who wants that particular adverb. But the early binding solutions tend to bind too soon, and the late binding solutions tend to bind too late. (The latter is much closer to how humans treat adverbs, though, since humans are essentially MD creatures with an overlay of various pre-compiled heuristics.)
Operator adverbs are, in essence, postfix operators with weird precedence. We're going to have to be very explicit about what we mean by "weird" here. Unfortunately, this morning I don't feel very explicit.
> While that probably works, I think better style would be to use a > comma:
> my $fh = open ">$filename", :excl;
> That explicitly passes :excl to open as a term in a list rather > than relying on the magical properties of :foo to find the preceding > operator adverbially when used where an operator is expected.
Hmm. If I must use the comma, I think I'd prefer a constant in this scenario:
my $fh = open ">$filename", open::exclusive;
Is it reasonable to use package:: as a prefix to a block to change the default namespace:
{ package open; exclusive + rw; }
becomes
open::{exclusive + rw}
which then lets me say:
my $fh = open ">$filename", open::{exclusive + rw};
On Tue, Jul 13, 2004 at 10:41:32AM -0700, Austin Hastings wrote:
: --- Larry Wall <la...@wall.org> wrote: : > While that probably works, I think better style would be to use a : > comma: : > : > my $fh = open ">$filename", :excl; : > : > That explicitly passes :excl to open as a term in a list rather : > than relying on the magical properties of :foo to find the preceding : > operator adverbially when used where an operator is expected. : : Hmm. If I must use the comma, I think I'd prefer a constant in this : scenario: : : my $fh = open ">$filename", open::exclusive;
The problem with constants is that they're constant. Sometimes you don't want them to be:
my $fh = open ">$filename", :encoding($myencoding);
: Is it reasonable to use package:: as a prefix to a block to change the : default namespace: : : { package open; exclusive + rw; } : : becomes : : open::{exclusive + rw}
Except that syntax already means something else (lookup of a symbol in the "open::" stash).
: which then lets me say: : : my $fh = open ">$filename", open::{exclusive + rw};
If you want to call a function that will artificially multiplex several arguments into a single argument, go ahead and call a function. Functions are allowed to be package qualified, and they're allowed to transmogrify random strings into package identifiers in whatever package they like. I don't think parameter names themselves need to be package qualified though. And I do think that something like :excl is a boolean argument, not an object. Explicitly making it into an object (even so simple an object as a bit) seems to me like a misfeature borrowed from C.
I can see how you're trying to use the identifier system to force compile time checking of symbol names here, but that approach is going to be of limited use in a language with MD. Most parameter names will end up being checked at runtime, and will end up throwing an exception if no function with an appropriate signature can be found. As I say, you can always force checking with a function or macro of your own choosing:
my $fh = open ">", $filename, *open::mode(:exclusive :rw);
> But in Perl 6, you don't have to specify things like that through the > mode string: you can specify them through named parameters: > my $fh = open ">$filename" :excl;
I was hoping we could finally get rid of mode characters, and especially combined mode and filename in one argument.
Ever since I read about the new :pairs, I thought that would imply :rw instead of >.
On Tue, Jul 13, 2004 at 09:25:52PM +0200, Juerd wrote:
: Luke Palmer skribis 2004-07-13 7:24 (-0600): : > But in Perl 6, you don't have to specify things like that through the : > mode string: you can specify them through named parameters: : > my $fh = open ">$filename" :excl; : : I was hoping we could finally get rid of mode characters, and especially : combined mode and filename in one argument.
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. :-)
We could mandate the two argument form. Or we could decide that "open" is one of those overly general concepts, and separate things by name:
my $fh = open $filename :excl; my $fh = append $filename :excl; my $fh = creat $filename :excl;
...er, I mean,
my $fh = create $filename :excl;
Of course, append and create might just be shorthand for a normal open with a :create or :append arguments, I suppose. Similar considerations apply for piped commands, though what the appropriate words might be, I don't know offhand.
Another interesting question is how well those things read in code like this:
But that might be a little too concise for good error messages. And they might lead people to write things like
while <open $file> {...}
which wouldn't necessarily work. At first blush, I'd expect that to reopen $file every time through the loop, which is probably not what the user expects. Possibly <...> in scalar context is smart enough to cache its iterator, but then you start getting into the "return false once" syndrome that confuses people. The inside of <...> isn't really a closure. So maybe <...> is smart about closures:
while <{open $file}> {...}
and only calls its closure after its previous iterator is exhausted. On the other hand, iterators tend to *look* like closures anyway, so may that'll be hard to distinguish in the absence of a declared return type.
But I digress...
: Ever since I read about the new :pairs, I thought that would imply :rw : instead of >.
But :rw is more or less orthogonal to < and > in UnixThink. 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.
> 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.
> 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.
On Tue, 13 Jul 2004, Larry Wall wrote: > On Tue, Jul 13, 2004 at 07:24:55AM -0600, Luke Palmer wrote: > : But in Perl 6, you don't have to specify things like that through the > : mode string: you can specify them through named parameters: > : > : my $fh = open ">$filename" :excl;
> While that probably works, I think better style would be to use a comma:
> my $fh = open ">$filename", :excl;
> That explicitly passes :excl to open as a term in a list rather > than relying on the magical properties of :foo to find the preceding > operator adverbially when used where an operator is expected. We may > have to tighten up the definition of operator :foo a bit, since it's > not always going to be clear which preceding operator is intended. > Even quotes are operators if you squint. [snip] > I see several directions we could go to clarify this, and I don't > entirely like any of them. [snip]
> Operator adverbs are, in essence, postfix operators with weird > precedence. We're going to have to be very explicit about what we > mean by "weird" here. Unfortunately, this morning I don't feel > very explicit.
My understanding is that the :adverb is at the end because it is passed as a named argument, which must be after the positional arguments. And that is certainly unambiguous with the comma.
However we have a precedent in m:i// where the :adverb comes directly after the operator. It's probably special because it "is parsed" that way, but it's so common that I feel I can use it as a precedent anyway.
So if :adverbs are having such a weird time finding their verbs, wouldn't it be easier to allow them to be placed directly after the verb all the time? Then the magic is merely moving it into the named args section instead of wondering which operator's named args section it belongs to.
Then
my $fh = open ">$filename" :excl;
is unambiguously
my $fh = open (circumfix:""('>$filename', :excl));
while what was actually wanted is written as either of
my $fh = open ">$filename", :excl; # part of the arg list my $fh = open :excl ">$filename"; # adverb to open
And besides, I think it reads better when the adverb is next to the verb.
On Tue, Jul 13, 2004 at 03:44:11PM -0600, John Williams wrote:
: On Tue, 13 Jul 2004, Larry Wall wrote: : > On Tue, Jul 13, 2004 at 07:24:55AM -0600, Luke Palmer wrote: : > : But in Perl 6, you don't have to specify things like that through the : > : mode string: you can specify them through named parameters: : > : : > : my $fh = open ">$filename" :excl; : > : > While that probably works, I think better style would be to use a comma: : > : > my $fh = open ">$filename", :excl; : > : > That explicitly passes :excl to open as a term in a list rather : > than relying on the magical properties of :foo to find the preceding : > operator adverbially when used where an operator is expected. We may : > have to tighten up the definition of operator :foo a bit, since it's : > not always going to be clear which preceding operator is intended. : > Even quotes are operators if you squint. : [snip] : > I see several directions we could go to clarify this, and I don't : > entirely like any of them. [snip] : > : > Operator adverbs are, in essence, postfix operators with weird : > precedence. We're going to have to be very explicit about what we : > mean by "weird" here. Unfortunately, this morning I don't feel : > very explicit. : : My understanding is that the :adverb is at the end because it is passed as : a named argument, which must be after the positional arguments. And that : is certainly unambiguous with the comma.
It's unambiguous becase a term is expected at that point, so :adverb isn't really an adverb--it's just a pair constructor.
: However we have a precedent in m:i// where the :adverb comes directly : after the operator. It's probably special because it "is parsed" that : way, but it's so common that I feel I can use it as a precedent anyway. : : So if :adverbs are having such a weird time finding their verbs, wouldn't : it be easier to allow them to be placed directly after the verb all the : time? Then the magic is merely moving it into the named args section : instead of wondering which operator's named args section it belongs to. : : Then : : my $fh = open ">$filename" :excl; : : is unambiguously : : my $fh = open (circumfix:""('>$filename', :excl)); : : while what was actually wanted is written as either of : : my $fh = open ">$filename", :excl; # part of the arg list : my $fh = open :excl ">$filename"; # adverb to open : : And besides, I think it reads better when the adverb is next to the verb. : : yes, no, maybe?
Except that you've but :excl where the parse is expecting a term, so it isn't an adverb. It's just a pair constructor. Which means that after it the parser is expecting an operator, and blows up on the string literal.
The adverb notation as currently defined only works where an operator is expected. Which means it can't really be used on unary operators unless there's some kind of pecking order where we try to apply it to the unary operator, and if that fails, apply it to the preceding binary operator. But that's a headache waiting to happen. I'm more inclined to say that the adverb notation only works for binary operators, and you have to use pair args with unaries. So you can't write
$x = rand 20 :bell_curve
because it would try to apply :bell_curve to the assignment, but you can write
$x = rand(20, :bell_curve)
I know it seems like the adverb syntax is misplacing the modifier with respect to the binary operator it's modifying, but I think most of its uses are actually very short:
1..100:by(3)
If you really want something fancy, you'd be better off to write it in functional form with a normal named pair argument:
infix:..(1, <some_big_hairy_expression>, :by(3))
instead of
1 .. <some_big_hairy_expression> :by(3)
Alternately, I do see an easy formatting hack:
1 .. (<some_big_hairy_expression>) :by(3)
Just don't anyone suggest that we indicate what's modified *syntactically* by placing the adverb directly under it...
Hmm. Maybe with an extra uparrow?
1 .. (<some_big_hairy_expression>) ^:by(3)
Yow. Two-dimensional programs. Reminds me of BF...
But we'd have to pay really close attention to how indenting is done. Maybe we should just pass this suggestion on to Guido... :-)
On Tue, 13 Jul 2004, Juerd wrote: > open '<', $foo; > open '>', $foo;
> is much harder to read than
> open 'r', $foo; > open 'w', $foo;
Are you sure?!? I would tend to disagree... not that MHO is particularly important, I guess, but just to stress the fact that it is by large a subjective matter...
Michele -- Oh, for God's sake! Earnest misunderstanding I can cope with, stupidity I can cope with, complete lack of sense of humour I can cope with, but lying I cannot. *PLONK* - Ben Morrow in clpmisc (slightly edited)
On Thursday 15 July 2004 19:42, Michele Dondi wrote:
> > open '<', $foo; > > open '>', $foo;
> > is much harder to read than
> > open 'r', $foo; > > open 'w', $foo; > Are you sure?!? I would tend to disagree... not that MHO is particularly > important, I guess, but just to stress the fact that it is by large a > subjective matter...
I have always felt that keeping it the same as shell scripting was a handy thing, especially when I have been teaching it to others. It also makes the ol' perl5
open FH, "|/usr/bin/foo";
make a lot more sense. Using something like
open "p", "/usr/bin/foo";
just wouldn't have the same ring to it. Aside from which, it gets even worse when you consider how you would have to change:
open FH, "/usr/bin/foo|";
Greg -- Greg Boug (gb...@unico.com.au) Systems and Network Administrator UNICO Computer Systems Melbourne, Australia. Phone: +61-3-9865-9116
On Thu 15 Jul 2004 11:42, Michele Dondi <bla...@pcteor1.mi.infn.it> wrote:
> On Tue, 13 Jul 2004, Juerd wrote:
> > open '<', $foo; > > open '>', $foo;
> > is much harder to read than
> > open 'r', $foo; > > open 'w', $foo;
> Are you sure?!? I would tend to disagree...
So do I. "<", and ">" are imho MUCH clearer than 'r' and 'w' for several reasons
0. More appealing to the eye 1. They do not ambiguate with files named 'r', or 'w' 2. They don't have to be translated (in german that would be 'l' and 's') 3. They play nice with possible extensions 'open ">:utf8", $file;
> not that MHO is particularly > important, I guess, but just to stress the fact that it is by large a > subjective matter...
-- H.Merijn Brand Amsterdam Perl Mongers (http://amsterdam.pm.org/) using perl-5.6.1, 5.8.3, & 5.9.x, and 809 on HP-UX 10.20 & 11.00, 11i, AIX 4.3, SuSE 9.0, and Win2k. http://www.cmve.net/~merijn/ http://archives.develooper.com/daily-bu...@perl.org/ perl...@perl.org send smoke reports to: smokers-repo...@perl.org, QA: http://qa.perl.org
Greg Boug wrote: > I have always felt that keeping it the same as shell scripting was a handy > thing, especially when I have been teaching it to others. It also makes > the ol' perl5
> open FH, "|/usr/bin/foo";
> make a lot more sense. Using something like
> open "p", "/usr/bin/foo";
> just wouldn't have the same ring to it. Aside from which, it gets even worse > when you consider how you would have to change:
The pipe would be legal on either side of the string. This would still allow the often-useful "type a pipe command at a prompt for a file", while matching the trait-based syntax suggested elsewhere.
-- Brent "Dax" Royal-Gordon <br...@brentdax.com> Perl and Parrot hacker
Brent 'Dax' Royal-Gordon writes: > My personal preference is for:
> $in=open :r "|/usr/bin/foo";
> The pipe would be legal on either side of the string. This would > still allow the often-useful "type a pipe command at a prompt for a > file",
And it still allows for all those security holes in websites where inexperienced programmers are just expecting a filename but write code that's capable of executing commands. Such behaviour is non-obvious to somebody who hasn't been specifically warned about the danger, and the potential for abuse is high.