The answer then was "impossible." Still the same now?

Yes.

Of course, now someone is going to come along with some deep mojo using B, PadWalker, some C code, and a system call to perl -d just to prove me wrong. Worse yet, they'll say "just use the Acme::Sub::Introspect::Argument::Names module; the get_names_of_passed_variables() function will work like a charm."

Seriously, what about calls with a literal like foo("bar") or calls with the return value of another sub like foo( bar() ) or calls with an element in a hash or an array? It doesn't seem like an easy problem.

I'm really curious about why you might want this. It would almost certainly be useless for anything but introspection for debugging or obfuscation and perl already has plenty of support for both of those without this capability.

Update: See. I just knew it. Devel::Caller, as PodMaster points out, will work as long as your calls don't get much harder than your example. It isn't a general solution. Some quick tests show it handles references poorly (it always returns '$bar' for foo($bar), foo(\$bar), foo(\\$bar), etc.) It doesn't seem to handle symbolic references at all. It also doesn't know about elements of an array or hash. For instance, foo($bar[0]) returns '@bar' and foo($bar{baz}) return '%bar'. Literals return undef.

-sauoq
"My two cents aren't worth a dime.";

In reply to Re: Get name of var passed to sub by sauoq
in thread Get name of var passed to sub by chunlou

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.