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

I'm getting an error. Maybe it's because my vars are declared all messy ? ;P
use strict; use warnings; use WWW::Mechanize; use HTML::TokeParser; my $pre = ''; my $html; my $htmll; my $htmlll; my $term = 20095; # 20095 = Summer; my $year = 1 ;# 1st = 1 , 2nd = 2, 3rd = 3, 4th = 4 my $key; my $v; my $coursecode; my $department; my $prepre; my $id; my $stuff; my $title; my $description; my $course_num; my $check; my $c_url = 'https://registrar.utm.utoronto.ca/student/timetable/new_t +t_calprev.php?course='.$pre; my $url = 'https://registrar.utm.utoronto.ca/student/timetable/index.p +hp'; my $mech = WWW::Mechanize->new(); my $stream = HTML::TokeParser->new(\$html); my $streamm = HTML::TokeParser->new(\$htmll); my $streammm = HTML::TokeParser->new(\$htmlll); my %dep; my %newdep; my @coursename; my @c_arr ;# Course array $mech->get($url); $html = $mech->content(); $stream->get_tag("option"); while(my $token = $stream->get_token()){ my $check = $token->[2]{name}; if($check eq "dept[]"){ while(my $token = $stream->get_token('option')){ if($token->[1] eq "option"){ $course_num = $token->[2]{value}; $department = $stream->get_trimmed_text(); $dep{ $course_num } = [$department]; } } } } while (($key, $v) = each(%dep)){ $coursecode = $key; $coursecode =~ s/\s//g; $department = $v; $mech->get($url); $mech->submit_form( fields=>{ session=>$term, 'yos[]'=>$year, 'dept[]'=>$key, }, ); $htmll = $mech->content(); ### PRINTS HERE while(my $tokenn = $streamm->get_token()){ ### DOESNT WANT TO PRINT HERE if($tokenn->[1] eq "a" && $tokenn->[2]{name}){ push(@coursename,$tokenn->[2]{name}); $prepre = $tokenn->[2]{name}; $newdep{ $department } = $coursecode; } } } while (($id, $stuff) = each(%newdep)){ print "Id:$id Stuff:$stuff .\n "; } sub print_csv{ foreach(@coursename){ $pre = $_; print $pre; $mech->get($c_url); $htmlll = $mech->content(); $streammm->get_tag("p"); $title = $streammm->get_trimmed_text(); $title =~ s/($coursecode).//g; $streammm->get_tag("/p"); $description = $streammm->get_trimmed_text(); #print '"'.$prefix.'";"'.$title.'";"'.$description.'";"'.$departme +nt.'"'."\n"; } }

Replies are listed 'Best First'.
Re: Darn Hash refs
by ELISHEVA (Prior) on Apr 27, 2009 at 19:02 UTC
    Use of uninitialized value in string eq at UOT_mississauga.pl line 40

    If the line 40 in your posted code is the same as the line 40 in your script, that means that the marked line below is the source of the problem.

    my @coursename; my @c_arr ;# Course array $mech->get($url); $html = $mech->content(); $stream->get_tag("option"); while(my $token = $stream->get_token()){ my $check = $token->[2]{name}; if($check eq "dept[]"){ while(my $token = $stream->get_token('option')){ #### THE LINE BELOW TRIGGERED THE FOLLOWING MSG #### #### Use of uninitialized value in string eq #### if($token->[1] eq "option"){ $course_num = $token->[2]{value}; $department = $stream->get_trimmed_text(); $dep{ $course_num } = [$department]; } } } }

    If I have the right line, my guess is that the value of $token->[1] is undefined. To verify that $token->[1] (or any other variable you might care about) is undefined, you can insert code similar to the following code snippet.

    # value surrounded with < > to make it easier to see # empty strings print STDERR "token[1]=<" . (defined($token->[1]) ? $token->[1] : 'undef') . ">\n";

    In future posts, please mark the exact line mentioned in the error message along with the error message as I have done above. We have no way of knowing whether the line numbers of the posted code match the line numbers of your script and will be playing guessing games about where your error might be unless you mark it for us.

    Best, beth

Re: Darn Hash refs
by kennethk (Abbot) on Apr 27, 2009 at 18:25 UTC
    What do you mean by "an error"? What "PRINTS HERE" but "DOESN'T WANT TO PRINT HERE"? Please read I know what I mean. Why don't you?. It's good that you've posted the code you've written, but without a clear description of your issue, we won't know how to attack the problem. Include printed errors, output, expected output, ...
      Right now I am getting this error... Use of uninitialized value in string eq at UOT_mississauga.pl line 40. I'm looking on google and here but I'm not sure how this error is happening. How would I work around this. How can token be uninitialized ?
      use strict; use warnings; use WWW::Mechanize; use HTML::TokeParser; use Data::Dumper; my $pre = ''; my $html; my $htmll; my $htmlll; my $term = 20095; # 20095 = Summer; my $year = 1 ;# 1st = 1 , 2nd = 2, 3rd = 3, 4th = 4 my $key; my $v; my $coursecode; my $department; my $prepre; my $id; my $stuff; my $title; my $description; my $course_num; my $check; my $c_url = 'https://registrar.utm.utoronto.ca/student/timetable/new_t +t_calprev.php?course='.$pre; my $url = 'https://registrar.utm.utoronto.ca/student/timetable/index.p +hp'; my $mech = WWW::Mechanize->new(); my $stream = HTML::TokeParser->new(\$html); my $streamm = HTML::TokeParser->new(\$htmll); my $streammm = HTML::TokeParser->new(\$htmlll); my %dep; my %newdep; my @coursename; my @c_arr ;# Course array $mech->get($url); $html = $mech->content(); $stream->get_tag("option"); while(my $token = $stream->get_token()){ if($token->[0] eq "S" && $token->[2]{name} eq "dept/[/]"){ print Dumper $token; while(my $token = $stream->get_token()){ if($token->[1] eq "option"){ $course_num = $token->[2]{value}; $department = $stream->get_trimmed_text(); $dep{ $course_num } = [$department]; } } } } while (($key, $v) = each(%dep)){ $coursecode = $key; $coursecode =~ s/\s//g; $department = $v; $mech->get($url); $mech->submit_form( fields=>{ session=>$term, 'yos[]'=>$year, 'dept[]'=>$key, }, ); $htmll = $mech->content(); ### PRINTS HERE while(my $tokenn = $streamm->get_token()){ ### DOESNT WANT TO PRINT HERE if($tokenn->[1] eq "a" && $tokenn->[2]{name}){ push(@coursename,$tokenn->[2]{name}); $prepre = $tokenn->[2]{name}; $newdep{ $department } = $coursecode; } } } while (($id, $stuff) = each(%newdep)){ print "Id:$id Stuff:$stuff .\n "; } sub print_csv{ foreach(@coursename){ $pre = $_; print $pre; $mech->get($c_url); $htmlll = $mech->content(); $streammm->get_tag("p"); $title = $streammm->get_trimmed_text(); $title =~ s/($coursecode).//g; $streammm->get_tag("/p"); $description = $streammm->get_trimmed_text(); #print '"'.$prefix.'";"'.$title.'";"'.$description.'";"'.$departme +nt.'"'."\n"; } }
        That start element you are processing doesn't have a "name" attribute, so $token->[2]{name} is undefined.
        Then shouldn't the compiler just skip over that line ? It's an if statement. I'm iterating through all the tags until one of them meets my conditions. I don't understand how this is bad code practice. I'm not able to know if a condition is met other wise. Why have if available to use in the first place right ?
Re: Darn Hash refs
by Anonymous Monk on Apr 27, 2009 at 18:52 UTC
    Can't use string ("") as a HASH ref while "strict refs" in use at 7603 +86.pl line 40 (#1) (F) Only hard references are allowed by "strict refs". Symbolic references are disallowed. See perlref.
    Line 40 is     my $check = $token->[2]{name};, and you're assuming $token->[2] is defined, and its a hashref (this is only true if $token is a Start tag). HTML::TokeParser::Simple might be easier to work with. Also see diagnostics,strict, perlref.