From: Dr.Ruud on
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
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
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
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
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."