CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
what can I do with an "accessor string" ?

 



perlFun
Novice

Aug 28, 2013, 2:22 PM

Post #1 of 4 (417 views)
what can I do with an "accessor string" ? Can't Post

I am using a library which searches through a complicated hash for a particular key/value pair, and what it returns is a string which is a "mapping" to where that key value pair is.

So for example, I will get a string called, let's say, $result

The value of $result will be:

"->[0]->{'hashKey1'}->[3]"

So it's a string that looks like "perl code" itself.

So now I have my hashref that I did the search on, let's call it, $myHash, and now I "know" exactly where my desired key/value pair is at. How do I use this accessor string to get it?

I mean - I realize I could parse this string, get the index numbers for the inner arrayrefs, hashrefs, etc., but I Just didn't know if perhaps perl had some functionality to use this string as the perl code itself. If I go, $myHash$result well this doesn't work. Just didn't know if it was in fact possible to do this, just obviously I'm doing it the wrong way


FishMonger
Veteran / Moderator

Aug 28, 2013, 2:50 PM

Post #2 of 4 (415 views)
Re: [perlFun] what can I do with an "accessor string" ? [In reply to] Can't Post

You should rethink your approach. What you're doing is very poor design.

To silve the poblem you're asking about you need to use the eval function.

See: perldoc -f eval
http://perldoc.perl.org/functions/eval.html


perlFun
Novice

Aug 28, 2013, 2:55 PM

Post #3 of 4 (413 views)
Re: [FishMonger] what can I do with an "accessor string" ? [In reply to] Can't Post

Unfortunately I haven't designed this library, this is all I have to work with. I am using my company's internal libraries. They give me tons of complicated hashes, and I use their search function, and the result of their search function is that string I mentioned. I don't have any other option because I can't make changes to how their search works.

EDIT: unfortunately they also dislike us to use eval blocks. I guess they can't have their cake and eat it to if they want us to use these weird functions and then not use eval!!


(This post was edited by perlFun on Aug 28, 2013, 3:00 PM)


Zhris
User

Aug 28, 2013, 4:18 PM

Post #4 of 4 (403 views)
Re: [perlFun] what can I do with an "accessor string" ? [In reply to] Can't Post

Hi,

With eval:

Code
#!/usr/bin/perl 
use strict;
use warnings;
use Data::Dumper;

my $ref = [ { hashKey1 => [ qw( one two three four five ) ] } ];
my $accessor = "->[0]->{hashKey1}->[3]";

$ref = eval '$ref' . $accessor;

print Dumper( $ref );


Without eval:

Code
#!/usr/bin/perl 
use strict;
use warnings;
use Data::Dumper;

my $ref = [ { hashKey1 => [ qw( one two three four five ) ] } ];
my $accessor = "->[0]->{hashKey1}->[3]";

foreach ( grep { /\[|{/ } split /->/, $accessor )
{
$ref = ( /\[(\d+)\]/ ) ? $ref->[$1] :
( /\{'?(.+?)'?\}/ ) ? $ref->{$1} :
die ;
}


print Dumper( $ref );


Chris


(This post was edited by Zhris on Aug 28, 2013, 4:21 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives