or if there were a lex_local that could just be dropped in for lexicals as local can for globals.

First of all local is the wrong approach, because local just manipulates already declared pack-vars, so under strict you would be forced to predeclare your variables with "our" (or "my") in the outer scope. (look at first our in the following code) (see update)

I would love to have a lexical local in P5 BUT not with the extra magic of declaring missing variables your assuming.

What you really want is aliasing like $_[ ] variables do.

I know about *, although it doesn't seem to do much for lexicals, but what do you mean by pack-vars?

Aliasing with package-variables

use strict; use warnings; $\="\n"; my $lamb = sub { our ($d,$e); # local doesn't declare!!! local (*d,*e)=\(@_); $d += $e ; }; our $d="D"; our $u=10; $lamb->($u,5); print $u; #> 15 print $d; #> D

However, I don't know how to improve.

from your /msg:

What can I do to improve? I try to phrase my questions as (1) an informal description of what I want; (2) imaginary code that'll do what I want; (3) descriptions of what I've tried, and what I don't want to do (ideally, with explanations).

some ideas:

  1. Post working code which exactly does what you want, but in a "unpretty" way. e.g. with
    • $_[0] or
    • lex-refs $$x or
    • aliased pack-vars *x ...
  2. Express what you want to avoid in the working example (uglyness, doubelsigils, pack-vars,...)

  3. Post a test program which proofs every functionality you want to have covered, like
    • aliasing,
    • locality..
  4. e.g. with Test:More like in this post

  5. Write a codegenerator using eval which covers all edge cases e.g.
    • with or without use strict;,
    • with or without use warnings;,
    • variables known/unknown in outer scope,
    • outerscope vars are lex or pack
  6. Make clear which moduls are acceptable to be used...
    • None
    • Just CORE
    • CPAN w/o XS
    • CPAN
    • any

Cheers Rolf

UPDATE: as JavaFan pointed out here you are not forced to use our with full-qualified varnames, so this works

my $lamb = sub { # our ($d,$e); local (*::d,*::e)=\(@_); $::d += $::e ; };
but I don't know if this meets your criteria of being more elegant than using $_[0] += $_[1] and furthermore it's only a pack-var thing, in P5 lex-vars have no namespaces...

In reply to Re^5: Local for lexicals by LanX
in thread Local for lexicals by JadeNB

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.