Re: Duh v D'oh
On Thu, 13 Nov 2008 11:24:27 +0100, Paul Johnson wrote: On Thu, Nov 13, 2008 at 10:12:25AM +, Peter Haworth wrote: while(my $k=each %h){ # Explicit iteration of keys } You could argue that making it robust in the face of false keys reduces the subtlely somewhat, but the extra unwieldiness also obscures the intent somewhat in my view: while(defined(my $k=each %h)){ } $ perl -MO=Deparse -e 'while(my $k=each %h){}' while (defined(my $k = each %h)) { (); } $ Rats, I did wonder whether that had been fixed, similar to the while($line=){} protection, but I didn't think to actually test it. -- Peter Haworth [EMAIL PROTECTED] On an enjoyment level, this is somewhere between repeatedly trapping my finger in a door, and sandpapering my gums. -- Mikefule
Re: Duh v D'oh
On Fri, 07 Nov 2008 12:44:40 +, Nigel Rantor wrote: Yes, well, Perl has 'each' for iterating over pairs, so they look almost identical. The main difference I can see is that python defaults to assuming you want the keys of a hash when iterating rather than the values. Perl also has 'each' for iterating over keys, and there's no default - it's always explicit, though possibly a bit subtle: while(my($k,$v)=each %h){ # Explicit iteration of pairs } while(my $k=each %h){ # Explicit iteration of keys } You could argue that making it robust in the face of false keys reduces the subtlely somewhat, but the extra unwieldiness also obscures the intent somewhat in my view: while(defined(my $k=each %h)){ } -- Peter Haworth [EMAIL PROTECTED] Unfortunately, in this case, ``obvious'' is synonymous with ``wrong''. -- Larry Wall in Apocalypse 3
Re: Duh v D'oh
On Thu, Nov 13, 2008 at 12:34:54PM +, Paul Makepeace wrote: stix:~$ python Python 2.4.5 (#2, Aug 1 2008, 12:02:10) [GCC 4.3.1] on linux2 Type help, copyright, credits or license for more information. a = {'foo': 'bar'} a['foo'] 'bar' for k in a: ... print k ... foo for k in a: print k ... foo exit Use exit() or Ctrl-D (i.e. EOF) to exit OK, it was easy, but it illustrates the differences in mentality so well. ;-) -- Philippe Bruhat (BooK) Putting beauty before brains is the surest way to wind up with neither. (Moral from Groo The Wanderer #24 (Epic))
Re: Duh v D'oh
On Thu, Nov 13, 2008 at 1:15 PM, Paul Makepeace [EMAIL PROTECTED] wrote: On Thu, Nov 13, 2008 at 12:59 PM, Philippe Bruhat (BooK) [EMAIL PROTECTED] wrote: On Thu, Nov 13, 2008 at 12:34:54PM +, Paul Makepeace wrote: stix:~$ python Python 2.4.5 (#2, Aug 1 2008, 12:02:10) [GCC 4.3.1] on linux2 Type help, copyright, credits or license for more information. a = {'foo': 'bar'} a['foo'] 'bar' for k in a: ... print k ... foo for k in a: print k ... foo exit Use exit() or Ctrl-D (i.e. EOF) to exit OK, it was easy, but it illustrates the differences in mentality so well. ;-) DB1 exit Debugged program terminated. Use q to quit or R to restart, use o inhibit_exit to avoid stopping after program termination, h q, h R or h o to get additional info. DB2 Not sure what to conclude from either of those :-) Try ^D after that exit, DB2 Config::DESTROY(/System/Library/Perl/5.8.8/darwin-thread-multi-2level/Config.pm:63): 63: sub DESTROY { } DB2 exit starburst:~ paulmakepeace$ Ya OK :) Then you have, DB1 quit DB2 And, DB1 ^C^C^C^C^C
Re: Duh v D'oh
Paul Makepeace [EMAIL PROTECTED] writes: On Mon, Nov 10, 2008 at 10:10 AM, Philippe Bruhat (BooK) [EMAIL PROTECTED] wrote: On Sat, Nov 08, 2008 at 02:47:51AM +, Nigel Rantor wrote: Eden Cardim wrote: Yes, that'd be cool and instructive, since I haven't had the time to dig in to things like Python and Ruby. I'll confess that my vision of Perl practicity might be clouded by the previous C, C++ and Java experiences. It would be interesting if we had some basis for comparison. There was this project: http://pleac.sourceforge.net/ PLEAC - Programming Language Examples Alike Cookbook A more 'meta' example: Compare and contrast working with the debugger, stix:~$ perl -de0 Loading DB routines from perl5db.pl version 1.28 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): 0 DB1 $a = {foo = 'bar'} DB2 $a-{foo} DB3 foreach my $k (keys %$a) { Missing right curly or square bracket at (eval 20)[/usr/share/perl/5.8/perl5db.pl:628] line 4, at end of line syntax error at (eval 20)[/usr/share/perl/5.8/perl5db.pl:628] line 4, at EOF DB4 foreach my $k (keys %$a) { print $k; } foo DB5 stix:~$ Python's REPL is hardly a debugger, comparing it to something like Devel::REPL is a bit fairer: [EMAIL PROTECTED]:~$ re.pl $ $a = { foo = 'bar' } $HASH1 = { foo = 'bar' }; $ $a-{foo} bar $ foreach my $k (keys %$a) { print $k } foo $ -- ilmari A disappointingly low fraction of the human race is, at any given time, on fire. - Stig Sandbeck Mathisen
Re: Duh v D'oh
On Thu, Nov 13, 2008 at 01:15:44PM +, Paul Makepeace wrote: DB1 exit Debugged program terminated. Use q to quit or R to restart, use o inhibit_exit to avoid stopping after program termination, h q, h R or h o to get additional info. DB2 Not sure what to conclude from either of those :-) I'm sure you can conclude that I do not conduct thorough research when trolling. :-) -- Philippe Bruhat (BooK) When you allow legends to rule your life, your world is based on fiction. (Moral from Groo The Wanderer #99 (Epic))
Re: Duh v D'oh
On Thu, Nov 13, 2008 at 10:12:25AM +, Peter Haworth wrote: while(my $k=each %h){ # Explicit iteration of keys } You could argue that making it robust in the face of false keys reduces the subtlely somewhat, but the extra unwieldiness also obscures the intent somewhat in my view: while(defined(my $k=each %h)){ } $ perl -MO=Deparse -e 'while(my $k=each %h){}' while (defined(my $k = each %h)) { (); } $ -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net
Re: Duh v D'oh
On Thu, Nov 13, 2008 at 12:59 PM, Philippe Bruhat (BooK) [EMAIL PROTECTED] wrote: On Thu, Nov 13, 2008 at 12:34:54PM +, Paul Makepeace wrote: stix:~$ python Python 2.4.5 (#2, Aug 1 2008, 12:02:10) [GCC 4.3.1] on linux2 Type help, copyright, credits or license for more information. a = {'foo': 'bar'} a['foo'] 'bar' for k in a: ... print k ... foo for k in a: print k ... foo exit Use exit() or Ctrl-D (i.e. EOF) to exit OK, it was easy, but it illustrates the differences in mentality so well. ;-) DB1 exit Debugged program terminated. Use q to quit or R to restart, use o inhibit_exit to avoid stopping after program termination, h q, h R or h o to get additional info. DB2 Not sure what to conclude from either of those :-) -- Philippe Bruhat (BooK) Putting beauty before brains is the surest way to wind up with neither. (Moral from Groo The Wanderer #24 (Epic))
Re: Duh v D'oh
On Mon, Nov 10, 2008 at 10:10 AM, Philippe Bruhat (BooK) [EMAIL PROTECTED] wrote: On Sat, Nov 08, 2008 at 02:47:51AM +, Nigel Rantor wrote: Eden Cardim wrote: Yes, that'd be cool and instructive, since I haven't had the time to dig in to things like Python and Ruby. I'll confess that my vision of Perl practicity might be clouded by the previous C, C++ and Java experiences. It would be interesting if we had some basis for comparison. There was this project: http://pleac.sourceforge.net/ PLEAC - Programming Language Examples Alike Cookbook A more 'meta' example: Compare and contrast working with the debugger, stix:~$ perl -de0 Loading DB routines from perl5db.pl version 1.28 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): 0 DB1 $a = {foo = 'bar'} DB2 $a-{foo} DB3 foreach my $k (keys %$a) { Missing right curly or square bracket at (eval 20)[/usr/share/perl/5.8/perl5db.pl:628] line 4, at end of line syntax error at (eval 20)[/usr/share/perl/5.8/perl5db.pl:628] line 4, at EOF DB4 foreach my $k (keys %$a) { print $k; } foo DB5 stix:~$ stix:~$ python Python 2.4.5 (#2, Aug 1 2008, 12:02:10) [GCC 4.3.1] on linux2 Type help, copyright, credits or license for more information. a = {'foo': 'bar'} a['foo'] 'bar' for k in a: ... print k ... foo for k in a: print k ... foo With perl you, a) have to remember some 'trick' to get into the debugger [-de0 v. nothing] b) can't just refer to expressions to inspect them [requires x $expr] c) can't do multiline commands d) ^D doesn't produce a tidy exit Arguably this is a function of perl not really having a first class interactive shell. Either way a proper shell is a really useful feature of many other languages that I've always felt was a major lack in perl. Having a shell to do ad-hoc experiments with makes learning a language much easier IME. (Of course someone's going to show how perl 5.x actually does have this and I've been living in cave the last $embarrassing_num years :-)) P
Re: Duh v D'oh
On Sat, Nov 08, 2008 at 02:47:51AM +, Nigel Rantor wrote: Eden Cardim wrote: Yes, that'd be cool and instructive, since I haven't had the time to dig in to things like Python and Ruby. I'll confess that my vision of Perl practicity might be clouded by the previous C, C++ and Java experiences. It would be interesting if we had some basis for comparison. There was this project: http://pleac.sourceforge.net/ PLEAC - Programming Language Examples Alike Cookbook -- Philippe Bruhat (BooK) When you wander near evil, Security is only a function of foolishness... (Moral from Groo The Wanderer #21 (Epic))
Re: Duh v D'oh
Paul Makepeace wrote: foreach my $given_source (%publication_map_by_name) { ENOKEYS: You are locked out of the house. A
Re: Duh v D'oh
On Fri, Nov 7, 2008 at 11:48 AM, Peter Corlett [EMAIL PROTECTED] wrote: On Thu, Nov 06, 2008 at 05:00:16PM +, Nigel Rantor wrote: Paul Makepeace wrote: [...] If it's not staring out at you, you're possibly also a python programmer. Python here arguably DWIM better than perl. Does python give you the keys by default? Sort of. In Python, for loops over values returned by an iterator, and a hash implements that interface. The interface is pretty clean, my_dict = {'a': 1, 'b': 2} for k in my_dict: print k gives, a b for k, v in my_dict.items(): print k, v gives, a 1 b 2 (ObPedantry, .iteritems() is preferred since it evaluates lazily with a generator.) P
Re: Duh v D'oh
On Thu, Nov 6, 2008 at 1:33 PM, Paul Makepeace [EMAIL PROTECTED] wrote: Following on the theme from a conversation with Mark at Dim Sum today*, spot the mistake I just wasted an hour on, my $publication_stats = [['Source', 'ID Publication', 'Article count']]; foreach my $given_source (%publication_map_by_name) { push @$publication_stats, [$given_source, $publication_map_by_name{$given_source}, $article_count_by_source{$given_source}]; } If it's not staring out at you, you're possibly also a python programmer. Python here arguably DWIM better than perl. Paul * there's some mistakes where you at the end go D'oh! because perl is messing you around with its weirdness or otherwise non-intuitive behavior. Duh when you're messing around and just not paying attention. I wouldn't call that non-intuitive, it's a matter of culture. Perl coders are used to being verbose about whether they want keys/values from a hash. If someone changed the behavior to make a hash in list context return it's keys, you would confuse a lot of Perl programmers out there too. And I'm particularly fond of perl's behaviour because it allows interesting cookbook tricks such as %config = (%default, %user_provided) -- Eden Cardim Need help with your Catalyst or DBIx::Class project? Code Monkeyhttp://www.shadowcat.co.uk/catalyst/ Shadowcat Systems Ltd. Want a managed development or deployment platform? http://edenc.vox.com/http://www.shadowcat.co.uk/servers/
Re: Duh v D'oh
On Fri, Nov 07, 2008 at 09:45:34AM -0300, Eden Cardim wrote: On Thu, Nov 6, 2008 at 1:33 PM, Paul Makepeace [EMAIL PROTECTED] wrote: Following on the theme from a conversation with Mark at Dim Sum today*, spot the mistake I just wasted an hour on, my $publication_stats = [['Source', 'ID Publication', 'Article count']]; foreach my $given_source (%publication_map_by_name) { push @$publication_stats, [$given_source, $publication_map_by_name{$given_source}, $article_count_by_source{$given_source}]; } If it's not staring out at you, you're possibly also a python programmer. Python here arguably DWIM better than perl. Paul * there's some mistakes where you at the end go D'oh! because perl is messing you around with its weirdness or otherwise non-intuitive behavior. Duh when you're messing around and just not paying attention. I wouldn't call that non-intuitive, it's a matter of culture. Perl coders are used to being verbose about whether they want keys/values from a hash. If someone changed the behavior to make a hash in list context return it's keys, you would confuse a lot of Perl programmers out there too. And I'm particularly fond of perl's behaviour because it allows interesting cookbook tricks such as %config = (%default, %user_provided) I don't think the Python behaviour excludes the above trick. It is as if Python has a context that Perl doesn't have [1]: iterator context. In such a context, just the keys are returned. No doubt there will be Perl code that will break if Perl was changed such that 'for(EXPR)' provides iterator context to EXPR, and a hash in such a context acted as if it was 'scalar each %hash', but I think it would not break that much. 'for (%hash)' isn't all that useful. Now I think that it isn't worth breaking Perl to introduce this, but it would have been nice if it was there from the start. Then people could written 'for ()' without slurping in the entire file, and the 'while ()' construct wouldn't have needed fixing around 5.004. [1] There are some constructs in Perl that behave like an iterator, in scalar context, //g in scalar context, for (EXPR .. EXPR) to name a view. Abigail
Re: Duh v D'oh
On Thu, Nov 06, 2008 at 05:00:16PM +, Nigel Rantor wrote: Paul Makepeace wrote: [...] If it's not staring out at you, you're possibly also a python programmer. Python here arguably DWIM better than perl. Does python give you the keys by default? Sort of. In Python, for loops over values returned by an iterator, and a hash implements that interface.
Re: Duh v D'oh
On Fri, Nov 7, 2008 at 12:45 PM, Eden Cardim [EMAIL PROTECTED] wrote: On Thu, Nov 6, 2008 at 1:33 PM, Paul Makepeace [EMAIL PROTECTED] wrote: Following on the theme from a conversation with Mark at Dim Sum today*, spot the mistake I just wasted an hour on, my $publication_stats = [['Source', 'ID Publication', 'Article count']]; foreach my $given_source (%publication_map_by_name) { push @$publication_stats, [$given_source, $publication_map_by_name{$given_source}, $article_count_by_source{$given_source}]; } If it's not staring out at you, you're possibly also a python programmer. Python here arguably DWIM better than perl. Paul * there's some mistakes where you at the end go D'oh! because perl is messing you around with its weirdness or otherwise non-intuitive behavior. Duh when you're messing around and just not paying attention. I wouldn't call that non-intuitive, it's a matter of culture. Perl coders are used to being verbose about whether they want keys/values from a hash. If someone changed the behavior to make a hash in list context return it's keys, you would confuse a lot of Perl programmers out there too. And I'm particularly fond of perl's behaviour because it allows interesting cookbook tricks such as %config = (%default, %user_provided) I don't think that's so exciting that this equivalent isn't acceptable in its stead, config = default config.update(user_provided) I think I prefer the slightly more explicit nature of this way , even. Would be interesting exercise to compare/contrast these perl idioms and their Language X equivalents and see where perl really shines, or not. P -- Eden Cardim Need help with your Catalyst or DBIx::Class project? Code Monkeyhttp://www.shadowcat.co.uk/catalyst/ Shadowcat Systems Ltd. Want a managed development or deployment platform? http://edenc.vox.com/http://www.shadowcat.co.uk/servers/
Re: Duh v D'oh
On Fri, Nov 7, 2008 at 2:08 PM, Paul Makepeace [EMAIL PROTECTED] wrote: I don't think that's so exciting that this equivalent isn't acceptable in its stead, config = default config.update(user_provided) Ditto :) I think I prefer the slightly more explicit nature of this way , even. Well, the way I see it, for k in my_dict is also implicit, and since you've manifested your preference of that construct over the explicit for keys %dict, in favour of readability, it is now clear that YMMV in regard to explicitness. Would be interesting exercise to compare/contrast these perl idioms and their Language X equivalents and see where perl really shines, or not. Yes, that'd be cool and instructive, since I haven't had the time to dig in to things like Python and Ruby. I'll confess that my vision of Perl practicity might be clouded by the previous C, C++ and Java experiences. -- Eden Cardim Need help with your Catalyst or DBIx::Class project? Code Monkeyhttp://www.shadowcat.co.uk/catalyst/ Shadowcat Systems Ltd. Want a managed development or deployment platform? http://edenc.vox.com/http://www.shadowcat.co.uk/servers/
Re: Duh v D'oh
Paul Makepeace wrote: On Fri, Nov 7, 2008 at 11:48 AM, Peter Corlett [EMAIL PROTECTED] wrote: On Thu, Nov 06, 2008 at 05:00:16PM +, Nigel Rantor wrote: Paul Makepeace wrote: [...] If it's not staring out at you, you're possibly also a python programmer. Python here arguably DWIM better than perl. Does python give you the keys by default? Sort of. In Python, for loops over values returned by an iterator, and a hash implements that interface. But by interface here we just mean it implements certain named methods that have certain semantics that everyone agrees make it an iterator...right? I mean, it's like C++ and templates, we duck type things rather than actually implement interfaces as you are required to do in Java. The interface is pretty clean, my_dict = {'a': 1, 'b': 2} for k in my_dict: print k gives, a b for k, v in my_dict.items(): print k, v gives, a 1 b 2 (ObPedantry, .iteritems() is preferred since it evaluates lazily with a generator.) Yes, well, Perl has 'each' for iterating over pairs, so they look almost identical. The main difference I can see is that python defaults to assuming you want the keys of a hash when iterating rather than the values. It's a fair enough design decision, but like any decision it's not always going to be the right thing. But yes, I agree, it is kind of DWIMier than iterating over both keys and values for a hash. If you look around though, lots of languages do this better than Perl these days. I was going to mention some languages but I can't be arsed with the inevitable flaming session. n p.s. I'm still reading the python list for a laugh. they just had a *HUGE* rant/discussion about what rebinding means and how you describe it to people coming from other languages.
Re: Duh v D'oh
Eden Cardim wrote: Yes, that'd be cool and instructive, since I haven't had the time to dig in to things like Python and Ruby. I'll confess that my vision of Perl practicity might be clouded by the previous C, C++ and Java experiences. It would be interesting if we had some basis for comparison. Paul, how's about you give us some things to accomplish and we tell you how we'd do them in different languages? I just think it might be unfair, or perhaps be perceived that way, if we chose our own examples and their other-language equivalents So, nothing too complex, it has to be do-able in less than a hundred or so lines of Perl. Or does that not make any sense? n
Duh v D'oh
Following on the theme from a conversation with Mark at Dim Sum today*, spot the mistake I just wasted an hour on, my $publication_stats = [['Source', 'ID Publication', 'Article count']]; foreach my $given_source (%publication_map_by_name) { push @$publication_stats, [$given_source, $publication_map_by_name{$given_source}, $article_count_by_source{$given_source}]; } If it's not staring out at you, you're possibly also a python programmer. Python here arguably DWIM better than perl. Paul * there's some mistakes where you at the end go D'oh! because perl is messing you around with its weirdness or otherwise non-intuitive behavior. Duh when you're messing around and just not paying attention.
Re: Duh v D'oh
Paul Makepeace wrote: Following on the theme from a conversation with Mark at Dim Sum today*, spot the mistake I just wasted an hour on, my $publication_stats = [['Source', 'ID Publication', 'Article count']]; foreach my $given_source (%publication_map_by_name) { push @$publication_stats, [$given_source, $publication_map_by_name{$given_source}, $article_count_by_source{$given_source}]; } You're setting $given_source alternately to be keys and values from the hash %publication_map_by_name. This probably won't end well. If it's not staring out at you, you're possibly also a python programmer. Python here arguably DWIM better than perl. Does python give you the keys by default? n
Re: Duh v D'oh
On Thu, November 6, 2008 9:00 am, Nigel Rantor wrote: Paul Makepeace wrote: Following on the theme from a conversation with Mark at Dim Sum today*, spot the mistake I just wasted an hour on, my $publication_stats = [['Source', 'ID Publication', 'Article count']]; foreach my $given_source (%publication_map_by_name) { push @$publication_stats, [$given_source, $publication_map_by_name{$given_source}, $article_count_by_source{$given_source}]; } You're setting $given_source alternately to be keys and values from the hash %publication_map_by_name. This probably won't end well. If it's not staring out at you, you're possibly also a python programmer. Python here arguably DWIM better than perl. Does python give you the keys by default? Pity we can't differentiate generic list context from iterator-source context. With the latter also having a pairlist flavor, I guess. So these would all work: %h = %h; # keys and values for (%h) # implicit keys for (pairlist %h) # no implicit keys It's a good thing perl 6 is coming; introducing this kind of thing one piecemeal into perl 5 could really screw it up.