Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl TK Questions

Status
Not open for further replies.

clueo8

Programmer
Jun 13, 2005
47
US
I'm trying to execute a command prompt statement that sets the tracelevel for my environment:
an Example of what I type in a cmd prompt (dos/unix black screen) is:
Code:
set DBTRACELEVEL=2
I would like perl to be able to issue this command from a TK window. I would set up a button with a command. I've tried many commands like:
Code:
system "set DBTRACELEVEL=2"
exec "set DBTRACELEVEL=2"

But neither of these work correctly. Exec will actually just close my entire perl tk program.

If anyone can shine some light on the subject, It would be greatly appriciated.
 
try
Code:
BEGIN{$ENV{DBTRACELEVEL}=2;}
but remember that this will only affect the current process and subprocesses, not the environment from which you launched your script.

Yours,

f

"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilkes
 
Please show your button code. Maybe you are not calling system correctly from within the TK widget.


Michael Libeson
 
Code:
$trace_mb -> command (-label => 'Set TRACELEVEL = 2',
  -activebackground => 'orange',
-command => sub{ BEGIN{$ENV{DBTRACELEVEL}=2;} } );

Since my program "will only affect the current process and subprocesses, not the environment from which you launched your script" I have just make a local tracevalue... since if I execute the program and then exit... the env trace will not be changed anyways...
 
I {changed directory, modified my environment} in a perl script. How come the
change disappeared when I exited the script? How do I get my changes to be vis
ible?
Unix
In the strictest sense, it can't be done--the script executes as a
different process from the shell it was started from. Changes to a
process are not reflected in its parent--only in any children
created after the change. There is shell magic that may allow you to
fake it by eval()ing the script's output in your shell; check out
the comp.unix.questions FAQ for details.


Michael Libeson
 
putting a BEGIN block in a sub won't do what you want - it gets executed before the rest of the code.
Code:
-command => sub{ $ENV{DBTRACELEVEL}=2;}
will set the environment variable for the local process.

If you want to excute the program and then exit, modifying the environment, your only option is to have your program print shell-specific strings (such as "export DBTRACELEVEL=2" for /bin/sh) and call it with an eval and backticks.
Code:
#!/usr/bin/perl
sub setDBTraceLevel {
   my $level = $_[0];
   foreach ($ENV{SHELL}) {
      /csh/ && do {
          print "setenv DBTRACELEVEL $level\n";
          last;
          }
      /bash|sh/ && do {
          print "export DBTRACELEVEL=$level\n";
          last;
          }
   }
}
...
setDBTraceLevel( 2 );
...
run like this
Code:
~# eval `myscript`

Is this heading in the right direction for you?

f

"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilkes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top