Dear Monks,

I'm confused here! I'm expecting this to continously cycle from main input to sub inputs with alarm timeouts on each cycle. What did I miss?

Also, is there an automatic way to export a method list from a class to a sub class? I know you can use $objref->can("test method name") to test if a method exists. Thanks for any help!

package Enginesub; use strict; use warnings; sub new { my ($class, %arg) = @_; my $objref = { _name => $arg{name} || "unknown" +, }; bless $objref, $class; return $objref; } sub name { return $_[0]->{_name}; } sub go { my $self = shift @_; $SIG{ALRM} = \&ti; eval { alarm(2); print "name ", $self->name, " (2 seconds) sub input: "; chomp(my $input = <STDIN>); alarm(0); }; if ($@ =~ /TIMEOUT/) { print "\n\nno input running defaults.....\n\n"; } sub ti { die "TIMEOUT"; } } 1; package Engine; use Enginesub; use strict; use warnings; sub new { my $class = shift; my $objref = { }; $objref->{OBJECTS} = []; bless $objref, $class; return $objref; } sub createobj { my $self = shift @_; my $aref = $self->{OBJECTS}; push @$aref, Enginesub->new(name => "name1"); push @$aref, Enginesub->new(name => "name2"); } sub run { my $self = shift @_; my $aref = $self->{OBJECTS}; foreach(@$aref) { $_->go(); } } 1; #!/usr/bin/perl use strict; use warnings; use Engine; my $engine = Engine->new(); $engine->createobj(); $SIG{ALRM} = \&timed_out; while() { eval { alarm(5); print "\n\n5 seconds main input: "; chomp(my $input = <STDIN>); alarm(0); }; if ($@ =~ /BLAH/) { print "\n\nno input running....\n\n"; $engine->run(); } sub timed_out { die "BLAH"; } }

./run_program

5 seconds main input:

no input running....

name name1 (2 seconds) sub input:

no input running defaults.....

name name2 (2 seconds) sub input:

no input running defaults.....

5 seconds main input:

5 seconds main input:

What happened to my sub inputs?

READMORE tags added by Arunbear


In reply to Perl OO, input alarm timeout by yoda54

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.