Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Will Perl 6 optimize away useless assignments?

by princepawn (Parson)
on Sep 03, 2001 at 20:26 UTC ( [id://109889]=perlquestion: print w/replies, xml ) Need Help??

princepawn has asked for the wisdom of the Perl Monks concerning the following question:

Let's say I have a naive code generator which generated this:
{ my $name = <STDIN>; print $name; }
This involves a useless assignment and could be optimized to :
{ print <STDIN>; }
because the my variable was not offered life outside this scope.

Replies are listed 'Best First'.
Re (tilly) 1: Will Perl 6 optimize away useless assignments?
by tilly (Archbishop) on Sep 03, 2001 at 23:31 UTC
    Modulo the obvious mistake, the answer is probably, "It will do both."

    Perl 6 aims to have multiple parsing front ends and multiple working back ends. The default back end that you get with perl will work as the current one does, interactively, and so cannot afford to be overly aggressive in optimization because compilation time is seen as part of run-time. However there are planned back-ends which will be able to take the time to optimize more aggressively.

Re: Will Perl 6 optimize away useless assignments?
by chromatic (Archbishop) on Sep 03, 2001 at 22:23 UTC
    It won't optimize this way, because your example is wrong. Did you test it? The first snippit reads and prints one line. The second prints all lines.

    print takes a list.

Re: Will Perl 6 optimize away useless assignments?
by stefp (Vicar) on Sep 03, 2001 at 20:48 UTC
    This is not such much a comment about the possible perl6 optimizations, but on the meaning of the perl6 code you gave.

    My understanding of perl6 is that it <H> gobbles as much and no more then the assignement context warrants.
    ($a, $b) = <STDIN>
    eats two lines of std input. But I suppose that
    print <STDIN>
    eats all of STDIN. So you example would be optimized to the perl5 equivalent of
    print scalar  <STDIN>;

    Please correct me if I am wrong.

Re: Will Perl 6 optimize away useless assignments?
by RhetTbull (Curate) on Sep 03, 2001 at 21:01 UTC
    A problem I see with this is that it's hard for the compiler to know what's a "useless" assignment -- especially with a language as extensible as perl. For example, what if the variable is tied? An assignment that looks useless may do quite a bit. Of course, you could tell the compiler "optimize it away unless it's tied" but there may be other examples where it's hard/impossible to tell. Although I doubt anyone uses perl for embedded work, this sort of thing is definitely an issue for C where a variable might really be a hardware register so an assignment isn't useless at all and is probably necessary. Can anyone think of a perl example other than a tied variable?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://109889]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-25 20:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found