Restarting a Program mid stream

P

pgodfrin

Greetings,
I have a perl program that has multiple steps. I'd like to be able to
restart it at any step. This works:

#!/bin/perl
goto $ARGV[0]; # forgive me
S1:
{
print "S1\n";
exit;
}
S2:
{
print "S2\n";
exit;
}

However, I was wondering if there is any programmatic access to the
name of the block. That way I could print the name of the block
instead of hard-coding an arbitrary value (print "S1\n")
pg
 
T

Tim Greer

pgodfrin said:
Greetings,
I have a perl program that has multiple steps. I'd like to be able to
restart it at any step. This works:

#!/bin/perl
goto $ARGV[0]; # forgive me
S1:
{
print "S1\n";
exit;
}
S2:
{
print "S2\n";
exit;
}

However, I was wondering if there is any programmatic access to the
name of the block. That way I could print the name of the block
instead of hard-coding an arbitrary value (print "S1\n")
pg

The name of the block is what you're printing, so you could just use
print $ARGV[0] instead, unless I misunderstood what you were asking?
 
J

Jürgen Exner

pgodfrin said:
I have a perl program that has multiple steps. I'd like to be able to
restart it at any step.

To restart a program again just exec() itself.
That will replace the existing instance with a fresh instance.

jue
 
U

Uri Guttman

p> Greetings,
p> I have a perl program that has multiple steps. I'd like to be able to
p> restart it at any step. This works:

p> #!/bin/perl
p> goto $ARGV[0]; # forgive me

but never do that again!

p> S1:
p> {
p> print "S1\n";
p> exit;
p> }
p> S2:
p> {
p> print "S2\n";
p> exit;
p> }

p> However, I was wondering if there is any programmatic access to the
p> name of the block. That way I could print the name of the block
p> instead of hard-coding an arbitrary value (print "S1\n")
p> pg

the blocks could be code references instead and you can make a dispatch
table to run any given step from the command line. but i sense you want
something that will track where you are and restart the process there
the next time it is run.

you need to do several things but nothing too hard.

you need to save in a file (or a db) a marker for when you finish any
given step and that is the name (label/dispatch table key) of the next
step.

then when you start the program it reads in that saved step name and
jumps to it. the problems now are coding series of steps with each one
knowing the name of the next one. this is easy for a handful of steps
but it gets massively painful and annoying as it grows in size and
complexity. it is a basic statemachine and they are not meant to be
directly coded when big.

so you will have to invest in something else. possible there are modules
that can do this too.

uri
 
P

pgodfrin

  p> Greetings,
  p> I have a perl program that has multiple steps. I'd like to be ableto
  p> restart it at any step. This works:

  p> #!/bin/perl
  p> goto $ARGV[0]; # forgive me

but never do that again!

  p> S1:
  p> {
  p>     print "S1\n";
  p>     exit;
  p> }
  p> S2:
  p> {
  p>     print "S2\n";
  p>     exit;
  p> }

  p> However, I was wondering if there is any programmatic access to the
  p> name of the block. That way I could print the name of the block
  p> instead of hard-coding an arbitrary value (print "S1\n")
  p> pg

the blocks could be code references instead and you can make a dispatch
table to run any given step from the command line. but i sense you want
something that will track where you are and restart the process there
the next time it is run.

you need to do several things but nothing too hard.

you need to save in a file (or a db) a marker for when you finish any
given step and that is the name (label/dispatch table key) of the next
step.

then when you start the program it reads in that saved step name and
jumps to it. the problems now are coding series of steps with each one
knowing the name of the next one. this is easy for a handful of steps
but it gets massively painful and annoying as it grows in size and
complexity. it is a basic statemachine and they are not meant to be
directly coded when big.

so you will have to invest in something else. possible there are modules
that can do this too.

uri

--
Uri Guttman  ------  (e-mail address removed)  --------  http://www.sysarch.com--
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training ---http://perlhunter.com/college.html---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com---------

In this case your first guess was correct. I want a dispatch table so
I can pass a step name from the command line for restart. Now I need
to figure out how to make a dispatch table :)
pg
 
P

pgodfrin

pgodfrin said:
Greetings,
I have a perl program that has multiple steps. I'd like to be able to
restart it at any step. This works:
#!/bin/perl
goto $ARGV[0]; # forgive me
S1:
{
    print "S1\n";
    exit;
}
S2:
{
    print "S2\n";
    exit;
}
However, I was wondering if there is any programmatic access to the
name of the block. That way I could print the name of the block
instead of hard-coding an arbitrary value (print "S1\n")
pg

The name of the block is what you're printing, so you could just use
print $ARGV[0] instead, unless I misunderstood what you were asking?
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!

I guess I could have been clearer. In this case since I have an input
argument then I have access to the label name, so that would work as
you say. I may have been over thinking the problem, if the intent is
to restart at a particular step then the step name will be passed via
argument and the program will have access to the step name.

So - perhaps I won't need this information to make the code do what I
want to, but now I'm curious. Essentially the interpreter needs to
know what label to go to, so somewhere internal to perl that
information is available, perhaps just not available to the humble
programmer <grin>
pg
 
X

xhoster

pgodfrin said:
Greetings,
I have a perl program that has multiple steps. I'd like to be able to
restart it at any step.

I'd generally implement this as several different scripts, with one
meta script (Perl or shell) that invokes them in turn.

This works:

#!/bin/perl
goto $ARGV[0]; # forgive me
S1:
{
print "S1\n";
exit;
}
S2:
{
print "S2\n";
exit;
}

However, I was wondering if there is any programmatic access to the
name of the block. That way I could print the name of the block
instead of hard-coding an arbitrary value (print "S1\n")

I can't think of a way that is not a major re-org of your code, or
worse yet a source filter.

Since the name of the block is an arbitrary value to start with,
including that arbitrary value in one other place "by hand" doesn't
seem like too much of a burden. I would think it less of a sin than
using goto in the first place.

Out of curiosity, how to do you plan to pass data between blocks? How will
you know if a block completed successfully or not? If S1 starts but then
dies before finishing, is it safe to restart on S2?

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
P

pgodfrin

pgodfrin said:
Greetings,
I have a perl program that has multiple steps. I'd like to be able to
restart it at any step.

I'd generally implement this as several different scripts, with one
meta script (Perl or shell) that invokes them in turn.


This works:
#!/bin/perl
goto $ARGV[0]; # forgive me
S1:
{
    print "S1\n";
    exit;
}
S2:
{
    print "S2\n";
    exit;
}
However, I was wondering if there is any programmatic access to the
name of the block. That way I could print the name of the block
instead of hard-coding an arbitrary value (print "S1\n")

I can't think of a way that is not a major re-org of your code, or
worse yet a source filter.

Since the name of the block is an arbitrary value to start with,
including that arbitrary value in one other place "by hand" doesn't
seem like too much of a burden.  I would think it less of a sin than
using goto in the first place.

Out of curiosity, how to do you plan to pass data between blocks?  How will
you know if a block completed successfully or not?  If S1 starts but then
dies before finishing, is it safe to restart on S2?

Xho

--
--------------------http://NewsReader.Com/--------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Hi Xho,
Always nice to hear from you. And as always your advice is well
received.
This code won't have data to pass between blocks - it's basically a
way to codify a procedure that is currently run manually. So the
"instructions" say: Change "this" to "that", save file and execute.
All I'm doing is wrapping it in a perl program.

Good point, if S1 fails, is it safe to run S2? The assumption is the
"user" will make whatever corrections to complete S1 and then manually
restart the procedure at S2. And as you correctly note, the label name
is arbitrary and certainly less sinful than using goto to begin with.

Ultimately, I will simply use getopts to accept a step name and then
restart the procedure at that step in a caveat emptor way of running.

And a dispatch table is not what I need for this...
pg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top