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

I trying to learn the art of Creating a Package (module)
So I want to make sure everything works the way it should so I enabled stricts and warnings.

Everytime I run my Code I end up with the Following errors:

Use of uninitialized value in string ne at C:/Perl/lib/Services.pm line 78.
Use of uninitialized value in exists at C:/Perl/lib/Services.pm line 67.

Could you tell me what I am doing wrong Or how I can Clean it up.

package Services; require Exporter; use Win32::OLE qw( in ); use strict; use warnings; require DBD::mysql; my @ISA = qw(Exporter); =head1 NAME Services - Connects to Servers to get Services and Usernames =head1 SYNOPSIS use Services; Services::UpdateDB; =head1 DESCRIPTION This module provides a list of all services running on a Server/PC and + the User Account That it is running under. The code begins after the "cut" statement. =cut my @EXPORT = qw( UpdateDB ); sub TimeClock { my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my @weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun); my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $da +yOfWeek, $dayOfYear, $daylightSavings) = localtime(); my $year = 1900 + $yearOffset; my $theTime = "$hour:$minute:$second, $weekDays[$dayOfWeek] $month +s[$month] $dayOfMonth, $year"; return $theTime } sub Services::UpdateDB { my $Machine = shift; my $IP = shift; my $Logfile = shift || "Null"; my $DSN = 'driver={SQL Server};Server=10.10.0.31;database=Services +_Info;uid=Nope;pwd=WhatEver;'; my $ServiceQuery = "Select Service from dbo.Services"; my $Class = "CIM_Service"; my %Service_Info; my $DataHandle = DBI->connect("dbi:ODBC:$DSN") or die "$DBI::errs +tr\n"; my $StatementHandle = $DataHandle->prepare("SELECT * FROM dbo.Serv +ices where ServerName = '$Machine'"); $StatementHandle->execute(); while(my $RecordData = $StatementHandle->fetchrow_hashref) { $Service_Info{ $RecordData->{Service} } = $RecordData->{Server +Name}; } my $WMIServices = Win32::OLE->GetObject("winmgmts:\\\\$Machine\\ro +ot\\CIMV2"); my $Services = $WMIServices->ExecQuery("SELECT * FROM $Class"); foreach my $Service ( in($Services)) { if(defined $Service_Info{ $Service->{DisplayName} }) { $StatementHandle = $DataHandle->prepare("UPDATE dbo.Servic +es SET ServerName = '$Machine', IP_Address = '$IP', Username = '$Service->{StartName}', Service = '$Service->{DisplayName}' where ServerName = '$Machine' AND Service = '$S +ervice->{DisplayName}'"); $StatementHandle->execute(); undef $Service_Info{ $Service->{DisplayName} }; } else { if ($Service->{DisplayName} ne "") { if ($Service->{StartName} ne "") { $Service->{DisplayName} =~ s/'/ /g; $StatementHandle = $DataHandle->prepare("INSERT IN +TO dbo.Services (ServerName, IP_Address, Username, Service) VALUES ('$Machine', '$IP', '$Service->{StartNa +me}', '$Service->{DisplayName}')"); $StatementHandle->execute(); undef $Service_Info{ $Service->{DisplayName} }; } } } } } 1;

Replies are listed 'Best First'.
Re: Problems with uninitialized value
by NetWallah (Canon) on Jan 05, 2008 at 00:10 UTC
    In both those lines, you are likely to encounter the warnings you see if
    $Service->{DisplayName}
    is undef.

    I would suggest doing a check for "exists $Service->{DisplayName} " as soon as you enter the "for my $Service" loop, and ivoking code if it does not exist. You could also try to run this with the perl debugger, although the OLE module is hostile toward that, and produces a lot of noise.

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

Re: Problems with uninitialized value
by alexm (Chaplain) on Jan 05, 2008 at 00:10 UTC
    foreach my $Service ( in($Services)) { if(defined $Service_Info{ $Service->{DisplayName} }) {
    It seems that $Service may be defined but it's not a hash reference or it is and doesn't have the key DisplayName. I'm just guessing, since I don't have a Win32 Perl to try.

    I'd give a try to perl debugger (perl -d script.pl) or use Data::Dumper to print debug messages with the content of $Service.

Re: Problems with uninitialized value
by jwkrahn (Abbot) on Jan 04, 2008 at 23:17 UTC
    Use of uninitialized value in exists at C:/Perl/lib/Services.pm line 6 +7. Use of uninitialized value in string ne at C:/Perl/lib/Services.pm lin +e 78.

    You don't use the exists function anywhere in the code you posted so the warning is probably from some other code that you haven't posted.