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 ""= #11

Open
schwern opened this issue Feb 20, 2015 · 2 comments
Open

Add ""= #11

schwern opened this issue Feb 20, 2015 · 2 comments

Comments

@schwern
Copy link
Contributor

schwern commented Feb 20, 2015

Similar to #10, it has the same rationale but I'm doing this in a separate issue since it requires making up new syntax.

The third common reason to apply a default is for an empty string. ||= can't be used because "0" would be valid. ""= would be equivalent to...

$arg = "default" if !defined($arg) or !length($arg);
@mauke
Copy link
Owner

mauke commented Feb 20, 2015

What's your use case for this?

@schwern
Copy link
Contributor Author

schwern commented Feb 21, 2015

//=, ||= and ""= are all for the cases where the caller has passed in something but it's trash, something that happens more often than we'd like in Perl. As the writer of the subroutine, I don't have control over how it's called. undef will pass the required parameter check, but the value is useless. It's better to apply a useful default than proceed with undef. //= is for when a false value is ok. ||= is for when only true values are ok. ""= is for when only non-empty strings are ok.

One could argue that instead the bogus values should instead be rejected by a type constraint. While I like type constraints, I don't think they're ready to be applied universally, and Function::Parameters is a universally useful module. Types aren't well accepted in Perl, they're slow, and writing custom types (such as "non empty string") is a PITA. It's worthwhile to support good, defensive styles that don't use types.

Here's an example which ensures the test always has a name. It's a thing that will be displayed, so it should be non-empty. //= is insufficient as it allows an empty string. ||= is insufficient as it disallows 0.

func is($have, $want, $name ""= qq['$have' eq '$want'] ) {
    return ok($have eq $want, $name);
}

Maybe you're building a command out of the arguments and want to make sure every piece is filled in. Passing in a non-blank string might allow a security problem or for a piece to be misinterpreted.

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

2 participants