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

Hi Guys,

Sorry, im a bit of a noob, only been learning perl for about a fortnight, so please dont laugh to hard! Basically, Im tyring to write a program that goes off and checks rbl's and then writes the results to an HTML template....but it isnt working on one line!

the rest of it works in isolation. very frustrating

********************* use warnings; use strict; use Net::RBLClient; use HTML::Processor; my @servers = ('213.123.20.134','213.123.20.119','213.123.20.120','213 +.123.20.121','213.123.20.122','213.123.20.123', '213.123.20.124','213.123.20.125','213.123.20.126','213.123.20.127','2 +13.123.20.128','213.123.20.129', '213.123.20.130','213.123.20.131','213.123.20.132'); my %rbl_list = (); foreach my $ip (@servers){ chomp $ip; $ip =~ s/"//i; my $rbl = Net::RBLClient->new; $rbl->lookup($ip); my @listed_by = $rbl->listed_by; $rbl_list{$ip} = \@listed_by; } foreach my $server(values %rbl_list) { $server =~ s//Nothing/gi } my $output = '/root/Documents/animals_output.html'; open(FH1, ">$output"); my $tpl = new HTML::Processor; my $rbl_watch = $tpl->new_loop("blacklists"); foreach my $omr( keys %rbl_list) { $rbl_watch->array("server_ip", $omr); my $blackholes = $tpl->new_loop("listings", $omr); foreach my $type ( @{$rbl_list{$omr}{blackholes} }) { $blackholes->array ("positive_listings", $type);v } } $tpl->variable("what", "Server Status"); print $tpl->process("/root/Documents/animals.html"); close (FH1); ******************************************

This is the problem!
foreach my $type ( @{$rbl_list{$omr}{blackholes} }) { $blackholes->array ("positive_listings", $type); }
I have been told I need to use qw somewhere....where, and how cause i keep getting the following error:

Can't use string ("NothingANothingRNothingRNothingA") as a HASH ref while "strict refs" in use at webpages.pl line 43.

Swear if you guys can help ill donate somethign to you! its been driving me mad for days.

Dec 21, 2025 at 23:17 UTC McDarren Added code tags

Replies are listed 'Best First'.
Re: help! Please please help!
by Corion (Patriarch) on Mar 14, 2008 at 11:58 UTC

    What is the following line supposed to do?

    $server =~ s//Nothing/gi

    As it is, it adds "Nothing" between each character in $string. Is that what you want?

    As an aside, please use <code>...</code> around your code so it renders and downloads nicely. Then you don't need to add HTML-formatting to your code.

      Corion, thanks! I'll rememeber that for next time! Right, basically that line is supposed to replace the value in server, which is blank, with nothing. I realise it wont do that now, but if I take it out it just says this now.

      "Cant coerce arrays into has at line 39"

      Most of the time the array will be blank, does that matter? Its still that line

        Maybe you can tell us what line 39 is?

        If you want people to easily check your code, please edit your above node and put your code in between <code>...</code> tags. That way, it will render and download nicely for everybody.

        It also often helps to supply example data with the program, or to reduce the program so it doesn't need any external data anymore yet still reproduces the problem.

        As for your concrete problem, you seem to be confused about the data structure you have. The line

        foreach my $type ( @{$rbl_list{$omr}{blackholes} })

        tries to access the values in %rbl_list as a hash, but before that, you filled it with references to arrays. I find that using Data::Dumper often helps to visualize the data structure and to find where my assumptions about the data structure do differ from reality.

        The issue is that you are trying to access an array element like a hash element. You put array refs into a hash here:

        $rbl_list{$ip} = \@listed_by;

        And later access like this:

        @{$rbl_list{$omr}{blackholes}

        $rbl_list{$omr} is an array ref, so $rbl_list{$omr}{blackholes} is like saying $arrayref->{$somekey}. (You also probably want $blackholes not the bareword blackholes)

        Edit: I am undone by corion's speed.

Re: help! Please please help!
by toolic (Bishop) on Mar 14, 2008 at 12:37 UTC
    I have been told I need to use qw somewhere....where,
    Using qw is never required, but it does save some typing (no need for all those commas or quotes) and it can make the code a little easier to read. So, you could use it in your code as follows:
    my @servers = qw( 213.123.20.134 213.123.20.119 213.123.20.120 );
Re: help! Please please help!
by amarquis (Curate) on Mar 14, 2008 at 12:47 UTC

    In addition to what Corion said, you don't need to use qw, but it can save you some work and help you format your source better.

    Basically:

    my @servers = ('213.123.20.134','213.123.20.119','213.123.20.120','213.123.20.121','213.123.20.122','213.123.20.123', '213.123.20.124','213.123.20.125','213.123.20.126','213.123.20.127','213.123.20.128','213.123.20.129', '213.123.20.130','213.123.20.131','213.123.20.132');

    ... can turn into:

    my @servers = qw( 213.123.20.134 213.123.20.119 213.123.20.120 213.123.20.121 #etc etc. );

    Basically, it will split on (I believe) whitespace* and treat anything between as quoted, so you get rid of quotes and commas. It lets you have more freedom in arranging a long list.

    Also, you might get better results posting here if you add <code;> tags around your code and write a title that summarizes your issue. It helps people get a handle on your problem quickly, which makes them more likely to help you out.

    * - Just checked perldoc, "Evaluates to a list of the words extracted out of STRING, using embedded whitespace as the word delimiters."

      I should point out that one may not embed comments in a qw(), as your example might tend to suggest. Doing so (with warnings enabled) will cause the "Possible attempt to put comments in qw() list" warning to be issued.

      Actually, you can, but I don't think the results will be what you expected.

      • another intruder with the mooring in the heart of the Perl

        That's one of the reasons why when I start getting a qw// that's spanning more than 2-ish lines I've taken to changing to using YAML::Syck in a here-doc sort of like this:

        use YAML::Syck qw( Load ); my @servers = @{ Load( <<'EOT' ) }; ## Now I can have comments - 213.123.20.134 - 213.123.20.119 - 213.123.20.120 - 213.123.20.121 EOT

        If the list grows even longer you can easily move it after your __END__ token and change to my $server_data = do { local $/; <DATA> };  my @servers = @{ Load( $server_data ) };; and it's not a big step from there to swap to a separate data file and use LoadFile instead.

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

        Thanks for the correction, I'm sure it will save me some bug hunting someday :). I've never put a comment inside a qw() before and had never really thought about it before.

Re: help! Please please help!
by McDarren (Abbot) on Mar 14, 2008 at 18:45 UTC
    In the following loop:
    foreach my $type ( @{$rbl_list{$omr}{blackholes} }) { $blackholes->array ("positive_listings", $type);v } }
    What's with that "v" on the 2nd last line? Is that in your original code, or was it just a copy/paste glitch?