lazy-loading objects in perl6 - how will they look?
Hola, Object::Realize::Later and friends in perl5 get the job done, but have many caveats. Will there be a mechanism to provide a way to do inplace replacement of an object's, err, thingyness (class, data), without losing it's identity? I would like a way to transpose objects between classes in the same instance for several reasons beside lazy loading, btw. I think one of the most beautiful uses is to provide protocol handlers in a state machine style implementation, mainly for security by simplicity. For example, here is the design of a pop3 server (i actually implemented it in perl5 and it wasn't too bad): parse input, regexy, into cmd args format call $backend_obj-cmd(@args); Each command is a method call, and to switch the modes of the statemachine, $backend_obj would rebless itself into different classes, with different commands. The pass command, for example, will transpose the object to the authenticated state, if the user/password pair matches. If it fails it returns to the connected state. The user command is a part of the connected state, and transposes to the accepting password state, a subclass of the connected state. Since method dispatch is quite rigorously tested edge cases are pretty much tested for, and the implementation is very robust, and has very explicit logic, provided you're willing to eat it. I'd like to be able to exploit this more cleanly in Perl 6, and also get easier and safer lazy loading of objects. -- () Yuval Kogman [EMAIL PROTECTED] 0xEBD27418 perl hacker /\ kung foo master: /me whallops greyface with a fnord: neeyah!!! pgpfMiub6SlqJ.pgp Description: PGP signature
lists in string context
An old exegesis says that ~foo bar is foo bar. It was still _('foo', 'bar') back then, though. This behaviour I couldn't find in the Synopses, but it wouldn't be the first time I completely overlook important information while looking for it. I think having it stringify as foobar is more useful, because in my scripts I more often join on '' than on ' '. For short join(' ') syntax, we already have @array[]. Huffman's principle agrees, for the scope of my repositories. With this, I wonder what reverse(LIST) does in scalar context. Does it join on '', like Perl 5 does? Or on ' ', consistent with current design for ~LIST? My gut prefers that both scalar reverse LIST and ~LIST join LIST on ''. Juerd -- http://convolution.nl/maak_juerd_blij.html http://convolution.nl/make_juerd_happy.html http://convolution.nl/gajigu_juerd_n.html
Re: lists in string context
Juerd skribis 2005-03-12 20:32 (+0100): My gut prefers that both scalar reverse LIST and ~LIST join LIST on ''. scalar reverse LIST probably returns an arrayref. I meant ~reverse LIST, which should probably do ~LIST at some point instead of join($sep, LIST), for consistency, and my request is that ~ join on '', not ' '. Juerd -- http://convolution.nl/maak_juerd_blij.html http://convolution.nl/make_juerd_happy.html http://convolution.nl/gajigu_juerd_n.html
Re: lazy-loading objects in perl6 - how will they look?
On Sat, Mar 12, 2005 at 21:21:23 +0200, Yuval Kogman wrote: Hola, Object::Realize::Later and friends in perl5 get the job done, but have many caveats. FYI, Juerd told me how to clean this up with Data::Swap (err, Data::Alias) more cleanly in perl 5. Thanks! -- () Yuval Kogman [EMAIL PROTECTED] 0xEBD27418 perl hacker /\ kung foo master: *shu*rik*en*sh*u*rik*en*s*hur*i*ke*n*: neeyah pgpuMxB5hftsY.pgp Description: PGP signature
Re: lists in string context
On Sat, Mar 12, 2005 at 08:37:52PM +0100, Juerd wrote: : Juerd skribis 2005-03-12 20:32 (+0100): : My gut prefers that both scalar reverse LIST and ~LIST join LIST on ''. : : scalar reverse LIST probably returns an arrayref. : : I meant ~reverse LIST, which should probably do ~LIST at some point : instead of join($sep, LIST), for consistency, and my request is that ~ : join on '', not ' '. Well, we thrashed that one around a lot at one of our meetings, and the general consensus was that array interpolation and the default stringification of arrays should probably work the same for consistency. (Likewise for hashes.) And that the default for both of those needs some whitespace. Basically we didn't want to have to explain why print @foo[] is different from print [EMAIL PROTECTED] On the other hand, without an argument, the method form of .join: print [EMAIL PROTECTED] is likely to join on ''. Or you could write print @foo.join() Or if we decide that's too likely to lead people to write something confusing like join(@foo) then we can come up with some other means of doing it succinctly, such as: cat(@foo) @foo.cat Or we could redefine the signature of join to make the delimiter a named optional parameter so that join(LIST) assumes a delimiter of ''. That might raise a few eyebrows though. And arguably, the current structure of join is that the delimiter is the invocant, so cat should be defined as ''.join(@foo) Larry
Re: lists in string context
Larry Wall skribis 2005-03-12 12:26 (-0800): Well, we thrashed that one around a lot at one of our meetings, and the general consensus was that array interpolation and the default stringification of arrays should probably work the same for consistency. (Likewise for hashes.) And that the default for both of those needs some whitespace. Fair enough, but does this mean that ~reverse(foo bar) eq rab oof, and not raboof? cat(@foo) @foo.cat I like this. And arguably, the current structure of join is that the delimiter is the invocant, so cat should be defined as ''.join(@foo) This is what Python does. It does not make any sense to me, and I can't wrap my mind around it at all. Ruby-ish @foo.join('') seems more natural. Just like with how I prefer $fh.print($text) to $text.print($fh), I cannot explain WHY this is how my mind works. Juerd -- http://convolution.nl/maak_juerd_blij.html http://convolution.nl/make_juerd_happy.html http://convolution.nl/gajigu_juerd_n.html
does imply () or are pairs special?
%foobar is really %foo{'bar'} and :foobar is actually :foo('bar') naturally, :foobar, 'baz' is :foo('bar'), 'baz' but is reversebar, 'baz' then reverse('bar'), 'baz' ? And if that is so, then is reverse bar, 'baz' any different? Juerd -- http://convolution.nl/maak_juerd_blij.html http://convolution.nl/make_juerd_happy.html http://convolution.nl/gajigu_juerd_n.html
Re: lists in string context
Juerd wrote: Larry Wall skribis 2005-03-12 12:26 (-0800): And arguably, the current structure of join is that the delimiter is the invocant, so cat should be defined as ''.join(@foo) This is what Python does. It does not make any sense to me, and I can't wrap my mind around it at all. Ruby-ish @foo.join('') seems more natural. Just like with how I prefer $fh.print($text) to $text.print($fh), I cannot explain WHY this is how my mind works. I'll hazard that your mind works that way because in both examples, you're considering the invocant as an indirect object, not a direct object. Deliver the bucket to the shed. Makes a lot more sense as: $bucket.deliver($shed); Than $shed.deliver($bucket); I think that the direct object needs to be the invocant, and the indirect object be the parameter to the method. However, one could easily say: Send the String to this File. Augment this File with the String. Shove commas between everything in the list. Give me a list with commas as delimiters. Which means I think we need all of: $str.print($fh); $fh.print($str); ','.join(@list); @list.join(''); I see the argument for having both of them as the same as having the argument for both foo() if $cond; if $cond {foo()}; Sometimes it's the $cond that's important, sometimes it's the foo(). Obviously this can't happen for everything, but for the builtin methods and classes, I don't see a penalty for supporting both forms. Consider: $str.split($rule); $rule.split($str); I can see using both of those. But I can't see $int.split($rule, $str); as an alias for $str.split($rule, $int_limit); because I doubt that the limit will ever be the most important thing about a split operation. -- Rod Adams
A possible solution for s?pintf
Without introduction, I'll just present the syntax idea: f/%03d %15s/$foo, $bar/; This gives s?printf to any expression with short and concise syntax, making printf redundant, which means I won't even have to start a discussion about sayf :) printf %03d %15s, $foo, $bar; vs print f/%03d %15s/$foo, $bar/; Of course, this is s///-like in quoting behaviour, so f[][] or f should work just as well. The RHS is not a string, but parsed as an expression in list context. If this feels weird, just think of s///e, where the RHS is also not a string. Juerd -- http://convolution.nl/maak_juerd_blij.html http://convolution.nl/make_juerd_happy.html http://convolution.nl/gajigu_juerd_n.html
Re: does imply () or are pairs special?
On Sat, Mar 12, 2005 at 09:40:46PM +0100, Juerd wrote: : %foobar : : is really : : %foo{'bar'} : : and : :foobar : : is actually : : :foo('bar') But it's not--it's actually :foo{'bar'} What's happening is that :foo is using the subscript syntax oddly. : naturally, : : :foobar, 'baz' : : is : : :foo('bar'), 'baz' Nope, :foo{'bar'}, 'baz' : but is : : reversebar, 'baz' : : then : : reverse('bar'), 'baz' No, that's reverse{'bar'}, 'baz' which makes sense only if reverse returns something that can be hash subscripted. : ? And if that is so, then is : : reverse bar, 'baz' : : any different? That's the same as reverse qw/bar/, 'baz' Larry
Re: A possible solution for s?pintf
Juerd wrote: Without introduction, I'll just present the syntax idea: f/%03d %15s/$foo, $bar/; This gives s?printf to any expression with short and concise syntax, making printf redundant, which means I won't even have to start a discussion about sayf :) printf %03d %15s, $foo, $bar; vs print f/%03d %15s/$foo, $bar/; Of course, this is s///-like in quoting behaviour, so f[][] or f should work just as well. The RHS is not a string, but parsed as an expression in list context. If this feels weird, just think of s///e, where the RHS is also not a string. Why not just rename C sprintf to C format and ditch printf and sayf? I will also remind you of the Scalar C .as() method, which sprintf's it's value. print $foo.as('%03d') $bar.as('%15s'); But I'd still like to keep something works on a whole list at once for those cases where I'm doing several non-trivial expressions at once. -- Rod Adams
Re: A possible solution for s?pintf
Rod Adams skribis 2005-03-12 17:41 (-0600): Why not just rename C sprintf to C format and ditch printf and sayf? Because format is almost as much typing as sprintf, and in many circumstances needs both parens and quotes: format(%03d %15s, $foo, $bar), $baz, ... compared to f/%03d %15s/$foo, $bar/, $baz, ... I will also remind you of the Scalar C .as() method, which sprintf's it's value. print $foo.as('%03d') $bar.as('%15s'); I like sprintf because with it, I can separate data from presentation. When I interpolate, I want the things I'm interpolating to be as simple and short as possible. Hello, $name looks natural. Hello, $person.name looks a little less natural. Any .as(ugliness) makes it impossible to read and leaves you with code-in-text, which as many templating languages have already shown can very quickly get very ugly. Juerd -- http://convolution.nl/maak_juerd_blij.html http://convolution.nl/make_juerd_happy.html http://convolution.nl/gajigu_juerd_n.html
Re: A possible solution for s?pintf
Juerd [EMAIL PROTECTED] wrote: Without introduction, I'll just present the syntax idea: f/%03d %15s/$foo, $bar/; Of course, this is s///-like in quoting behaviour, so f[][] or f should work just as well. The RHS is not a string, but parsed as an expression in list context. If this feels weird, just think of s///e, where the RHS is also not a string. I think s///e is going away, since you can just use s/pattern/{code}/ now anyway. Besides, I think as will do just fine, especially since you can now interpolate method calls as well. You can even do something like this if you want to perform bulk formatting: say join ' ', ($n1, $n2, $n3) .as('%d'); Or, if that's not quite sufficient: say map { .key.as(.value) } $num = '%d', $str = '%s', ...; -- Brent 'Dax' Royal-Gordon [EMAIL PROTECTED] Perl and Parrot hacker I used to have a life, but I liked mail-reading so much better.
Re: A possible solution for s?pintf
On Sat, Mar 12, 2005 at 11:57:39PM +0100, Juerd wrote: : Without introduction, I'll just present the syntax idea: : : f/%03d %15s/$foo, $bar/; : : This gives s?printf to any expression with short and concise syntax, : making printf redundant, which means I won't even have to start a : discussion about sayf :) : : printf %03d %15s, $foo, $bar; : : vs : : print f/%03d %15s/$foo, $bar/; Well, we do already have: print $foo.as('%03d'), $bar.as('%15s') which works on interpolated values as well. It als puts the variable name out front, since the name is more important than the pattern in most cases. : Of course, this is s///-like in quoting behaviour, so f[][] or f : should work just as well. The RHS is not a string, but parsed as an : expression in list context. I don't see that this buys us anything over just shortening sprintf to something shorter, like: print as '%03d %15s', $foo, $bar; And your argument list falls out naturally from making as a listop. Plus it naturally lets you say other as-ly things: print as MyBigInt, $foo, $bar; : If this feels weird, just think of s///e, : where the RHS is also not a string. Um, actually we did away with /e in Perl 6... Larry
Re: A possible solution for s?pintf
Brent 'Dax' Royal-Gordon skribis 2005-03-12 15:51 (-0800): Besides, I think as will do just fine, especially since you can now interpolate method calls as well. You can even do something like this if you want to perform bulk formatting: say join ' ', ($n1, $n2, $n3) .as('%d'); Or, if that's not quite sufficient: say map { .key.as(.value) } $num = '%d', $str = '%s', ...; I'm really getting the feeling I'm the only one who uses sprintf because it *separates* and lets you write complex things on one simple line. That, and I use it a lot in one liners. Writing complex things as complex things feels weird. I know there are alternatives, and they're good. I can certainly see how .as can come in handy. But sprintf (or call it format, which I do like better than the unpronounceable sprintf) as a semi-list operator is very useful too. I just don't like the amount of typing (with the nested delimiters: parens and quotes) it requires. Juerd -- http://convolution.nl/maak_juerd_blij.html http://convolution.nl/make_juerd_happy.html http://convolution.nl/gajigu_juerd_n.html
Re: A possible solution for s?pintf
Larry Wall skribis 2005-03-12 15:55 (-0800): Well, we do already have: print $foo.as('%03d'), $bar.as('%15s') which works on interpolated values as well. It als puts the variable name out front, since the name is more important than the pattern in most cases. It puts the variable name out front, which is great, but it also puts the second variable name a the way to the right, after the line noise. I don't see that this buys us anything over just shortening sprintf to something shorter, like: print as '%03d %15s', $foo, $bar; I like it, although in more complex lists, it still requires the parens that I so like to avoid. I could certainly live with a list op as short as 'as', though! Juerd -- http://convolution.nl/maak_juerd_blij.html http://convolution.nl/make_juerd_happy.html http://convolution.nl/gajigu_juerd_n.html
Re: lists in string context
On Sat, Mar 12, 2005 at 03:13:37PM -0600, Rod Adams wrote: : Obviously this can't happen for everything, but for the builtin methods : and classes, I don't see a penalty for supporting both forms. Consider: : :$str.split($rule); :$rule.split($str); : : I can see using both of those. But I can't see : :$int.split($rule, $str); : : as an alias for : :$str.split($rule, $int_limit); : : because I doubt that the limit will ever be the most important thing : about a split operation. Just for the record, let me say that I expect the limit parameter to split to be +$limit and not ?$limit. I think a number of seldom-used options will go this route. Another consideration is whether the parameter needs to be considered for MMD. Larry
Re: lazy-loading objects in perl6 - how will they look?
On Sat, Mar 12, 2005 at 09:21:23PM +0200, Yuval Kogman wrote: : Hola, : : Object::Realize::Later and friends in perl5 get the job done, but : have many caveats. : : Will there be a mechanism to provide a way to do inplace replacement : of an object's, err, thingyness (class, data), without losing it's : identity? : : : I would like a way to transpose objects between classes in the same : instance for several reasons beside lazy loading, btw. : : I think one of the most beautiful uses is to provide protocol : handlers in a state machine style implementation, mainly for : security by simplicity. Sounds like simple delegation to me. The delegation model in Perl 6 lets you specify which methods the delegate handles without committing to the identity or existence of the delegated object. You can probably even get an extra degree of type safety between the delegatee and delegator by using roles to pull in your interfaces. The default implementation for your role's methods could delegate, while the delegated implementation object defines them to do something real. Larry
Re: A possible solution for s?pintf
On Sun, Mar 13, 2005 at 12:58:50AM +0100, Juerd wrote: : I'm really getting the feeling I'm the only one who uses sprintf because : it *separates* and lets you write complex things on one simple line. : That, and I use it a lot in one liners. Then you should feel much better after you read my message where I use as as a list operator. : I know there are alternatives, and they're good. I can certainly see how : .as can come in handy. But sprintf (or call it format, which I do like : better than the unpronounceable sprintf) as a semi-list operator is : very useful too. I just don't like the amount of typing (with the nested : delimiters: parens and quotes) it requires. A list operator doesn't require parens unless you start chunking up your outputs into sublists, but if you do that then you're falling into the same trap of scattering complexity around that you complained about for .as(). Larry
Re: A possible solution for s?pintf
Juerd wrote: Rod Adams skribis 2005-03-12 17:41 (-0600): Why not just rename C sprintf to C format and ditch printf and sayf? Because format is almost as much typing as sprintf, and in many circumstances needs both parens and quotes: format(%03d %15s, $foo, $bar), $baz, ... compared to f/%03d %15s/$foo, $bar/, $baz, ... Well, I use s?printf extensively in my code, and IMO, it's about the right level of huffmanization. C .as() seems to nicely take care of the cases where C sprintf is too heavy. Also, on second thought, don't ditch C printf . I use it too much. But I don't see a need for C sayf . If you're going through the effort of supplying a pattern, you can type the \n. What's always bothered me about s?printf was that it was doing something roughly similar to RE's: describing a string pattern, but used a completely different syntax. Admittedly, it's working in the other direction, generation, not parsing, but they still seem terribly far apart at that. But I haven't thought enough about it to offer a proposal on the subject. -- Rod Adams
Re: A possible solution for s?pintf
Larry Wall wrote: I don't see that this buys us anything over just shortening sprintf to something shorter, like: print as '%03d %15s', $foo, $bar; And your argument list falls out naturally from making as a listop. Plus it naturally lets you say other as-ly things: print as MyBigInt, $foo, $bar; ooh. Nice. I like that. -- Rod Adams
Re: A possible solution for s?pintf
Larry Wall wrote: I don't see that this buys us anything over just shortening sprintf to something shorter, like: print as '%03d %15s', $foo, $bar; And your argument list falls out naturally from making as a listop. Plus it naturally lets you say other as-ly things: print as MyBigInt, $foo, $bar; This seems to have some overlap with the form listop in E7. Do we need both, or will we make cForm a possible 1st arg to Cas. Eg, (modified from the first E7 example): sub myster_rite { our ($name, $age, $ID, $comments); print as Form :interleave '.' === | NAME |AGE | ID NUMBER | |--++---| | {} | {} | {} | |===| | COMMENTS | |---| | {[[[} | === . $name, $age,$ID, $comments; }
for @list sub;
Are the following all legal and equivalent? for 1..10 - $a, $b { say $a, $b }; for 1..10 { say $^a, $^b }; sub foo ($a, $b) { say $a, $b }; for 1..10 foo; What happens with: for 1..10 - [EMAIL PROTECTED] { say @a }; -- Rod Adams
Re: for @list sub;
Rod Adams writes: Are the following all legal and equivalent? for 1..10 - $a, $b { say $a, $b }; for 1..10 { say $^a, $^b }; sub foo ($a, $b) { say $a, $b }; for 1..10 foo; Almost. The last one should be: for 1..10, foo; What happens with: for 1..10 - [EMAIL PROTECTED] { say @a }; Good question. That's a function of how Cfor interprets the arity. The formal arity of a sub with *@ is Inf, so I suppose say would get 1..10 and the loop would run once. That's probably the best way for Cfor to behave, because that's what I'd expect in this case. Luke
Re: A possible solution for s?pintf
On Sun, Mar 13, 2005 at 01:01:39AM +0100, Juerd wrote: : It puts the variable name out front, which is great, but it also puts : the second variable name a the way to the right, after the line : noise. print $foo.as('%03d'), $bar.as('%15s'); Larry
Re: A possible solution for s?pintf
Matt Diephouse wrote: Brent 'Dax' Royal-Gordon [EMAIL PROTECTED] wrote: Besides, I think as will do just fine, especially since you can now interpolate method calls as well. You can even do something like this if you want to perform bulk formatting: say join ' ', ($n1, $n2, $n3) .as('%d'); What about: say [ $n1, $n2, $n3 .as('%d') ].join; ? Can I (1) use join on a bracketed list Certainly. Almost anywhere you can use an array, an array ref will do instead. and (2) leave off the parentheses around C$n1, $n2, $n3? (I couldn't find where hyper ops are on the precedence table.) I think you'd need the parens there, to distinguish that the . operator applies to the list, not to $n3. Hyper's are not on the list because they are adverbs to the existing operators. In this case, you're using unary ., so you follow it's precedence. You could easily write the above as say (($n1, $n2, $n3)».as('%d')).join; What I'm not certain about is if say ($n1, $n2, $n3)».as('%d').join; does the same thing, but I think it does. And this: say [ $num = '%d', $str = '%s' ] .key.as(.value); No. .key returns a string, which you call the .as method of, which is fine, but the .value is a separate expression, and references the current topic, which is not tied to the array. Related to what I'm not sure about above is that I think you'd have to say C [...]».key».as($pattern) to get it working correctly. (4) use square brackets in this instance (to make sure my list doesn't form a parameter list to Csay)? Yes, but whitespace between the say and ( should do the trick as well. Might generate a warning, however. -- Rod Adams
The S29 Functions Project
Barring objections, I'm going to attempt to compile a S29. Plan of attack: I'm using a recent copy of Perl 5's perlfunc as a very rough template. At some point, I'll drudge through the A's and S's to look for functions new to Perl 6. I'll try not to make up too many new ones on my own, but I'll mark them as such. When I've written up a given function from what I believe it will be, I'll post the relevant section of pod to the list for comments/approval. I will not be attempting to rewrite perlfunc. The target audience will be someone familiar with Perl 5 and the other synopses. I will be listing sub/method signatures, including variants found in the standard type classes. One thing I've already done is make a list of Perl 5 functions I cannot address until some future Apocalypse/Synopsis is generated on the topic. By far the bulk of this list falls on S16. Partly because IPC is a mess, and partly because I lumped all I/O in there. The list of pending functions, by group, is: =over 8 =item A/S14: Tied Variables tie tied untie =item A/S16: IPC / IO / Signals -X accept alarm bind binmode chown close closedir connect eof fcntl fileno flock getc getpeername /[get|set][host|net|proto|serv|sock].*/ glob ioctl kill link listen lstat mkdir /msg.*/ open opendir pipe print printf read readdir readline readlink readpipe recv rename rewinddir rmdir seek seekdir select(both) /sem.*/ send setsockopt /shm.*/ shutdown socket socketpair stat symlink syscall sysopen sysread sysseek syswrite tell telldir truncate umask unlink utime =item A/S??: OS Interaction chroot exec getlogin /[get|set][pw|gr].*/ setpgrp setpriority system times =item A/S17: Threads fork lock wait waitpid =back And now for a few simple functions to get things started: =item multi sub abs (?Num) returns Num =item multi method Num::abs () returns Num Absolute Value. $_ if omited. =item multi sub atan2 (Num, Num) returns Num Arctangent of Y/X (radians) =item multi sub cos (?Num) returns Num =item multi method Num::cos () returns Num Cosine (radians). $_ if omited. =item multi sub exp (?Num) returns Num =item multi method Num::exp() returns Num Exponent or inverse natural log. $_ if omited. -- Rod Adams