update: demoronified the code in one place
sub string_to_struct { my $string = shift; my ($var, $keys) = split /-/, $string, 2; $keys = "-$keys"; # you could use a lookahead in the split, but in my + experience they break on older Perl 5s. return 0 unless ( $var eq "safe" or $var eq "alsosafe" ); $change{$var}++; # global no strict refs; my $ref = $$var; # reference to the original. can alter the original use strict refs; return deref($ref, $keys); } sub deref { return \$_[0] unless $_[1] =~ s/^-([\w_]+)//; deref ($_[0]->{$1}, $_[1]); }
This should take e.g. safe-foo-bar and return a reference to $safe->{foo}{bar}. Comments are welcome - it's the result of a lot of debugging, so probably not the most elegant in the world!
I also asked about the reverse process. I'm still not sure there's a way to get from a particular hash key to a string representing it, i.e. from $safe->{foo}{bar} to safe-foo-bar. At least not if you use a subroutine. The subroutine just gets the value of $safe->{foo}{bar}. It gets it by reference, but it still doesn't know where the reference "is". I suppose you could pass it like this:
and do something with the list of values, but by that time, you may as well type it yourself...struct_to_string($safe,$safe->{foo},$safe->{foo}{bar});
Thanks for all the help.
dave hj~
In reply to Re: stringification
by dash2
in thread stringification
by dash2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |