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

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.

Replies are listed 'Best First'.
Re: Use of uninitialized value in split
by Limbic~Region (Chancellor) on Oct 30, 2003 at 13:26 UTC
    Anonymous Monk,
    The variable in question is $textfinal. The line that initializes that variable is $textfinal = $ref->{'text'};. Since that line is inside a while loop, my guess would be is that the condition for the while loop to run is never true and hence is not initializing the variable. Try this and see what you get.
    # my $textfinal; my $textfinal = "What\nis\nwrong"; # more of your code my @text = split(/\n/, $textfinal); print $_, $/ for @text;
    If my guess is correct, you will no longer get the error, but you will not get the result you expect. You will need to determine what is wrong with the condition in the while loop.

    Since the two pieces of code connect to the same database and the select statements are identical, you probably want to look at param('iid'). Additionally, you really should be using placeholders in your select statements as they avoid all kinds of gotchas.

    Cheers - L~R

Re: Use of uninitialized value in split
by dreadpiratepeter (Priest) on Oct 30, 2003 at 13:30 UTC
    from the looks of it, your select statement is not returning any rows, therefore $textfinal is never being initialized.
    Which also explains why you get no results.


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."