Re: variable name
by ikegami (Patriarch) on Jul 22, 2006 at 11:39 UTC
|
| [reply] [d/l] |
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.
| [reply] |
|
| [reply] |
|
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.
| [reply] |
|
"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.
| [reply] |
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'?
| [reply] [d/l] [select] |
|
var_name_for_this = :someSymbol
print var_name_for_this.id2name
this is from Ruby world
| [reply] [d/l] |
|
$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 =>
| [reply] [d/l] [select] |
|
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. | [reply] [d/l] |
|
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.
| [reply] [d/l] [select] |
Re: variable name
by sh1tn (Priest) on Jan 06, 2008 at 06:20 UTC
|
# 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};
| [reply] [d/l] |