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

This gives an error that states that I split an unintialized value, why? I declared all variables and i just gave split a string to parse. I am not familiar with these errors so far since I am trying to script with warnings and strict from now on to become a better programmer. I am sorry for asking question so regularly however I am not have a huge trouble figuring these things error. Thank you so much in advance.

use LWP::Simple; use List::Util 'max'; use strict; use warnings; stockquotegetter(); sub stockquotegetter { my (@quotes,$html,@aandelen,@url,@url1,@url2,$aandeel,%stockhash1); $html = get("http://www.iex.nl/Koersen/Aandelen.aspx") or die "Couldn't fetch the stock quotes"; @aandelen = qw(Aegon Air Ahold Aperam Akzo ASML ArcelorMittal ING Bosk +alis Corio DSM Fugro Heineken KPN Philips PostNL Randstad Reed Royal + SBM TNT TomTom Wolters Unilever); foreach $aandeel (@aandelen) { print "$aandeel"; @url = split(/Realtime koers $aandeel/, $html); @url1 = split(/LastPrice"><span>/, $url[1]); @url2 = split(/</, $url1[1]); printf "$aandeel $url2[0]\n"; push (@quotes,$url2[0]); $stockhash1{ $aandeel } = $url2[0]; #eval for hourly } #print @quotes; return %stockhash1; #print %stockhash1; }

Replies are listed 'Best First'.
Re: why does this perl routine error?
by toolic (Bishop) on Sep 07, 2012 at 20:40 UTC
    The 1st "Use of uninitialized value in split" warning message you get is because the @url array does not have more that one element. Therefore, $url[1] is undefined. You can prove this to yourself by adding this debug line:
    @url = split( /Realtime koers $aandeel/, $html ); print scalar(@url), "\n"; # show number of elements in array

    See also:

Re: why does this perl routine error?
by jethro (Monsignor) on Sep 07, 2012 at 20:42 UTC

    You have more than one split in there. If the first split can't split because the search string isn't found then the complete string would be in $url[0]. Consequently $url[1] would be undef. Same for @url1

      Thank I will look into this now:) I am happy when i finally program with strict and warnings on since I can declare lexicals strictly and make easily reusable modules:)

Re: why does this perl routine error?
by kcott (Archbishop) on Sep 08, 2012 at 07:52 UTC
    "I am not familiar with these errors so far since I am trying to script with warnings and strict from now on to become a better programmer."

    Try using the diagnostics pragma for more descriptive error messages:

    use strict; use warnings; use diagnostics;

    As an example, I posted this earlier today:

    $ perl -Mstrict -Mwarnings -le 'for (0..1) { our $x = $_ } print $x' Variable "$x" is not imported at -e line 1. Global symbol "$x" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors.

    This is probably not overly useful if you've just started learning Perl; however, with diagnostics you get a more verbose explanation of the problems:

    $ perl -Mstrict -Mwarnings -le 'use diagnostics; for (0..1) { our $x = + $_ } print $x' Variable "$x" is not imported at -e line 1 (#1) (W misc) With "use strict" in effect, you referred to a global var +iable that you apparently thought was imported from another module, beca +use something else of the same name (usually a subroutine) is exported + by that module. It usually means you put the wrong funny character o +n the front of your variable. Global symbol "$x" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors (#2) (F) You've said "use strict" or "use strict vars", which indicates + that all variables must either be lexically scoped (using "my" or +"state"), declared beforehand using "our", or explicitly qualified to say which package the global variable is in (using "::"). Uncaught exception from user code: Global symbol "$x" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors. at -e line 1.

    See also: perldiag

    -- Ken

Re: why does this perl routine error?
by Anonymous Monk on Sep 07, 2012 at 20:28 UTC
    You know why :) because the variable is undef, obviously

      But $url1 has a value, how can it be undef if it has a value?@url does not generate an error if i print it and gives the html but $url does and still gives me a value:SS

        But $url1 has a value, how can it be undef if it has a value?

        How do you know?

        As Basic debugging checklist says, use Dumper, show the output