Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving empty data via Storable 1

Status
Not open for further replies.

Zhris

Programmer
Aug 5, 2008
254
GB
Hey,

I'm currently writing an OO chat room in Perl and have bumped into a couple of issues I am unable to resolve easily.

My aim was to use CGI::Session to handle users, and Storable to handle messages. Both of these modules can store data structures which is important.

One of the first things I do in the chat script is get the messages. However if the "message response" is empty at this point there are two issues:

____________________________________________________________________
1)

"Magic number checking on storable file failed at blib/lib/Storable.pm (autosplit into blib/lib/auto/Storable/_retrieve.al)"

This happens when a totally empty text file is retrieved, inevitably because the file isn't a "Storable file" yet. Is there a way that Storable can handle turning a file into a "Storable file" for me, or will I have to create some routine to do it myself?

____________________________________________________________________
2)

"Modification of non-creatable array value attempted, subscript -1"

While checking the last message a user has seen, I need to look each time for the last message id:

Code:
my $messages = $chat_room->get_messages(handle => \&handle_get_messages);

$chat_room->update_my_user(id => $messages->[-1]->{id}) if ($messages);

However if $messages (an array reference), is empty then I have problems with using $messages->[-1]->{id} even with a condition. Whats the alternative way around this?
____________________________________________________________________

Any advice will be greatly appreciated. Thanks alot,

Chris
 
Hi Chris,

1. A fairly quick hack is to stick the retrieve into an eval, so the die is caught, this is something I often do myself, i.e:

eval '$status = retrieve($config->{\'status_file\'})';

Then I just check $status is defined...

2. You might want to change the if statement to the following:

if(defined($messages) && scalar(@$messages))

This way you check the array has entries and is defined

Cheers,
Scott
 
Hi Scott,

I haven't fully tested yet, but thank you very very much for your solution. I changed my code to incorporate the methods you suggested (as well as a couple of other things to suit my wider code) and both my issues have been resolved.

Before your response I used a dirty solution of creating an initial message log which contained 1 "dummy" message. I'm very pleased with the way its working now.

Thank you once again.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top