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: Re: [perlFun] what can I do with an "accessor string" ?: Edit Log



Zhris
Enthusiast

Aug 28, 2013, 4:18 PM


Views: 2620
Re: [perlFun] what can I do with an "accessor string" ?

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)


Edit Log:
Post edited by Zhris (Enthusiast) 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