Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

undefined value as a hash reference?

by grashoper (Monk)
on Oct 31, 2008 at 16:33 UTC ( [id://720728]=perlquestion: print w/replies, xml ) Need Help??

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

I have a script that is giving me an error that I can't use an undefined value as a hash reference on line 75. where is the hash reference in this line? How can I check to see where the problem is at I am stumped on this. I can add the whole script but its quite long its purpose is to remind users they have scheduled a training class on one of our products.
line 75 which it says has the hash error is ..my $SQL_UserUpdate = "up +date ClassRoster set Reminded={ fn NOW() } where idEnrollee = '".$RS_ +UsersToRemind->Fields('idEnrollee')->{Value}."';"; what do the braces + do around fn now?

Replies are listed 'Best First'.
Re: undefined value as a hash reference?
by ccn (Vicar) on Oct 31, 2008 at 16:38 UTC

    The problem is here: $RS_UsersToRemind->Fields('idEnrollee')->{Value}
    Obviously $RS_UsersToRemind->Fields('idEnrollee') returned undef for some reason.

    You can check that value before composing SQL query.

      How do I check that value? thats my problem I did not write the original and I am not that familiar with the hash and hash reference stuff I would like to print out the value all I get when I try to print is the address I want the value. I am guessing thats what ole-hash(somenumber) is the memory address.
Re: undefined value as a hash reference?
by MidLifeXis (Monsignor) on Oct 31, 2008 at 16:43 UTC

    Assumption: you are using DBI.

    First, I would use placeholders:

    my $SQL_UserUpdate = q(update ClassRoster set Reminded={ fn NOW() } wh +ere idEnrollee = '?'); ... ... $stmt = $db->prepare($SQL_UserUpdate); $stmt->execute($RS_UsersToRemind->Fields('idEnrollee')->{Value});

    However, the real cause of the error is that, because your string is in single quotes, the parser is trying to resolve the stuff between "{" and "}" as a hashref. Since fn NOW() apparently has a value of undef, {undef} generates the error you are seeing. what ccn said above.

    --MidLifeXis

      These are the errors I get what could be the problem?
      prepare('SELECT ClassSchedule.EventDate AS EventDate,DATEDIFF(Day,{fn +NOW()},ClassSchedule.EventDate) AS Until,,DATEDIFF(Day,ClassRoster.En +rolled, ClassSchedule.EventDate) AS Since,,Classes.Duration AS Durati +on, Classes.Topic AS Topic, Classes.Description AS Description, ClassLocation.Location AS Location, ClassLocation.Address AS Address, ClassLocation.Directions AS Directions, Cl...')= DBI::st=HASH(0x37642dc) at test5.pl line 55 !! ERROR: 1 '[Microsoft][ODBC SQL Server Driver][SQL Server]Line 1 +: Incorrect syntax near ','. (SQL-42000) [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not +be prepared. (SQL-42000)' (err#2) <- execute= undef at test5.pl line 57 ERROR: 1 '[Microsoft][ODBC SQL Server Driver][SQL Server]Line 1 +: Incorrect syntax near ','. (SQL-42000) [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not +be prepared. (SQL-42000)' (err#2) <- DESTROY(DBI::st=HASH(37641ec))= undef at test5.pl line 57 ERROR: 1 '[Microsoft][ODBC SQL Server Driver][SQL Server]Line 1 +: Incorrect syntax near ','. (SQL-42000) [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not +be prepared. (SQL-42000)' (err#0) <- DESTROY(DBI::db=HASH(376401c))= undef at test5.pl line 57 !! ERROR: 1 CLEARED by call to disconnect_all method <- disconnect_all= '' at DBI.pm line 715 ! <- DESTROY(DBI::dr=HASH(3763b8c))= undef during global destruction

        Search for m/,,/, you have Since,,Classes.Doration. I would guess that this would be the cause.

        --MidLifeXis

Re: undefined value as a hash reference?
by grashoper (Monk) on Oct 31, 2008 at 17:36 UTC
    I am not sure adding the whole script in
    #### #### Filename: reminder.pl #### Version: .2.0 #### Written By: Monty Edwards #### Written Date: 03/22/06 #### Last Change Date: 03/27/06 #### Updated 10/31/2008 #### Comments: 03/22/06 This had to be completly re-written due to the + excahnge server change #### Comments: 03/27/06 Fixed logging to include date/timestamp and se +perate days with newlines #### #### Modules #### use OLE; use Mail::Sender; #### lnitialization of Global Variables & Arrays #### my $debug =0; my $t = time; (my $sec, my $min, my $hour, my $mday, my $mon, my $year, my $wday +, my $yday, my $isdst)=localtime($t); $year = $year + "1900"; my $SQLServer = "VASVCSSQL"; my $DB = "MLXhelp"; my $SQLUname = "user"; my $SQLpass="pass"; #### Get List of Users to Remind #### open(LOGFILE,">>Reminder.log"); print LOGFILE "\n\n".localtime($t)."\n"; my $Conn = CreateObject OLE "ADODB.Connection"; $Conn->Open("PROVIDER=SQLOLEDB;DATA SOURCE=$SQLServer;UID=$SQLUnam +e;PWD=$SQLpass;DATABASE=$DB"); my $RS_UsersToRemind = CreateObject OLE "ADODB.Recordset"; my $RS_UserUpdate = CreateObject OLE "ADODB.Recordset"; if ($Conn) { my $SQL_UsersToRemind = " SELECT ClassSchedule.EventDate AS EventDate, DATEDIFF(dd, { fn NOW() }, ClassSchedule.EventDate) AS + Until, DATEDIFF(dd, ClassRoster.Enrolled, ClassSchedule.Event +Date) AS Since, Classes.Duration AS Duration, Classes.Topic AS Topic, Classes.Description AS Description, ClassLocation.Location AS Location, ClassLocation.Address AS Address, ClassLocation.Directions AS Directions, ClassLocation.Map AS Map, ClassRoster.idEnrollee AS idEnrollee, ClassRoster.System AS System, ClassRoster.Account AS Account, ClassRoster.PublicID AS PublicID, ClassRoster.Name AS Name, ClassRoster.Email AS emailAddress FROM ClassSchedule INNER JOIN Classes ON ClassSchedule.id_Class = Classes.idClas +s INNER JOIN ClassRoster ON ClassSchedule.idEvent = ClassRoster +.id_Event INNER JOIN ClassLocation ON ClassSchedule.id_Location = Class +Location.idLocation WHERE (ClassSchedule.EventDate > { fn NOW() }) AND (ClassRoster.Cancelled IS NULL) AND (ClassRoster.Reminded IS NULL) AND (DATEDIFF(dd, ClassRoster.Enrolled, ClassSchedule. +EventDate) > 7) AND (NOT (DATEPART(dw, { fn NOW() }) IN (1, 7))) AND (DATEDIFF(dd, { fn NOW() }, ClassSchedule.EventDat +e) < 7)"; $RS_UsersToRemind->Open($SQL_UsersToRemind, $Conn); if ($RS_UsersToRemind) { while(!$RS_UsersToRemind->EOF()) { my $SQL_UserUpdate = "update ClassRoster set Reminded= +{ fn NOW() } where idEnrollee = '".$RS_UsersToRemind->Fields('idEnrol +lee')->{Value}."';"; my $sender = new Mail::Sender { smtp => 'email.changedforsecurity.com', from => 'email@arealplace.com' }; ( ref ($sender->MailMsg( { to => "".$RS_UsersToRemind->Fields('emailAddre +ss')->{Value}."", bcc => "email@realplace.com" + subject => "REMINDER: ".$RS_UsersToRemind->Fiel +ds('Topic')->{Value}."", msg => "This message is to remind you that you are registered to attend ".$RS +_UsersToRemind->Fields('Topic')->{Value}.". Date/Time: ".$RS_UsersToRemind->Fields('EventDate')->{Value}." Location: ".$RS_UsersToRemind->Fields('Location')->{Value}." Address : ".$RS_UsersToRemind->Fields('Address')->{Value}." Seating is limited, so if you no longer plan to attend the class, plea +se cancel on www.MLXhelp.com", auth => 'LOGIN', authid => 'FAMLS\\mlukasze', authpwd => 'Rospgalk8000', }) ) and print LOGFILE "Mail sent to ".$RS_UsersT +oRemind->Fields('emailAddress')->{Value}." OK.\n" ) or die "$Mail::Se +nder::Error\n"; $Conn->Execute($SQL_UserUpdate); print LOGFILE "Updated ".$RS_UsersToRemind->Fields('Sy +stem')->{Value}.",".$RS_UsersToRemind->Fields('Account')->{Value}."," +.$RS_UsersToRemind->Fields('PublicID')->{Value}.",".$RS_UsersToRemind +->Fields('Name')->{Value}.",".$RS_UsersToRemind->Fields('emailAddress +')->{Value}." for ".$RS_UsersToRemind->Fields('EventDate')->{Value}." +,".$RS_UsersToRemind->Fields('Topic')->{Value}.",".$RS_UsersToRemind- +>Fields('Location')->{Value}." OK.\n" or die "Unable to Update\n"; $RS_UsersToRemind->MoveNext(); } $RS_UsersToRemind->Close(); } $Conn->Close; close(LOGFILE); }
Re: undefined value as a hash reference?
by grashoper (Monk) on Oct 31, 2008 at 21:40 UTC
    I tried to print the value of the hash reference all I get is this.. ole=hash(0x1822e2c)
    if ($RS_UsersToRemind) { while(!$RS_UsersToRemind->EOF()) { print "$RS_UsersToRemind \n"; my $test=eval $RS_UsersToRemind->Fields('idEnrollee')- +>{Value}; print "test is : $test"; my $SQL_UserUpdate = "update ClassRoster set Reminded= +{ fn NOW() } where idEnrollee = '".$RS_UsersToRemind->Fields('idEnrol +lee')->{Value}."';"; my $sender = new Mail::Sender { smtp => 'vaexch001.firstamericanmls.com', from => 'training@marketlinx.com' }; ( ref ($sender->MailMsg( { to => "".$RS_UsersToRemind->Fields('emailAddre +ss')->{Value}."", bcc => "training\@marketlinx.com, fran +cisco.oquendo\@firstamericanmls.com,medwards\@marketlinx.com", subject => "REMINDER: ".$RS_UsersToRem +ind->Fields('Topic')->{Value}."", msg => "This message is to remind you that you are registered to attend ".$RS +_UsersToRemind->Fields('Topic')->{Value}.".

      Peer inside the hash reference via Data::Dumper ...

      use Data::Dumper; { local $Data::Dumper::Indent = 1; local $Data::Dumper::Deepcopy = 1; local $Data::Dumper::Sortkeys = 1; warn Dumper( $RS_UsersToRemind->Fields( 'idEnrollee' ) ); }

        hm all I get with that is var1 is undefined. maybe I put in the wrong place or something?
        #### #### Filename: reminder.pl #### Version: .1.0 #### Written #### Written Date: 03/22/06 #### Last Change #### Comments: 03/22/06 This had to be completly re-written #### Comments: 03/27/06 Fixed logging to include date/timestamp and se +perate days with newlines #### Comments: 10/28/08 Updated From Email address to generic address +to exchange changes,added ### myself for debugging purposes. #### 12/19/06 Removed all references to old IDs #### #### Modules #### use OLE; use Mail::Sender; use Data::Dumper; #### lnitialization of Global Variables & Arrays #### my $debug =0; my $t = time; (my $sec, my $min, my $hour, my $mday, my $mon, my $year, my $wday +, my $yday, my $isdst)=localtime($t); $year = $year + "1900"; #### my $SQLServer="servername"; my $DB = "$dbname"; ### changed for security.. my $SQLUname = "username"; my $SQLpass="pass"; #### Get List of Users to Remind #### open(LOGFILE,">>Reminder.log"); print LOGFILE "\n\n".localtime($t)."\n"; my $Conn = CreateObject OLE "ADODB.Connection"; $Conn->Open("PROVIDER=SQLOLEDB;DATA SOURCE=$SQLServer;UID=$SQLUnam +e;PWD=$SQLpass;DATABASE=$DB"); my $RS_UsersToRemind = CreateObject OLE "ADODB.Recordset"; my $RS_UserUpdate = CreateObject OLE "ADODB.Recordset"; if ($Conn) { my $SQL_UsersToRemind = " SELECT ClassSchedule.EventDate AS EventDate, DATEDIFF(dd, { fn NOW() }, ClassSchedule.EventDate) AS + Until, DATEDIFF(dd, ClassRoster.Enrolled, ClassSchedule.Event +Date) AS Since, Classes.Duration AS Duration, Classes.Topic AS Topic, Classes.Description AS Description, ClassLocation.Location AS Location, ClassLocation.Address AS Address, ClassLocation.Directions AS Directions, ClassLocation.Map AS Map, ClassRoster.idEnrollee AS idEnrollee, ClassRoster.System AS System, ClassRoster.Account AS Account, ClassRoster.PublicID AS PublicID, ClassRoster.Name AS Name, ClassRoster.Email AS emailAddress FROM ClassSchedule INNER JOIN Classes ON ClassSchedule.id_Class = Classes.idClas +s INNER JOIN ClassRoster ON ClassSchedule.idEvent = ClassRoster +.id_Event INNER JOIN ClassLocation ON ClassSchedule.id_Location = Class +Location.idLocation WHERE (ClassSchedule.EventDate > { fn NOW() }) AND (ClassRoster.Cancelled IS NULL) AND (ClassRoster.Reminded IS NULL) AND (DATEDIFF(dd, ClassRoster.Enrolled, ClassSchedule. +EventDate) > 7) AND (NOT (DATEPART(dw, { fn NOW() }) IN (1, 7))) AND (DATEDIFF(dd, { fn NOW() }, ClassSchedule.EventDat +e) < 7)"; $RS_UsersToRemind->Open($SQL_UsersToRemind, $Conn); local $Data::Dumper::Indent = 1; local $Data::Dumper::Deepcopy = 1; local $Data::Dumper::Sortkeys = 1; warn Dumper( $RS_UsersToRemind->Fields( 'idEnrollee' )); if ($RS_UsersToRemind) { while(!$RS_UsersToRemind->EOF()) { print "$RS_UsersToRemind"; my $SQL_UserUpdate = "update ClassRoster set Reminded= +{ fn NOW() } where idEnrollee = '".$RS_UsersToRemind->Fields('idEnrol +lee')->{Value}."';"; my $sender = new Mail::Sender { smtp => 'vaexch001.firstamericanmls.com', from => 'training@marketlinx.com' }; ( ref ($sender->MailMsg( { to => "".$RS_UsersToRemind->Fields('emailAddre +ss')->{Value}."", bcc => "training\@marketlinx.com, fran +cisco.oquendo\@firstamericanmls.com,medwards\@marketlinx.com", subject => "REMINDER: ".$RS_UsersToRem +ind->Fields('Topic')->{Value}."", msg => "This message is to remind you that you are registered to attend ".$RS +_UsersToRemind->Fields('Topic')->{Value}.". Date/Time: ".$RS_UsersToRemind->Fields('EventDate')->{Value}." Location: ".$RS_UsersToRemind->Fields('Location')->{Value}." Address : ".$RS_UsersToRemind->Fields('Address')->{Value}." Seating is limited, so if you no longer plan to attend the class, plea +se cancel on www.MLXhelp.com", auth => 'LOGIN', authid => 'FAMLS\\foquendo', authpwd => '', }) ) and print LOGFILE "Mail sent to ".$RS_UsersT +oRemind->Fields('emailAddress')->{Value}." OK.\n" ) or die "$Mail::Se +nder::Error\n"; $Conn->Execute($SQL_UserUpdate); print LOGFILE "Updated ".$RS_UsersToRemind->Fields('Sy +stem')->{Value}.",".$RS_UsersToRemind->Fields('Account')->{Value}."," +.$RS_UsersToRemind->Fields('PublicID')->{Value}.",".$RS_UsersToRemind +->Fields('Name')->{Value}.",".$RS_UsersToRemind->Fields('emailAddress +')->{Value}." for ".$RS_UsersToRemind->Fields('EventDate')->{Value}." +,".$RS_UsersToRemind->Fields('Topic')->{Value}.",".$RS_UsersToRemind- +>Fields('Location')->{Value}." OK.\n" or die "Unable to Update\n"; $RS_UsersToRemind->MoveNext(); } $RS_UsersToRemind->Close(); } $Conn->Close; close(LOGFILE); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://720728]
Approved by ccn
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 05:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found