CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Why $stock_url->as_HTML report can't call method as_HTML ?

 



attonie
New User

May 12, 2010, 10:28 AM

Post #1 of 5 (873 views)
Why $stock_url->as_HTML report can't call method as_HTML ? Can't Post

The perl report "Can't call method "as_HTML" on an undefined value at m.pl line 27. ", The $stock_url are a HTML::ELEMENTS type same as the line 25, why report error ?


-------------------------------------------------
#!/usr/bin/perl

use HTML::Tree;
use HTML::Element;

$url="http://biz.yahoo.com/research/earncal/today.html";
$file="/tmp/today.html";
$arg_2="-O$file";
system ( "wget", $url, $arg_2) ;

print "Content-type: text/html \n\n";

$tree = HTML::TreeBuilder->new();
$tree->parse_file( $file );

@tables = $tree->find('table');

@stocks = @tables[5]->find('tr');

print "<b>Totally found ".scalar(@stocks)." Stocks.</b>";
print "<br><b>Fetching ...</b><br>";

for( $i = 3; $i < scalar(@stocks); $i++){

print @stocks[$i]->as_HTML; # line 25
$stock_url = @stocks[$i]->find('a');
print $stock_url->as_HTML; # line 27

}


FishMonger
Veteran / Moderator

May 12, 2010, 10:40 AM

Post #2 of 5 (869 views)
Re: [attonie] Why $stock_url->as_HTML report can't call method as_HTML ? [In reply to] Can't Post

@stocks[$i]
That's an array slice.

Change it to:
$stocks[$i]

Also, add these 2 pragmas, which should be in all of your scripts, and fix the problems they point out.


Code
use strict; 
use warnings;



(This post was edited by FishMonger on May 12, 2010, 10:41 AM)


attonie
New User

May 12, 2010, 10:51 AM

Post #3 of 5 (866 views)
Re: [FishMonger] Why $stock_url->as_HTML report can't call method as_HTML ? [In reply to] Can't Post

change @stocks[$i] to $stocks[$i] doesn't works, still have error that say line 25 can't call method as_HTML

use @stocks, I think is right, the @stocks contains a large set (list) of <TR>...</TR> data.


FishMonger
Veteran / Moderator

May 12, 2010, 11:12 AM

Post #4 of 5 (863 views)
Re: [attonie] Why $stock_url->as_HTML report can't call method as_HTML ? [In reply to] Can't Post


Quote
use @stocks, I think is right, the @stocks contains a large set (list) of <TR>...</TR> data.


No, it is NOT right.

An array is dereferenced with $ and since it's not an object, you can't expect to use it to call a method.

Did you add the 2 pragmas? If not, please do so before posting any additional updates.


FishMonger
Veteran / Moderator

May 12, 2010, 11:20 AM

Post #5 of 5 (860 views)
Re: [attonie] Why $stock_url->as_HTML report can't call method as_HTML ? [In reply to] Can't Post

array slices
http://perldoc.perl.org/perldata.html#Slices

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives