Jason Hutchinson has asked for the wisdom of the Perl Monks concerning the following question:
I'm currently doing work on a Perl SDK for an HTTP API, and have hit a bit of a stumbling block with s///.
The API takes XML for input, and when adding or updating fields it will accept either the numeric field identifier or a formatted version of the field label/field name. For the formatting, the field name would be "Field Name" and the reformatted version would be "field_name".
Because it's an API I'd like to do that reformatting transparently, so the end user just has to type in the field name they want to use. I'm using the s/// regexp to do the formatting, but I'm in a situation where I'd like to use it inline but it only returns the number of substitutions performed. Is there any way to use the s/// inline and return the substitution's results?
Here's the code I'm using:
sub add_record($){ my ($self, $data) = @_; my $record=[]; foreach my $key (keys %{$data}){ push(@{$record}, { tag=>"field", atts=>{ ($key=~m{^\d+$} ? "fid" : "name") => ($key=~m{^\d+$} ? + $key : lc($key =~ s/[^a-z0-9]/_/ig)) }, value=>$data->{$key} }); } return post_api("API_AddRecord", $record); }
And here's how it would be used:
add_record({"Field Name"=>"Field Value"})
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using s/// Inline
by ikegami (Patriarch) on Nov 16, 2009 at 16:20 UTC | |
by bichonfrise74 (Vicar) on Nov 16, 2009 at 19:39 UTC | |
by ikegami (Patriarch) on Nov 16, 2009 at 20:00 UTC | |
by Jason Hutchinson (Acolyte) on Nov 16, 2009 at 21:09 UTC | |
by ikegami (Patriarch) on Nov 16, 2009 at 21:27 UTC | |
by bobr (Monk) on Nov 17, 2009 at 13:47 UTC | |
by Jason Hutchinson (Acolyte) on Nov 16, 2009 at 16:41 UTC |