0

This is my script:

#!/usr/bin/perl
use strict;
use warnings;
use Web::Scraper;
use Data::Dumper;
use URI;
my $purlToScrape='http://www.natboard.edu.in/dnbfinal.php';
my $webdata = scraper {      
      process 'td.noticeboard', "data[]" => 'TEXT';      
  };
my $dnbnotices = $webdata->scrape(URI->new($purlToScrape));
my @noticeboard=$dnbnotices->{data};
print ref @noticeboard."\n\n";
print Dumper(@noticeboard);
print scalar @noticeboard."\n";
for my $notice ( @noticeboard ) {
    if ($notice) {
        print $notice."\n";
    }    
}
print $noticeboard[1];

Output:

 $VAR1 = [
           'December - 2016 ',
           'DNB Final December -2016',
           '30th September, 2016',
           '',
           '',
           'JUNE - 2016 ',
           'DNB Final JUNE -2016',
           '15th May, 2016',
           '',
           '',
           'Dec - 2015 ',
           'DNB Final Dec -2015',
           '9th October, 2015',
           '',
           '',
           'June - 2015 ',
           'DNB Final June -2015',
           '6th May, 2015',
           '',
           '',
           'December - 2014 ',
           'DNB Final December - 2014',
           '30th September, 2014',
           '',
           ''
         ];
 1
 ARRAY(0x4521bd8)
 Use of uninitialized value in print at ./test1.pl line 21.

Questions:

  1. Why is the ref not printing out "ARRAY"?
  2. Why is the array count shown as 1? It's not a single element array.
  3. Why are the elements of the array not being printed out?
  4. Why is $noticeboard[1] uninitialized?
1
  • If you want to dump a hash or array, use Dumper(\%hash) or Dumper(\@array). It's too confusing otherwise, as you've found out (since your array truly does have only one element, a reference to another array). Commented Apr 3, 2017 at 7:59

1 Answer 1

4

1- ref returns an empty string when called on something else than a ref. So when doing ref @xxx, you can't expect anything else than an empty string.
Furthermore, ref @noticeboard."\n\n" is the same as ref ((scalar @noticeboard)."\n\n"), which is ref "1\n\n", which returns an empty string.

2-3-4- scrape returned you an array reference, not an array. So when assigning it to an array (my @noticeboard=$dnbnotices->{data}), it went in the first element. (the fact that the array only has one element that is an array ref explains your question 2, 3 and 4).

Your script should be:

#!/usr/bin/perl
use strict;
use warnings;
use Web::Scraper;
use Data::Dumper;
use URI;
my $purlToScrape='http://www.natboard.edu.in/dnbfinal.php';
my $webdata = scraper {      
      process 'td.noticeboard', "data[]" => 'TEXT';      
  };
my $dnbnotices = $webdata->scrape(URI->new($purlToScrape));
my $noticeboard=$dnbnotices->{data};
print "ref=",ref($noticeboard),"\n\n";
print "dumper:",Dumper($noticeboard);
print "nb elements:",scalar @$noticeboard,"\n";
print "content:\n";
for my $notice ( @$noticeboard ) {
    if ($notice) {
        print "  ",$notice,"\n";
    }    
}
print "first element:",$noticeboard->[1];

Also, feel free to use say instead of print ... "\n", it can be more convenient. (you'll need use feature 'say' or use v5.10 or higher though).

If you need further references about references, see perlref and perlreftut.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.