Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

variable name

by sh1tn (Priest)
on Jul 22, 2006 at 10:54 UTC ( #562997=perlquestion: print w/ replies, xml ) Need Help??
sh1tn has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

Is it possible the name of a variable to be reached?
my $var_for_this = 'sm.th.'; # can I take 'var_for_this' somehow?
update: I can do this in Ruby, why not in Perl ?


Comment on variable name
Download Code
Re: variable name
by ikegami (Pope) on Jul 22, 2006 at 11:39 UTC
Re: variable name
by Leviathan (Scribe) on Jul 22, 2006 at 12:52 UTC

    I'm not sure why you want to use that, but there is a way to find the variable names, and that's by checking the symbol table (hash %::) but it contains symbols you don't want... here's an example to filter them out:

    $bleh = 1; $var = 3; print join "\n", grep { ! ( /^\*/ || /(::|\W)/ ) } grep { ! /(std(err|out|in)|ARGV|INC|ENV|0|STD(ERR|OUT|IN)|_)/ } keys %::;

    So now you have all the variable names in the script (in package main here) but finding out which variable is which it tricky.

    --
    Leviathan.
      That won't work with lexical (my) variables as in the OP's example: these don't appear in the symbol table: see what happens if you change your $bleh to  my $bleh.
Re: variable name
by japhy (Canon) on Jul 22, 2006 at 13:43 UTC
    The bigger question is not "how can I do this?" or "why can't I do this in Perl if I can do it in Ruby?" -- the question is, "why do I need to do it?" Answer that first.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

      Yup — sounds like an XY Problem.

      We're building the house of the future together.
      The bigger question is not to make policy when some technology cannot do sm.th.. If you can(or cannot) do sm.th. just let me know, without "why so and so ..." propaganda.


        "Propaganda"? I really don't know what you're trying to accomplish. You want to know if you can get the name of a lexical FROM the lexical. While it IS possible, it's a very rare task, and one which begs the question "why". Perhaps you're doing something that you should just be using a hash for. And, so what if Ruby can do it? What benefit is this ability going to get you? Yes, it's possible, but no, you probably shouldn't use it.

        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: variable name
by Joost (Canon) on Jul 22, 2006 at 15:17 UTC
    What do you mean by "take 'var_for_this'" ?

    Update: feel free to post the equivalent ruby code if you can't explain in english

    The name of the variable you're using is normally known to you, since that's what you're writing. i.e. why would you want to do $name = name_of_var($var_for_this) when you can also write $name = '$name_of_var'?

      var_name_for_this = :someSymbol print var_name_for_this.id2name
      this is from Ruby world


        sorry, but symbols have nothing to do with variables; they're just "fast strings".

        Perl doesnt have symbols (or at least, nothing like ruby's symbols), so the equivalent in perl would be something like

        $var_name_for_this = 'someSymbol';
        Note that there is no 'someSymbol' variable anywhere, but then, there isn't one in your ruby code either.

        update: to be clear, ruby's symbols (the :something construct) are NOT variables, they're values. For most purposes, ruby symbols act like strings with the advantage that you don't have to fully quote them - just a ":" at the beginning is enough:

        pair = { :name, value } # name quoted with :
        Note that perl has something similar when using the => 'quoting comma':
        $pair = { name => $value }; # name quoted with =>
Re: variable name
by sh1tn (Priest) on Jan 06, 2008 at 06:20 UTC
    I got the asnwer from brian_d_foy's Mastering Perl book.
    # my mistake not to define the type of the variable # not lexical, but in main package $main::var_for_this = q{}; print *var_for_this{NAME};


Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2014-08-23 09:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (173 votes), past polls