Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Perl tk - How to integrate external scripts

by Giorgio C (Novice)
on Jan 27, 2012 at 15:28 UTC ( #950387=perlquestion: print w/ replies, xml ) Need Help??
Giorgio C has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, i have a question hoping you can answer me: I have a tk script with 2 Buttons and a Scrolled text windows: 1. Button ("OPEN") 2. Button ("Get Statistics") When i push the "Open" button i choose a .txt file and then the content is printed out in the Scrolled windows. Up to now no problem! The difficulty is this: When i push the "Get Statistic Button" it should call an external Script (that works fine alone) but i wish it worked on the opened file. (opened before with the "Open" button. Can you help me, please ? Here is the tk code and the script i would like to integrate into. The Tk script:
#The Open button: $mw-> Button (-text =>'Open',-command=>\open_file)->place(-x=>240, -y= +>35); sub open_file {my $open = $mw->getOpenFile( -filetypes => $types_OPEN, -defaultextension => '.sff'); $te->Load( "$open");} #The Get Statistic button: $mw-> Button (-text =>'Get Statistics', -command =>\&get_statistics); sub get_statistics {-------Here i need your help----}; # The Scrolled windows: $te = $mw->Scrolled( q/TextUndo/, -scrollbars => 'se', -background.... +.......);
The External (get_statistic.pl) Script (it works fine like: "perl get_statistic.pl filename.txt")
# The get_statistic.pl script: use Statistics::Descriptive; my $stat = Statistics::Descriptive::Full->new(); my (%distrib); my @bins = qw/18 19 20 21 22 23 24 25 26 27 28/; my $fastaFile = shift; open (FASTA, "<$fastaFile"); $/ = ">"; my $junkFirstOne = <FASTA>; while (<FASTA>) { chomp; my ($def,@seqlines) = split /\n/, $_; my $seq = join '', @seqlines; $stat->add_data(length($seq));} %distrib = $stat->frequency_distribution(\@bins); print "Total reads:\t" . $stat->count() . "\n"; print "Total nt:\t" . $stat->sum() . "\n"; print "Mean length:\t" . $stat->mean() . "\n"; print "Median length:\t" . $stat->median() . "\n"; print "Mode length:\t" . $stat->mode() . "\n"; print "Max length:\t" . $stat->max() . "\n"; print "Min length:\t" . $stat->min() . "\n"; print "Length\t# Seqs\n"; foreach (sort {$a <=> $b} keys %distrib) { print "$_\t$distrib{$_}\n"; }
Now i would like to run this script on the $open file and see the result in a dialog box windows. Please help !!! Thank you very much as always.

Comment on Perl tk - How to integrate external scripts
Select or Download Code
Replies are listed 'Best First'.
Re: Perl tk - How to integrate external scripts
by chessgui (Scribe) on Jan 27, 2012 at 15:55 UTC
    Why not just copy paste it and call it as a sub?
      Yes i try to do it, but give me "uninitialized value" errors and doesnt' work, because i'm unable to integrate variables inside. Have you idea on how to do it ?

        You are going to need the pass the file name to the get_statistics subroutine when you call it, which means you probably are going to need to save it in a globally accessible variable. You really should also be checking for definedness / existence of the file and success on open too. Also good practice to use lexical file handles and 3 argument opens. (Not critical but they can help avoid subtle bugs.)

        my $current_file;
        $mw-> Button (-text =>'Get Statistics', -command =>\&get_statistics($current_file));
        ## Fixed as pointed out by Eliya++ below.
        $mw-> Button (-text =>'Get Statistics', -command => sub{ get_statistic +s($current_file)}); sub open_file { my $open = $mw->getOpenFile( -filetypes => $types_OPEN, -defaultextension => '.sff' ); if (defined $open and -e $open){ $te->Load( "$open"); $current_file = $open; } sub get_statistics { # blah blah my $fastaFile = shift; unless (defined $fastaFile){ warn "No file name supplied.\n"; return; } open my $FASTA, '<', $fastaFile or warn "Couldn't open file: $!\n"; # blah blah while (<$FASTA>) { # blah blah
        };
        Just leave it alone, run it separately as a system command and redirect its output to a file:
        system('perl.exe yourstatisticalroutine.pl inputfile.name >outputfile. +name');
        (Since this is a system command you have to find out the proper format of that on your system. This code is tested on Win32.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2015-07-29 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls