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

A "boolify" operator. #301

Open
schwern opened this issue Nov 16, 2017 · 0 comments
Open

A "boolify" operator. #301

schwern opened this issue Nov 16, 2017 · 0 comments

Comments

@schwern
Copy link
Contributor

schwern commented Nov 16, 2017

Sometimes you want to return true or false, not a value which happens to be true or false. Usually this is to avoid the value being used for some undocumented purpose, or to avoid leaking private data which should not be available. Often the !! secret operator is used to "boolify" an expression.

For example...

# Accidentally returns the count of things.
sub has_things {
    return scalar @things;
}

# Returns true or false.
sub has_things {
    return !!@things;
}

Rather than use a secret operator, we could provide something like boolify to make the meaning more plain.

# Force it into scalar context so `boolify @things` works on the size of @things, not $things[0]
sub boolify($) { return !!$_[0] }

sub has_things {
    return boolify @things;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant