Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ResultSet::rs to force scalar context #68

Closed

Conversation

andrewgregory
Copy link

rs is a simple helper method that does nothing but return the original
ResultSet in order to force scalar context for search methods. This is
primarily intended for predefined queries in subclasses to allow them to
take advantage of search's context-aware return values without requiring
them to either manually force scalar context or define a second XXX_rs
search method to get a resultset object in list context.

rs is a simple helper method that does nothing but return the original
ResultSet in order to force scalar context for search methods.  This is
primarily intended for predefined queries in subclasses to allow them to
take advantage of search's context-aware return values without requiring
them to either manually force scalar context or define a second XXX_rs
search method to get a resultset object in list context.
@abraxxa
Copy link
Contributor

abraxxa commented Nov 18, 2014

Why not use the existing search_rs method?

@andrewgregory
Copy link
Author

The goal is to make it easier for users to create predefined searches in subclasses without having to create them in pairs. For instance, instead of creating both search_foo and search_foo_rs methods they would only need to create search_foo and call it as search_foo()->rs when they need to force it to return a resultset. This could replace search_rs as well, but that is not my goal.

@abraxxa
Copy link
Contributor

abraxxa commented Nov 19, 2014

It seems the documentation doesn't make it clear that you can call search_rs without any parameters on a resultset just to get a copy of it, doc patch welcome.

A predefined search usually means a resultset method that returns another resultset of the same or a related class with some filtering applied. If you use search inside those you will get the context sensitivity, to force scalar context you can either use 'scalar' in front of the predefined search call (better performance) or add '->search_rs' to it just like you proposed with '->rs' which will create a copy of the just created resultset (worse performance).

@andrewgregory
Copy link
Author

Sure. This is just meant to be a little more convenient, in the same way $rs->search_rs is more convenient than having to call scalar $rs->search(). I find it more readable than using scalar or search_rs to force scalar context and, as you said, using search_rs involves a performance penalty.

@ribasushi
Copy link
Collaborator

Sorry for the late response.

Thank you for the contribution, but as @abraxxa already noted earlier - this feature is too trivil to be carried as part of the core distribution. I am afraid I will have to reject this patch.

Nevertheless I am encouraging you to consider uploading a DBIC component to CPAN which provides the above functionality. This way you (and possibly others) can use the syntax you find intuitive, and the core API is not saddled with yet another way to do things.

Cheers!

@ribasushi ribasushi closed this Dec 20, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants