develooper Front page | perl.perl5.porters | Postings from March 2007

lexical subs

Thread Next
From:
H.Merijn Brand
Date:
March 8, 2007 23:45
Subject:
lexical subs
Message ID:
20070309084515.4c46df41@pc09
Now that we have lexical $_, why not lexical subs

if ($some_expression) {
    sub complex_calc {
        # Some very complex calculus for this lexical scope only
        }
    my $foo = complex_calc (.....) + complex_calc (.....);
    my $bar = $foo * complex_calc (.....);
    # etc
    }
if ($some_similar_expr) {
    sub complex_calc {
        # Some very complex calculus for this lexical scope only
        }
    my $foo = complex_calc (.....) + complex_calc (.....);
    my $bar = $foo * complex_calc (.....);
    # etc
    }

And guess what happens without a warning
Same if complex_calc would also have been defined outside of
the scope(s). It would be so handy and useful to be able to

if (...) {
    my sub handy {
        # ...
        }
    # ...
    }
if (...) {
    my sub handy {
        # ...
        }
    # ...
    }

and now have *two* functions handy () that are invisible
outside of the lexical scope.

This would also be an ideal solution to hide private
functions and methods inside modules.

feasible? useful? doable?

-- 
H.Merijn Brand         Amsterdam Perl Mongers (http://amsterdam.pm.org/)
using & porting perl 5.6.2, 5.8.x, 5.9.x   on HP-UX 10.20, 11.00, 11.11,
& 11.23, SuSE 10.0 & 10.2, AIX 4.3 & 5.2, and Cygwin. http://qa.perl.org
http://mirrors.develooper.com/hpux/            http://www.test-smoke.org
                        http://www.goldmark.org/jeff/stupid-disclaimers/

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About