Restarting a Program mid stream

Discussion in 'Perl Misc' started by pgodfrin, Nov 21, 2008.

  1. pgodfrin

    pgodfrin Guest

    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
     
    pgodfrin, Nov 21, 2008
    #1
    1. Advertising

  2. pgodfrin

    Tim Greer Guest

    pgodfrin wrote:

    > 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!
     
    Tim Greer, Nov 22, 2008
    #2
    1. Advertising

  3. pgodfrin <> wrote:
    >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
     
    Jürgen Exner, Nov 22, 2008
    #3
  4. pgodfrin

    Uri Guttman Guest

    >>>>> "p" == pgodfrin <> writes:

    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

    --
    Uri Guttman ------ -------- 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 ---------
     
    Uri Guttman, Nov 22, 2008
    #4
  5. pgodfrin

    pgodfrin Guest

    On Nov 21, 11:20 pm, Uri Guttman <> wrote:
    > >>>>> "p" == pgodfrin  <> writes:

    >
    >   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  ------    --------  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
     
    pgodfrin, Nov 23, 2008
    #5
  6. pgodfrin

    pgodfrin Guest

    On Nov 21, 6:08 pm, Tim Greer <> wrote:
    > pgodfrin wrote:
    > > 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
     
    pgodfrin, Nov 23, 2008
    #6
  7. pgodfrin

    Guest

    pgodfrin <> wrote:
    > 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.
     
    , Nov 23, 2008
    #7
  8. pgodfrin

    pgodfrin Guest

    On Nov 23, 3:40 pm, wrote:
    > pgodfrin<> wrote:
    > > 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
     
    pgodfrin, Nov 25, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MarkD
    Replies:
    0
    Views:
    746
    MarkD
    Dec 4, 2003
  2. bruce barker
    Replies:
    2
    Views:
    367
    bruce barker
    Aug 23, 2004
  3. Geoldr

    Restarting a program

    Geoldr, May 22, 2008, in forum: Python
    Replies:
    4
    Views:
    265
    Geoldr
    May 22, 2008
  4. kevin donne

    Restarting my program

    kevin donne, Feb 1, 2011, in forum: C Programming
    Replies:
    14
    Views:
    543
    Turtle Wizard
    Feb 1, 2012
  5. Peace
    Replies:
    3
    Views:
    84
    dieter
    Mar 22, 2014
Loading...

Share This Page