in reply to Writing to a file using Formats

format report_TOP=

You don't define the report filehandle yet.   You should define it first.


$~="report";
$^="report_TOP";
$title = "Some Report";

You define these variables too late to use them.


        ($num1, $num2) = (/(\d+)/g) for ($_);

That should simply be:

        ( $num1, $num2 ) = /\d+/g;


sub add{

Your subroutine is called "add" but it also opens a file, increments two variables and queries the user for a yes/no answer and a pair of numbers.   Either make the name more descriptive or reduce the functionality to match the name.


                   "START";

You have a string in void context which should generate a warning message.   But if you are trying to jump to the START label you shouldn't try to do that from inside a subroutine.

Replies are listed 'Best First'.
Re^2: Writing to a file using Formats
by bowei_99 (Friar) on Aug 02, 2009 at 08:00 UTC
    While the comments here are great, I've personally found using the Perl6::Form module to be easier to use, less finicky, and has made my code more readable and maintainable. It's written by Damian Conway; he recommends it in his book _Perl Best Practices_.

    An example from the CPAN page:

    use Perl6::Form; $text = form " =================================== ", "| NAME | AGE | ID NUMBER |", "|----------+------------+-----------|", "| {<<<<<<} | {||||||||} | {>>>>>>>} |", $name, $age, $ID, "|===================================|", "| COMMENTS |", "|-----------------------------------|", "| {[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[} |", $comments, " =================================== ";

    -- Burvil