Is Perlish BEGIN / END possible? 
Author Message
 Is Perlish BEGIN / END possible?

Is it possible to simulate the Perl BEGIN / END blocks in Tcl?

That is, when a program starts it runs the procedure named BEGIN as soon
as possible, and when it exits it runs the procedure named END as late
as possible. Is there a way to get this into the Tcl framework without
hacking at the C level?



Sat, 28 Feb 2004 18:24:37 GMT  
 Is Perlish BEGIN / END possible?

Quote:
>Is it possible to simulate the Perl BEGIN / END blocks in Tcl?

of course ;-)

proc BEGIN body {uplevel #0 $body}
proc END body {rename exit _exit; proc exit {args} $body\n_exit}

BEGIN {
  # put begin stuff here

Quote:
}

END {
  #put end stuff here

Quote:
}

# put remaining stuff here
exit

--
Newsflash: Sproingy made it to the ground !
  read more ... <http://avl.enemy.org/sproingy>



Sat, 28 Feb 2004 20:10:37 GMT  
 Is Perlish BEGIN / END possible?

Quote:

> Is it possible to simulate the Perl BEGIN / END blocks in Tcl?

Tcl's default mode of operation is to behave as if everything was in a
BEGIN block.  You can simulate END by redefining [exit] to include the
behaviour, though if you do you *must* be careful to make sure you
chain to the original [exit] implementation or you will end up with a
process that won't quit of its own accord...

However, in general there is no real way to simulate BEGIN/END as Tcl
just isn't defined that way.  There's no sense of a starting or ending
period; the script runs and then it terminates (or drops into the event
loop if you're running wish.)

You might be able to simulate the behaviour of BEGIN and END blocks
using an extra interp and a redefined [source] command.  Consider it an
(advanced) exercise for the interested reader!

Donal.
--
"While this is anecdotal, and thus bound to be ignored by all the pedanticism-
 fascists out there, I have owned a fully-loaded copy of Emacs and many other
 gnutilities for many years, and have never had a fatality yet."



Sat, 28 Feb 2004 22:38:11 GMT  
 Is Perlish BEGIN / END possible?


Quote:

>> Is it possible to simulate the Perl BEGIN / END blocks in Tcl?

>Tcl's default mode of operation is to behave as if everything was in a
>BEGIN block.  You can simulate END by redefining [exit] to include the
>behaviour, though if you do you *must* be careful to make sure you
>chain to the original [exit] implementation or you will end up with a
>process that won't quit of its own accord...

>However, in general there is no real way to simulate BEGIN/END as Tcl
>just isn't defined that way.  There's no sense of a starting or ending
>period; the script runs and then it terminates (or drops into the event
>loop if you're running wish.)

>You might be able to simulate the behaviour of BEGIN and END blocks
>using an extra interp and a redefined [source] command.  Consider it an
>(advanced) exercise for the interested reader!

                        .
                        .
                        .
Someone with an interest in this area probably will want
to know also about .tclshrc <URL: http://mini.net/tcl/689 >.
--


Business:  http://www.Phaseit.net
Personal:  http://starbase.neosoft.com/~claird/home.html



Sat, 28 Feb 2004 23:24:32 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Cals Begin and End Balance

2. prob. with BEGIN/END

3. Smalltalk on the small end (was: Advice requested: GUI project beginning)

4. /BEGIN/ .. /END/ file reading

5. How to write filesearch with begin-pattern till end-pattern

6. begin/end doesn't behave as expected

7. Braces and begin/end

8. Suggestion for addition to Begin/End syntax

9. Are begin/end blocks atomic?

10. Beginning of End of Ada Mandate

11. remove begin and end tags and its content in between

12. I am thinking of if this is possible

 

 
Powered by phpBB® Forum Software