|
Prev: Why does Net::SFTP trigger my die handler when no errors?
Next: FAQ 4.7 How do I multiply matrices?
From: Dr.Ruud on 31 Jan 2006 18:46 Abigail: > Dr.Ruud: >> $ echo 'if( exists $hash{$key} ) { print "$hash($key)\n"; }' \ >> | perl -MO=Deparse,-x7 >> >> exists $hash{$key} and do { >> print "$hash($key)\n" >> }; >> >> I don't know why the do-block is needed. > > Because the then part of an if statement is a block. 'do {}' turns a > block into an expression. I still don't see why it isn't 'cleaned up' further in the special case that the expression is one statement. As you can deduce, I assume that the "do{}" is never necessary around a single statement, that "()" is sufficient. I don't expect "()" for "do{}" to speed up anything, I just wondered a bit why that step isn't taken. -- Affijn, Ruud "Gewoon is een tijger."
From: Anno Siegel on 31 Jan 2006 19:26 Dr.Ruud <rvtol+news(a)isolution.nl> wrote in comp.lang.perl.misc: > Abigail: > > Dr.Ruud: > > >> $ echo 'if( exists $hash{$key} ) { print "$hash($key)\n"; }' \ > >> | perl -MO=Deparse,-x7 > >> > >> exists $hash{$key} and do { > >> print "$hash($key)\n" > >> }; > >> > >> I don't know why the do-block is needed. > > > > Because the then part of an if statement is a block. 'do {}' turns a > > block into an expression. > > I still don't see why it isn't 'cleaned up' further in the special case > that the expression is one statement. As you can deduce, I assume that > the "do{}" is never necessary around a single statement, that "()" is > sufficient. No, the "do{}"s are necessary, there *must* be a statement in this place. It's just that the DWIM mechanism inserts them for you in many situations where little else would make sense. What deparse shows is what perl actually compiles. > I don't expect "()" for "do{}" to speed up anything, I just wondered a > bit why that step isn't taken. I don't see who could take that step. The Perl interpreter needs the "do{}"s, it cant discard them. Deparsing can't very well suppress them either, once they're there. Anno -- If you want to post a followup via groups.google.com, don't use the broken "Reply" link at the bottom of the article. Click on "show options" at the top of the article, then click on the "Reply" at the bottom of the article headers.
From: Dr.Ruud on 31 Jan 2006 20:27 Anno Siegel schreef: > Dr.Ruud: >> Abigail: >>> Dr.Ruud: >>>> $ echo 'if( exists $hash{$key} ) { print "$hash($key)\n"; }' \ >>>> | perl -MO=Deparse,-x7 >>>> >>>> exists $hash{$key} and do { >>>> print "$hash($key)\n" >>>> }; >>>> >>>> I don't know why the do-block is needed. >>> >>> Because the then part of an if statement is a block. 'do {}' turns a >>> block into an expression. >> >> I still don't see why it isn't 'cleaned up' further in the special >> case that the expression is one statement. As you can deduce, I >> assume that the "do{}" is never necessary around a single statement, >> that "()" is sufficient. > > No, the "do{}"s are necessary, there *must* be a statement in this > place. It's just that the DWIM mechanism inserts them for you in many > situations where little else would make sense. What deparse shows is > what perl actually compiles. From an earlier post: $ echo 'print "$hash{$key}\n" if exists $hash{$key};' \ | perl -MO=Deparse,-x7 exists $hash{$key} and print "$hash{$key}\n"; That doesn't get a do{}. > Deparse can't very well suppress them either, once they're there. And I just wondered why not, for cases like this one. -- Affijn, Ruud "Gewoon is een tijger."
From: Abigail on 1 Feb 2006 01:09 Dr.Ruud (rvtol+news(a)isolution.nl) wrote on MMMMDXXXVII September MCMXCIII in <URL:news:drp6r0.js.1(a)news.isolution.nl>: || Anno Siegel schreef: || > Dr.Ruud: || >> Abigail: || >>> Dr.Ruud: || || >>>> $ echo 'if( exists $hash{$key} ) { print "$hash($key)\n"; }' \ || >>>> | perl -MO=Deparse,-x7 || >>>> || >>>> exists $hash{$key} and do { || >>>> print "$hash($key)\n" || >>>> }; || >>>> || >>>> I don't know why the do-block is needed. || >>> || >>> Because the then part of an if statement is a block. 'do {}' turns a || >>> block into an expression. || >> || >> I still don't see why it isn't 'cleaned up' further in the special || >> case that the expression is one statement. As you can deduce, I || >> assume that the "do{}" is never necessary around a single statement, || >> that "()" is sufficient. || > || > No, the "do{}"s are necessary, there *must* be a statement in this || > place. It's just that the DWIM mechanism inserts them for you in many || > situations where little else would make sense. What deparse shows is || > what perl actually compiles. || || From an earlier post: || || $ echo 'print "$hash{$key}\n" if exists $hash{$key};' \ || | perl -MO=Deparse,-x7 || || exists $hash{$key} and print "$hash{$key}\n"; || || That doesn't get a do{}. || || || > Deparse can't very well suppress them either, once they're there. || || And I just wondered why not, for cases like this one. $ perl -le 'my $x = "foo"; if (1) {my $x = "bar"} print $x' foo $ perl -le 'my $x = "foo"; my $x = "bar" if 1; print $x' bar $ perl -le 'my $x = "foo"; 1 and do {my $x = "bar"}; print $x' foo $ perl -le 'my $x = "foo"; 1 and my $x = "bar"; print $x' bar Abigail -- BEGIN {$^H {join "" => ("a" .. "z") [8, 13, 19, 4, 6, 4, 17]} = sub {["", "Just ", "another ", "Perl ", "Hacker"] -> [shift]}; $^H = hex join "" => reverse map {int ($_ / 2)} 0 .. 4} print 1, 2, 3, 4, "\n";
From: Dr.Ruud on 1 Feb 2006 13:57 Abigail schreef: > [why Deparse keeps {} for a single statement] > > $ perl -le 'my $x = "foo"; if (1) {my $x = "bar"} print $x' > foo > > $ perl -le 'my $x = "foo"; my $x = "bar" if 1; print $x' > bar > > $ perl -le 'my $x = "foo"; 1 and do {my $x = "bar"}; print $x' > foo > > $ perl -le 'my $x = "foo"; 1 and my $x = "bar"; print $x' > bar Thanks for the examples. Yes, lexical dependencies should make the {} stay. I benchmarked "if (1) {print}" against "print if 1", and the speed of the first was less, but less than 5% so. -- Affijn, Ruud "Gewoon is een tijger."
First
|
Prev
|
Pages: 1 2 3 4 Prev: Why does Net::SFTP trigger my die handler when no errors? Next: FAQ 4.7 How do I multiply matrices? |