Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Using s/// Inline

by Jason Hutchinson (Acolyte)
on Nov 16, 2009 at 16:04 UTC ( [id://807498]=perlquestion: print w/replies, xml ) Need Help??

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

    ($key=~m{^\d+$} ? "fid" : "name") => ($key=~m{^\d+$} ? $key : lc($key =~ s/[^a-z0-9]/_/ig))

    Do you think that's readable? And you want to add to it? Whoa!

    sub add_record { my ($self, $data) = @_; my @record; for my $key (keys %$data) { my $att_key = $key =~ /[^0-9]/ ? "name" : "fid"; ( my $att_val = lc($key) ) =~ s/[^a-z0-9]/_/g; push @record, { tag => "field", atts => { $att_key => $att_val }, value => $data->{$key}, }; } return post_api("API_AddRecord", \@record); }

    By the way, don't use prototypes! You didn't even use the right one, so that means you have to bypass it to call your sub!

      Hi Ikegami,

      I was looking at the search result of prototypes in perlmonk and found that the general wisdom is not to use prototypes. But is there a good explanation as to why it should not be used?

      I looked at Prototypes and it looks like prototypes are useful if you want to control the arguments coming into your subroutine (this might be wrong, but this is just how I understood).

        Simply put, they change the parsing rules (e.g. the evaluation context of the arguments). Did you mean to change the parsing rules? No, you probably wanted some form of argument validation. Therefore, you shouldn't be using prototypes.

        Besides, you were placing them on methods where they are totally ignored (to the point that you were using the wrong one and you didn't notice).

      Many thanks, ikegami. The modified version in your reply is much more readable and functions exactly as I need it to.

      I have also taken your advice against prototypes and removed them from the SDK. I have to admit that I didn't see any benefit to them, and was only keeping them in the module because the original creator had them there.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://807498]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2024-04-19 09:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found