I have been having an error of "Use of uninitialized value in split at filename.pl line XX", with a split going into an array. I have -w and use strict on. When I turn them off I don't get the message, but the script doesn't print out anything to the file (pdf). I have some other code that uses the same split statement and it works perfect.

Here is the faulty code:
#!/usr/bin/perl -w use strict; use Text::Wrap; use PDF::Create; use CGI qw(:standard); use DBI(); CGI::ReadParse(); #define how many columns text to be wrapped to $Text::Wrap::columns = 58; #database stuff to get my text my $dbh = DBI->connect("DBI:mysql:database=somedb;host=xyz.net", "username", "password", {'RaiseError' => 1}); my $text_id = param('iid'); my $textfinal; my $retrieve = $dbh->prepare("SELECT text FROM texts where id=$tex +t_id"); $retrieve->execute(); while (my $ref = $retrieve->fetchrow_hashref()) { $textfinal = $ref->{'text'}; } $retrieve->finish(); # Disconnect from the database. $dbh->disconnect(); #pdf stuff to make my pdf my $pdf = new PDF::Create('filename' => '/Users/username/Desktop/p +agetest.pdf', 'Version' => 1.2, 'PageMode' => 'UseNone', 'Author' => 'www.xyz.net', 'Title' => 'Some Document', ); my $root = $pdf->new_page('MediaBox' => [ 0, 0, 612, 792 ]); # Add a page which inherits its attributes from $root my $page = $root->new_page; # Prepare 2 fonts my $font1 = $pdf->font('Subtype' => 'Type1', 'Encoding' => 'WinAnsiEncoding', 'BaseFont' => 'Helvetica'); my $font2 = $pdf->font('Subtype' => 'Type1', 'Encoding' => 'WinAnsiEncoding', 'BaseFont' => 'Courier'); #some text for the page my $line1 = 'some text here'; my $line2 = 'some text there'; my $footer = 'www.xyz.net'; #for text::wrap to format it needs these values my $initial_tab = "\t"; # Tab before first line my $subsequent_tab = ""; # All other lines flush left #split the original text into an array #this is where the error comes up #Use of uninitialized value in split at filename.pl line XX. #However when I have this in another perl script it works just fine my @text = split(/\n/, $textfinal); #indent all paragraphs not just the first my $newtext = fill($initial_tab, $subsequent_tab, @text); #now split the formatted text into lines so I can print them onto the +pdf my @page_text = split(/\n/, $newtext); #other stuff like page number, formatting, etc. my $pageNo = 1; $page->string($font2, 10, 530, 45, $pageNo++); $page->stringc($font2, 10, 306, 720, $line1); $page->stringc($font2, 10, 306, 708, $line2); $page->stringc($font2, 10, 306, 45, $footer); my $lineCountNo = 1; my $height = 675; my $lineCount = 1; foreach my $new_line(@page_text) { #this is where the formatted text comes in one line at a time +for the pdf $page->string($font2, 10, 95, $height, $new_line); $height -= 23; $lineCount++; } # finish the pdf $pdf->close;
And here is the code that works:
#!/usr/bin/perl -w use strict; use Text::Wrap; use CGI qw(:standard); use DBI(); CGI::ReadParse(); $Text::Wrap::columns = 58; my $dbh = DBI->connect("DBI:mysql:database=somedb;host=xyz.net", "username", "password", {'RaiseError' => 1}); my $text_id = param('iid'); my $textfinal; my $retrieve = $dbh->prepare("SELECT text FROM texts where id=$text_id +"); $retrieve->execute(); while (my $ref = $retrieve->fetchrow_hashref()) { $textfinal = $ref->{'text'}; } $retrieve->finish(); # Disconnect from the database. $dbh->disconnect(); my $initial_tab = "\t"; # Tab before first line my $subsequent_tab = ""; # All other lines flush left my @text = split(/\n/, $textfinal); my $newtext = fill($initial_tab, $subsequent_tab, @text); my @newtext = split(/\n/, $newtext); foreach my $gettext(@newtext) { print "$gettext\n"; }
Why am I getting this error only with the first code and the second one works perfectly?

Edit, BazB: added readmore tags.


In reply to Use of uninitialized value in split by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.