dash2 has asked for the wisdom of the Perl Monks concerning the following question:
sub string_to_struct { my $string = shift; my @refs = split /__/, $string; $string = "\$$refs[0]"; for (1 .. $#refs) { $string .= "-> {'$refs[$_]'}" }; return eval "\\$string"; # a ref, for reasons to do with not failing +if eval "\$string" would be an array, hash, hashref etc. }
This turns e.g"FOO__bar__baz_bop" into $FOO->{bar}{baz_bop}
Leaving aside the merits of this WTDI, (but feel free to comment if you wish ;-) I am wondering if there is a simple way to do the reverse - a "struct_to_string" function? My initial thought is, not really. If you pass a value to a subroutine, you just get the value. Even if it's a reference, you just get a memory address. So that's no good if you want to specify the string itself. Is there any way to access a variable's name as a string?
David
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: stringification
by chromatic (Archbishop) on Feb 27, 2001 at 22:17 UTC | |
|
(jeffa) Re: stringification
by jeffa (Bishop) on Feb 27, 2001 at 22:12 UTC | |
|
Re: stringification
by davorg (Chancellor) on Feb 27, 2001 at 22:12 UTC | |
|
Re: stringification
by dash2 (Hermit) on Feb 27, 2001 at 22:33 UTC | |
by chromatic (Archbishop) on Feb 27, 2001 at 22:42 UTC | |
by merlyn (Sage) on Feb 27, 2001 at 22:56 UTC | |
by dash2 (Hermit) on Feb 28, 2001 at 17:25 UTC | |
by chipmunk (Parson) on Feb 28, 2001 at 20:28 UTC | |
by dash2 (Hermit) on Mar 01, 2001 at 14:21 UTC | |
by deprecated (Priest) on Feb 28, 2001 at 17:48 UTC | |
|
Re: stringification
by merlyn (Sage) on Mar 10, 2001 at 23:18 UTC | |
|
Re: stringification
by dash2 (Hermit) on Mar 01, 2001 at 16:45 UTC |