Anonymous avatar Anonymous committed 24c5118

Extract a method.

Comments (0)

Files changed (1)

abc-path/perl-quick-and-dirty/abc-path-solver.pl

     return $tb;
 }
 
+# Input the board.
+
+sub input_from_file
+{
+    my ($class, $board_fn) = @_;
+
+    my $solver = $class->new;
+
+    open my $in_fh, "<", $board_fn
+        or die "Cannot open '$board_fn' - $!";
+
+    my $first_line = <$in_fh>;
+    chomp($first_line);
+
+    my $magic = 'ABC Path Solver Layout Version 1:';
+    if ($first_line !~ m{\A\Q$magic\E\s*\z})
+    {
+        die "Can only process files whose first line is '$magic'!";
+    }
+
+    my $layout_string = '';
+    foreach my $line_idx (1 .. 7)
+    {
+        chomp(my $line = <$in_fh>);
+        $layout_string .= "$line\n";
+    }
+    close($in_fh);
+
+    $solver->input({ layout => $layout_string, version => 1});
+
+    return $solver;
+}
+
 package main;
 
-my $solver = Games::ABC_Path::Solver::Board->new;
-
-# Input the board.
+# my $solver = Games::ABC_Path::Solver::Board->new;
 
 my $board_fn = shift(@ARGV);
 
-open my $in_fh, "<", $board_fn
-    or die "Cannot open '$board_fn' - $!";
-
-my $first_line = <$in_fh>;
-chomp($first_line);
-
-my $magic = 'ABC Path Solver Layout Version 1:';
-if ($first_line !~ m{\A\Q$magic\E\s*\z})
+if (!defined ($board_fn))
 {
-    die "Can only process files whose first line is '$magic'!";
+    die "Filename not specified - usage: abc-path-solver.pl [filename]!";
 }
 
-my $layout_string = '';
-foreach my $line_idx (1 .. 7)
-{
-    chomp(my $line = <$in_fh>);
-    $layout_string .= "$line\n";
-}
-close($in_fh);
-
-$solver->input({ layout => $layout_string, version => 1});
-
+my $solver = Games::ABC_Path::Solver::Board->input_from_file($board_fn);
 # Now let's do a neighbourhood inferring of the board.
 
 $solver->neighbourhood_and_individuality_inferring;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.