Project::Euler::Problem::Base - Abstract class that the problems will extend from
package Project::Euler::Problem::P999; use Moose; with 'Project::Euler::Problem::Base';
To ensure that each problem class performs a minimum set of functions, this class will define the basic subroutines and variables that every object must implement.
Problem number on the problem's website
Short name for the problem designated by the module author
Date the problem was posted on the website
Description posted on the problem's website
The base URL for the problems on http://projecteuler.net
URL to the problem's homepage
Default input posted on the problem's website
Answer for the default input
Indicates if the problem takes any input from the user
Whether the problem should use the default input/answer strings
A message to assist the user in using the specific problem
The user-provided input to the problem
The user-provided answer to the problem
Flag to indicate if the last run was successful
The solved answer from the previous run
The wanted answer from the previous run
Any additional information the last run provided
These two functions must be overridden by the extending class
Ensure the input provided by the user is compliant
This is the main function which will return the status/answer for a problem
This function will point to the internal function that actually solves the problem. Depending on the object attributes that are set, it uses either the default or provided inputs (if they are required). It returns the answer as a string in scalar context, or an array containing the status, calculated answer, and expected answer. If values are passed to the function, then they are taken as the custom_input and custom_answer respectively. This also turns off use_defaults temporarily.
my $problem_1 = Project::Euler::Problem::P001->new(); my $p1_def_answer = $problem_1->solve; $problem_1->custom_input => (42); $problem_1->custom_answer => (24); $problem_1->use_defaults => (0); my $p1_custom_answer = $problem_1->solve; my ($p1_status, $p1_answer, $p1_expected) = $problem_1->solve; # OR # my $problem_2 = Project::Euler::Problem::P002->new(); my $p2_def_answer = $problem_2->solve; # Providing input automatically stops using the defaults my $p2_custom_answer = $problem_2->solve( 1, 4 ); # Provide custom input & answer my ($p2_status, $p2_answer, $p2_expected) = $problem_2->solve;
This function simply returns a nice, readable status message that tells you the outcome of the last run of the module.
my $problem_1 = Project::Euler::Problem::P001->new(); $problem_1->solve; my $message = $problem_1->last_run_message;
Adam Lesperance <email@example.com>
This software is copyright (c) 2010 by Adam Lesperance.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.