Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Method for sharing variables

by diggernz (Sexton)
on Oct 04, 2005 at 02:59 UTC ( [id://497119]=perlquestion: print w/replies, xml ) Need Help??

diggernz has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a tk inteface to allow users to run a number of different scripts. I am wanting to beable to share certain global variables between the interface and the various scripts (eg file directory, save as etc etc). I have looked at writing my own modules, includes and passing data to the command line. What would be a good method to achieve this goal. Your wisdom and comments are always welcome. Thank you

Replies are listed 'Best First'.
Re: Method for sharing variables
by BrowserUk (Patriarch) on Oct 04, 2005 at 03:25 UTC
    I am wanting to beable to share certain global variables between the interface and the various scripts...

    I think you'll need to clarify what you mean by that?

  • How are you running the "various scripts"?

    Is it your intention that these will be separate scripts invoked through system, backticks or similar means?

  • When you say "share", do you mean mono-directionally between the parent (tk) script and a the subordinate scripts?

    If so, passing the value in via the command line involation would work. Or you could set them into %ENV prior to the invokation and they will be available in the called scripts %ENV.

    Return values from invoked scripts could be retrieved using backticks.

    There are several other interpretations of you question ranging from invoking the "scripts" via do, where they would have automatic access to any existing global data within the calling script, to writing your sub-scripts as proper modules.

    Your clarification will probably elicit better answers.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      Recently I wrote a module ( with plenty of help from monks ) that read in a file that created variables consistently for each user. I created a file that has var=value pairs and set them each time a script was run. $ENV{'variable'} is your friend.
Re: Method for sharing variables
by GrandFather (Saint) on Oct 04, 2005 at 03:23 UTC

    If what you mean is you wish to share configuration information, then take a look at XML::Simple. If you mean something else you should show us some code so we understand the issue.


    Perl is Huffman encoded by design.
Re: Method for sharing variables
by bioMan (Beadle) on Oct 04, 2005 at 03:19 UTC

    Being a lowly acolyte I'm leary to post as I'm sure wiser voices will post much sounder advice. However, as you are creating your variables (for example $foo) in the Main namespace, you can refer to those variables in any block by using Main::$foo.

    Mike

      you can refer to those variables in any block by using Main::$foo

      Well, you mean $main::foo and you probably mean "package" rather than "block." It's doubtful this is going to help the OP in any case because he said he was going to be running other scripts from his frontend.

      -sauoq
      "My two cents aren't worth a dime.";
      
Re: Method for sharing variables
by zentara (Archbishop) on Oct 04, 2005 at 13:32 UTC
    It would be very easy for you share globals in your script, as others have pointed out. For simplicity, I would use Tk::ExecuteCommand to do the actual running of the commands. You can import your globals into it, and it forks off the commands automatically behind the scenes, so it won't block your gui. Of course, you can use threads, IPC::Open3, or piped opens also, but Tk::ExecuteCommand is pretty easy. I think it uses IPC::Open3 internally, so it may not work well on all win32 versions, but you didn't say what platform you are on.
    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::ExecuteCommand; my $global = 'foobar'; my $mw = MainWindow->new; my $ec_dir = $mw->ExecuteCommand( -command => "echo $global; dir; sleep 5; dir;", -entryWidth => 50, -height => 10, -label => '', -text => 'Execute dir ', )->pack; my $ec_date = $mw->ExecuteCommand( -command => "echo $global; date; sleep 5; date;", -entryWidth => 50, -height => 10, -label => '', -text => 'Execute date ', )->pack; my $dir_but = $mw->Button( -text => 'Execute dir', -background => 'hotpink', -command => sub{ $ec_dir->execute_command })->pack; my $date_but = $mw->Button( -text => 'Execute date', -background => 'lightgreen', -command => sub{ $ec_date->execute_command })->pack; MainLoop;
    Also, you can popup the execution widows, or hide them, depending on your needs. Tk::Execute::Command is good with subwidgets, so you can read the output textbox of each command, or change their colors, etc. The following example demonstrates that.
    #!/usr/bin/perl -w use Tk; use Tk::ExecuteCommand; use strict; my $mw = MainWindow->new; #create and hide toplevel############ my $tl = $mw->Toplevel(); $tl->withdraw; my $snaptext = $tl->Scrolled('Text', -background=>'lightsteelblue', -foreground=>'black', )->pack(); $tl->Button(-text =>'Close', -command => sub{$tl->withdraw })->pack(); ####################################### my $ec = $mw->ExecuteCommand( -command => '', -entryWidth => 50, -height => 10, -label => '', -text => 'Execute', )->pack; my $dtext = $ec->Subwidget('text'); $dtext->configure( -background => 'black', -foreground => 'yellow', ); $mw->Button(-text => "Snap Shot", -command => sub{ my $time = localtime; my $old_y_view = $dtext->index('end'); print "$old_y_view\n"; my $gettext = $dtext->get('end -10 lines', 'end'); print "gettext->$gettext\n"; $snaptext->insert('end',"\n##$time\n$gettext\n###end $tim +e\n"); $snaptext->see('end'); $tl->deiconify; })->pack(); $ec->configure(-command => './test'); $ec->execute_command; MainLoop;

    I'm not really a human, but I play one on earth. flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://497119]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-18 04:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found