V
valued customer
### QUESTION
You have a complex perl data structure that you want to access using a
flexible 'xpath-like' syntax supplied in a simple string. How can this
be done elegantly??
### EXAMPLE
Suppose you have a perl data structure that looks something like this
....
$news = {};
$news->{top_stories} = ##* ... code-omitted ...*;
$news->{sports} = ##* ... code-omitted ...*;
$news->{weather} = ##* ... code-omitted ...*
You can infer what the omitted parts look like from the following
sample data nodes ...
### get stuff about todays top story
print $news->{top_stories}[0]{headline};
print $news->{top_stories}[0]{bodytext};
### get stuff about the weather
print $news->{weather}{forecast}[3]{temp}; ## forecast day 3
print $news->{weather}{current}{temp};
### PROBLEM DETAILS
How do you allow a user to arbitrarily access any single data node of
any arbitrary depth in the data structure, simply by specifying a
single query string?
eg; 'news/top_stories/[0]/headline'
eg; 'news/weather/current/temp'
eg; 'news/weather/forecast/[2]/temp'
eg; 'news/classifieds/for_sale/vehicles/trucks/[12]/asking_price'
Is there a more elegant solution than splitting the string,
concatenating
and then doing 'eval'? Dereferencing with '${}' does not seem flexible
enough because
the application does not know in advance how many 'path steps' the
user will
supply in the query string. Is there an XPath-like syntax module
available, or an alternative approach?
You have a complex perl data structure that you want to access using a
flexible 'xpath-like' syntax supplied in a simple string. How can this
be done elegantly??
### EXAMPLE
Suppose you have a perl data structure that looks something like this
....
$news = {};
$news->{top_stories} = ##* ... code-omitted ...*;
$news->{sports} = ##* ... code-omitted ...*;
$news->{weather} = ##* ... code-omitted ...*
You can infer what the omitted parts look like from the following
sample data nodes ...
### get stuff about todays top story
print $news->{top_stories}[0]{headline};
print $news->{top_stories}[0]{bodytext};
### get stuff about the weather
print $news->{weather}{forecast}[3]{temp}; ## forecast day 3
print $news->{weather}{current}{temp};
### PROBLEM DETAILS
How do you allow a user to arbitrarily access any single data node of
any arbitrary depth in the data structure, simply by specifying a
single query string?
eg; 'news/top_stories/[0]/headline'
eg; 'news/weather/current/temp'
eg; 'news/weather/forecast/[2]/temp'
eg; 'news/classifieds/for_sale/vehicles/trucks/[12]/asking_price'
Is there a more elegant solution than splitting the string,
concatenating
and then doing 'eval'? Dereferencing with '${}' does not seem flexible
enough because
the application does not know in advance how many 'path steps' the
user will
supply in the query string. Is there an XPath-like syntax module
available, or an alternative approach?