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

I made a few other posts regarding a project I am working on. All of the individual pieces seem to be working, the whole is another story. I am going to post some of the code and examples of the data files I am looking at in the hopes that someone might be able to point me in the right direction. The code is pretty messed up at this point, I just flipped it over starting with the last sub hoping that might help. When I execute the program I get endless Use of uninitialized value error msgs. I am new to Perl (painfully obvious). So any constructive critisism is very welcome. Here is the code:
#!/usr/local/bin/perl -w use strict; use PDF::Create; open DOM_WORK, '>/usr/spool/lpd/dom/dom.work' or die "Could not open f +ile because $!"; get_text(); close DOM_WORK; get_key(); sub custinvoice { open DOM_PRINT, '/usr/spool/lpd/dom/dom.work'; my @domtext = <DOM_PRINT>; my $pdf = new PDF::Create( 'filename' => 'invoice.pdf', 'Author' => ' STANPAK', 'Title' => 'Test Invoice to PDF', 'Version' => '1.2' ); my $page = $pdf->new_page( 'MediaBox' => [ 0, 0, 792, 612 ] ); my $f1 = $pdf->font( 'Subtype' => 'Type1', 'Encoding' => 'WinAnsiEncoding', 'BaseFont' => 'Arial' ); my $image = $pdf->image('InvoiceLand.jpg'); $page->image( 'image' => $image, 'scalex' => 1, 'scaley' => 1, 'xpos' => 0, 'ypos' => 0, ); my $y = 500; foreach ( 1 .. $#domtext ) { $page->string( $f1, 10, 40, $y, $_ ); $y = $y - 8; } $pdf->close; close DOM_PRINT; } sub get_domopts { print "Hello world"; my $dom_opts = shift; my @dom_opts = split ( //, $dom_opts ); my $cvtype = $dom_opts[0]; my $cvnumber = join ( "", @dom_opts[ 1 .. 10 ] ); my $dtype = join ( "", @dom_opts[ 11, 12 ] ); my $cdir = join ( "", @dom_opts[ 13 .. 15 ] ); my $dp = join ( "", $dom_opts[16] ); my $dpform = join ( "", @dom_opts[ 17, 18 ] ); my $dpnc = join ( "", $dom_opts[19] ); my $dprntr = join ( "", @dom_opts[ 20 .. 23 ] ); my $dpfill = join ( "", @dom_opts[ 24 .. 73 ] ); my $de = join ( "", $dom_opts[74] ); my $deform = join ( "", @dom_opts[ 75, 76 ] ); my $deadd = join ( "", @dom_opts[ 77 .. 176 ] ); my $defill = join ( "", @dom_opts[ 177 .. 226 ] ); my $df = join ( "", $dom_opts[227] ); my $dfform = join ( "", @dom_opts[ 228, 229 ] ); my $dfadd = join ( "", @dom_opts[ 230 .. 269 ] ); my $dftnbr = join ( "", @dom_opts[ 270 .. 283 ] ); my $dfdev = join ( "", @dom_opts[ 284 .. 287 ] ); my $dffill = join ( "", @dom_opts[ 288 .. 337 ] ); my $dsfcntl = join ( "", @dom_opts[ 338 .. 487 ] ); my $dx = join ( "", $dom_opts[488] ); my $dxtamp = join ( "", @dom_opts[ 489 .. 490 ] ); my $dxmot = join ( "", @dom_opts[ 491 .. 500 ] ); my $dxfill = join ( "", @dom_opts[ 501 .. 650 ] ); my $domfill = join ( "", @dom_opts[ 651 .. 750 ] ); if ( $cvtype eq "C" && $dtype eq "I" ) { custinvoice( $cdir, $dp, $dpform, $dpnc, $dprntr ); } sub get_domcf { my $key = shift; open DOM_CF, '/u/ds2/r61/domcf.dat' or die "Control file unavailable $!"; my @dom_cf = <DOM_CF>; while (@dom_cf) { if (/$key/) { } my $dom_opts = ($'); get_domopts($dom_opts); close DOM_CF; } sub get_key { open DOM_TEXT, '/usr/spool/lpd/dom/dom.work' or die "Could not open work file $!\n"; while (<DOM_TEXT>) { if (/\*([^*]+)\*/) { my $key = $1; close DOM_TEXT; get_domcf($key); } } sub get_text { while (<>) { tr/\x0c-\x0d//d; print DOM_WORK $_; } } } } } here is the data I am reading in from STDIN and writing to /usr/spool/ +lpd/dom.work: *C100102 I R61* yadd yadd yadda This is a bunch of data ...yadda yadda yadda This is a bunch of data ...yadda yadda yadda This is a bunch of data ...yadda yadda yadda This is a bunch of data ...yadda yadda yadda This is a bunch of data ...yadda yadda yadda This is a bunch of data ...yadda yadda yadda This is a bunch of data ...yadda yadda yadda This is a bunch of data ...yadda yadda yadda This is a bunch of data ...yadda yadda yadda This is a bunch of data ...yadda yadda yadda This is a bunch of data ...yadda yadda yadda This is a bunch of data ...yadda yadda yadda Here is the data I am reading in from /u/ds2/r61/domcf.dat: C100102 I R61C100102 I R61Y3 1LP2 + YPFaseidas@megalink.net + + YLS68 Bridge Street, Suffield, CT 668-3001 F04 + CTED I R61CTED I R61YI 2DOM + YPS + + YS 68 BRIDGE STREET 668-3001 F04
Thanks in advance---bring the pain !

Replies are listed 'Best First'.
Re: Use of uninitialized value errors
by blackjudas (Pilgrim) on May 16, 2002 at 16:49 UTC
    Although I think your attempt is excellent, I will comment on the "uninitialized variable" warnings you get. This is happening because wherever you have a conditional based on a variable that may not contain a value, or even just printing a non-initialized variable will produce this warning. You may want to look at the lines that the warnings are producing and looking at every variable to see if it does actually contain a value. Here's a simple test:
    if ($var) { print "\$var contains $var"; } else { print 'no content in $var'; }

    I know its simplistic but it will certainly help, testing as I've found is very important. It saved me from a 14 GB error log!

    BlackJudas
      "This is happening because wherever you have a conditional based on a variable that may not contain a value"

      Nope. Perl forgives you if you simply test or assign a bare uninitialized variable in a conditional. Your code works for this reason -- no warning for "if ($var)". Going over your post, I think this is a case where you understand but just mis-stated your assertion.

      Note:

      #!/usr/bin/perl -w use strict; my $stuff; # No complaint here if ($stuff) { print "hello\n"; } # No complaint here if (my $stuff2 = $stuff) { print "hello\n"; } # But this interpolation causes a warning if (my $stuff3 = "$stuff") { print "hello\n"; } # This concatenation complains also if (my $newstuff = "new $stuff") { print "hello\n"; }

      ------------------------------------------------------------
      "Perl is a mess and that's good because the
      problem space is also a mess.
      " - Larry Wall

        True dvergin I was simply saying that if you make any decisions based on what a variable contains, if that variable is undefined, then perl will complain. But thank you for bringing that up!

        BlackJudas
Re: Use of uninitialized value errors
by jsprat (Curate) on May 16, 2002 at 17:45 UTC
    Either do what blackjudas showed above, or:
    1. Initialize your variables, which doesn't look reasonable.
    2. no warnings qw(uninitialized); #(what I would do)
    perldoc perllexwarn for more information on #2. I would suppress warnings in just that block, so the rest of your code won't be affected.