From: Jim D. <ji...@du...> - 2008-03-13 15:32:56
|
Gregg, Well, this actually works well. I needed to use "#" as the higher-level quote delimeter. I think this is okay. Whevever some object intantiates other objects whose reference is stored in the $self->{ } hash of the object, this method can be used to safely restore the objects within the object. If there was a means for each object to maintain a list of all instantiates objects, then we could abstract this whole thing in Generic_Item. Since it's not used much, I'm not sure it's worth the trouble. For my needs, I find this quite acceptable and not too much of a hack. # Trim leading and trailing spaces sub _trim { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; } # We need to save the persistent information for the Process_Item p_sphinx object between restart # and startup such that we can safely kill any running pocketsphinx processes. We'll save the # persistent information in restore_data and eval to play it out and restore the object # information. sub restore_string { &main::print_log ("PocketSphinx_Listener:: restore_string called") if $main::Debug{pocketsphinx}; my ($self) = @_; my $restore_string = ''; my $restore_data = &_trim( $self->{p_sphinx}->restore_string( ) ); $restore_data =~ s/\->/\$self\->{p_sphinx}\->/g; $restore_string .= $self->{object_name} . "->{restore_data} = q#$restore_data#;\n"; $restore_string .= $self->{object_name} . "->restore();\n" if $self->{p_sphinx}->pid( ); return $restore_string; } # restore will be called on startup or restart such that we can play out the information # contained in p_shpinx_state, which represents the persistent data for the Process_Item # object. sub restore { my ($self) = @_; my $restore_data = $self->{restore_data}; &main::print_log ("PocketSphinx_Listener:: restore_string: $restore_data") if $main::Debug{pocketsphinx}; eval $restore_data; &main::print_log ("PocketSphinx_Listener:: restore: Error in Persistent data restore: $@\n") if $@; $self->{p_sphinx}->stop( ); } An example of what appears in mh_temp.saved_states is: if (defined $pocketsphinx_listener) {$pocketsphinx_listener->{restore_data} = q#@{$self->{p_sphinx}->{cmds}} = split($;, q~pocketsphinx -host localhost -port 3235 -log_file /home/misterhouse/data/pocketsphinx/pocketsphinx -sent_file /home/misterhouse/data/pocketsphinx/current.sent -lm_file /home/misterhouse/data/pocketsphinx/current.lm -dict_file /home/misterhouse/data/pocketsphinx/current.dic -hmm_file /usr/local/share/pocketsphinx/model/hmm/wsj1 -program /usr/local/bin/pocketsphinx_continuous -device microphone -sample 16000~);$self->{p_sphinx}->{cmd_index} = q~1~; $self->{p_sphinx}->{output} = q~/home/misterhouse/data/pocketsphinx/pocketsphinx_microphone.stdout~; $self->{p_sphinx}->{errlog} = q~/home/misterhouse/data/pocketsphinx/pocketsphinx_microphone.stderr~; $self->{p_sphinx}->{killsig} = q~TERM~; $self->{p_sphinx}->{started} = q~1205420613~; $self->{p_sphinx}->{pid} = q~32504~; $self->{p_sphinx}->restore_active();#; $pocketsphinx_listener->restore(); } else {'var not used'} Thanks for all you help and suggestions. Jim "Gregg Liming" <gr...@li...> wrote in message news:47D...@li...... > Hi Jim, > > Jim Duda wrote: > >> Is it safe to do this in the contructor?: >> >> $self->{object_name} = '$self'; > > If by "safe"--you mean--throw an error, then yes--it won't immediately > throw an error. But, if you mean "ok for use within mh", then no--it > will cause unexpected problems because '$self' will not resolve to what > you hope. See discussion below. > >> I've seen {object_name} referenced many times in various places, but >> never seen where/how it gets loaded. > > It's in "the big loop". See bin/mh and specifically, read_user_code. > Within read_user_code, all of the user code is scanned and parsed. As > part of that parsing, object names (literal strings) are pulled out. > Then, a call is made to store_object_data (you likely recall that one) > except that the first argument is a string (the object name) vice an > object. And, store_object_data sets $obj->{object_name}. There's a bit > of indirection. Of course, this won't work unless you're in the context > of the user code parser since it's the only thing that knows the actual > object names. That's why separate calls made within constructors to > store_object_names pass the object vice the object name; other code > understands that occasionally object refs get returned from the lookup > tables vice object names. > > I hope the above helps (not sure if I would go as far as "makes sense"). > It's possible that I've missed something useful to you; so, my > suggestion is to take a look. It's also possible that my understanding > of the user code scanning and the equivalent of "auto wire up" is > slightly flawed. > > Gregg > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > ________________________________________________________ > To unsubscribe from this list, go to: http://sourceforge.net/mail/?group_id=1365 > > |