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"})In reply to Using s/// Inline by Jason Hutchinson
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |