http://qs1969.pair.com?node_id=680408

negzero7 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get perl to read in a bunch of info from the user and format it, to print it out on screen like this:

+--------------------------------------------------------------+ | Check No. $chkno | | $first $last | | $addr[0] | | $addr[1] | | $phone $date | | | | Pay to the | | Order of: $payee $amount | | | | For: $memo Stamped Signature | | | +--------------------------------------------------------------+

I keep running into errors and I am not sure what's wrong. The address is supposed to be stored in an @array[0] and @array1 (first for house number and street, second for city, state, zip).

Take a look at my code and see if you can help me out.

#!/usr/bin/env perl use warnings; use strict; print "Please enter the check number: "; chomp (my $chkno = <STDIN>); print "Please enter your first and last name: "; chomp (my $first = <STDIN>); chomp (my $last = <STDIN>); print "Please enter your house number (or PO box) and street name: "; chomp (my $one =<STDIN>); print "Please enter your city, state, and zipcode: "; chomp (my $two = <STDIN>); my @addr = {$one, $two}; print "Please enter your phone number: "; chomp (my $phone = (<STDIN>)); print "Please enter the date: "; chomp (my $date = <STDIN>); print "Please enter the payee's name: "; chomp (my $payee = <STDIN>); print "Please enter the amount to be paid: "; chomp (my $amount = <STDIN>); print "Please enter a memo for the check: "; chomp (my $memo = <STDIN>); format CHECK = +--------------------------------------------------------------+ | Check No. @##### | $chkno, | @<<<<<<<<< @||||||||||||||| | $first, $last, | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | $addr[0], | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | $addr[1], | @<<<<<<<<<< @|||||||||||||||||| | $phone, $date, | | | Pay to the | | Order of: @|||||||||||||||||||||||||||| @#####.## | $payee, $amount, | | | For: @<<<<<<<<<<<<<<< Stamped Signature | $memo | | +--------------------------------------------------------------+ . write (CHECK);

Replies are listed 'Best First'.
Re: Formatting and write help
by ikegami (Patriarch) on Apr 15, 2008 at 01:43 UTC

    Curly brackets create a hash, initializes it with the contents of the curlies, and returns a reference to the hash. So

    my @addr = {$one, $two};

    means

    my %anon_hash; $anon_hash{$one} = $two; my @addr = \%anon_hash;

    Obviously, that's not what you want. To build a list, use parentheses.

    my @addr = ($one, $two);
Re: Formatting and write help
by ysth (Canon) on Apr 15, 2008 at 02:26 UTC

      Thanks for the tip, I wasn't catching that part from the examples I was using, it now works perfectly!

Re: Formatting and write help
by moklevat (Priest) on Apr 15, 2008 at 01:47 UTC
    It looks like you'll have a working solution with ikegami's help, but if you're in the mood to explore a different way of accomplishing your task, you might look at Perl6::Form.
Re: Formatting and write help
by amarquis (Curate) on Apr 15, 2008 at 01:45 UTC

    Curly braces define an anonymous hash reference, you want parens to make a list:

    my @addr = ($one, $two);

    Edit: Beaten by Ikegami, which seems to be a running theme :).

      Whoops, silly mistake there. Ok, that is fixed, but I am getting this error:

      write() on unopened filehandle CHECK at project3.pl line 63, <STDIN> line 10.

      Now I'm assuming that means that my filehandle CHECK is somehow not initialized or it's not formatted properly to be read. Any help with that one?