exercism-perl5

Repository for my Perl 5 Exercism exercises
git clone git://git.samirparikh.com/exercism-perl5
Log | Files | Refs | README

commit 471c9144c0395fc09a48fe7427ea29313db60ab5
parent cd4add2cfa09915a1361eee27241a50b07239347
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Thu,  6 Jan 2022 00:49:32 +0000

solve all tests

Diffstat:
Atwo-fer/HELP.md | 36++++++++++++++++++++++++++++++++++++
Atwo-fer/README.md | 46++++++++++++++++++++++++++++++++++++++++++++++
Atwo-fer/TwoFer.pm | 23+++++++++++++++++++++++
Atwo-fer/two-fer.t | 49+++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 154 insertions(+), 0 deletions(-)

diff --git a/two-fer/HELP.md b/two-fer/HELP.md @@ -0,0 +1,35 @@ +# Help + +## Running the tests + +There is a Perl 5 script with the extension `.t`, which will be used to test +your solution. You can run through the tests by using the command: + +```bash +`prove .` +``` + +## Submitting your solution + +You can submit your solution using the `exercism submit TwoFer.pm` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Perl 5 track's documentation](https://exercism.org/docs/tracks/perl5) +- [Exercism's support channel on gitter](https://gitter.im/exercism/support) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +To get help if you're having trouble, you can use one of the following resources: + +- [/r/perl5](https://www.reddit.com/r/perl5) is the perl5 subreddit. +- [StackOverflow](http://stackoverflow.com/questions/tagged/perl5) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions. +\ No newline at end of file diff --git a/two-fer/README.md b/two-fer/README.md @@ -0,0 +1,45 @@ +# Two Fer + +Welcome to Two Fer on Exercism's Perl 5 Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +`Two-fer` or `2-fer` is short for two for one. One for you and one for me. + +Given a name, return a string with the message: + +```text +One for name, one for me. +``` + +Where "name" is the given name. + +However, if the name is missing, return the string: + +```text +One for you, one for me. +``` + +Here are some examples: + +|Name |String to return +|:-------|:------------------ +|Alice |One for Alice, one for me. +|Bob |One for Bob, one for me. +| |One for you, one for me. +|Zaphod |One for Zaphod, one for me. + +## Source + +### Created by + +- @m-dango + +### Contributed to by + +- @rfilipo + +### Based on + +https://github.com/exercism/problem-specifications/issues/757 +\ No newline at end of file diff --git a/two-fer/TwoFer.pm b/two-fer/TwoFer.pm @@ -0,0 +1,23 @@ +package TwoFer; +use strict; +use warnings; +use Exporter qw<import>; +our @EXPORT_OK = qw<two_fer>; + +sub two_fer { + my ($name) = @_; + return "One for " . ($name ? $name : 'you') . ", one for me."; +} + +1; + +__END__ +If you are using perl v5.20 or newer, you +can replace the existing sub declaration +with the following code: + +use experimental qw(signatures); + +sub two_fer ($name = undef) {} + +1; diff --git a/two-fer/two-fer.t b/two-fer/two-fer.t @@ -0,0 +1,49 @@ +#!/usr/bin/env perl +use Test2::V0; +use JSON::PP; +use constant JSON => JSON::PP->new; + +use FindBin qw<$Bin>; +use lib $Bin, "$Bin/local/lib/perl5"; + +use TwoFer qw<two_fer>; + +my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; }; +plan 4; + +imported_ok qw<two_fer> or bail_out; + +for my $case (@test_cases) { + is $case->{input}{name} + ? two_fer( $case->{input}{name} ) + : two_fer(), + $case->{expected}, $case->{description}; +} + +__DATA__ +[ + { + "description": "no name given", + "expected": "One for you, one for me.", + "input": { + "name": null + }, + "property": "twoFer" + }, + { + "description": "a name given", + "expected": "One for Alice, one for me.", + "input": { + "name": "Alice" + }, + "property": "twoFer" + }, + { + "description": "another name given", + "expected": "One for Bob, one for me.", + "input": { + "name": "Bob" + }, + "property": "twoFer" + } +]