note
Fletch
<p>
Shelling out to <c>cat</c> or <c>grep</c> in backticks is almost never what you want to do (unless you're doing a one-liner throwaway or something). And your approach loses the correspondence between questions and answers.
</p>
<p>
You should write a sub, let's call it <c>parse_quiz_text</c>, which will take the filename and return your questions and answers. The sub can cheat and use <c>local $/ = q{};</c> (see [doc://perlvar]) to say you want to read in paragraph mode, then you'd split off the first line from the answer(s). Each question would be represented by a hashref of the question and an arrayref of answers ([doc://perldsc] and [doc://perlref] will be useful if you're unfamiliar with those).
</p>
<c>
sub parse_quiz_text {
my( $quiz_file ) = @_;
open( my $fh, q{<}, $quiz_file ) or die "problem opening quiz '$quiz_file': $!\n";
local( $/ ) = q{};
my @quiz_questions;
while( defined( my $paragraph = <$fh> ) ) {
my @lines = split( /\n/, $paragraph );
push @quiz_questions, { question => (shift @lines), answers => \@lines };
}
close( $fh );
return @quiz_questions;
}
</c>
<p>
<b>Additionally:</b> caveat that this does no error checking and is presuming the file is in the correct format (<i>e.g.</i> questions are always the first line of a paragraph, questions are always followed by at least one answer, yadda yadda yadda). You'd probably want to add some error checking at some point (check that the first line of the paragraph ends in a '?', check that <c>@lines >= 2</c>, . . .).
</p>
<div class="pmsig"><div class="pmsig-58196">
<p>
The cake is a lie.<br />
The cake is a lie.<br />
The cake is a lie.<br />
</p>
</div></div>
11114076
11114076