in reply to Re^5: Perl tk - How to integrate external scripts
in thread Perl tk - How to integrate external scripts

Thank you very much for your answer, i've tried with your advices but it still doesn't work. Can you correct here the script to make it works ? It's clear i'm a newbie.
my $open; $mw-> Button (-text =>'Open', -command =>\&open_file)->place(-x=>240, +-y=>35); $mw-> Button (-text =>'Get Statistics',-command =>[\&get_statistics, $ +open])->place(-x=>320, -y=>35); sub open_file {$open = $mw->getOpenFile( -filetypes => $types_OPEN, -defaultextension => '.sff'); $te->Load( "$open");} if (defined $open and -e $open){ $te->Load( "$open"); $fastaFile = $open; } sub get_statistics {my $stat = Statistics::Descriptive::Full->new(); my (%distrib); my @bins = qw/18 19 20 21 22 23 24 25 26 27 28/; my $fastaFile = $open; unless (defined $fastaFile){ warn "No file name supplied.\n"; return; } open my $FASTA, '<', $fastaFile or warn "Couldn't open file: $!\n"; 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"; }};

Replies are listed 'Best First'.
Re^7: Perl tk - How to integrate external scripts
by Eliya (Vicar) on Jan 31, 2012 at 12:26 UTC

    Well, in what way does it "not work"?

    Here's a minimal version that works in the sense that when you first click "Open" and select a file, and then click "Get Statistics", the latter routine prints the selected file name to the console.  And if it can print the file name, it should also be able to do something else with it.

    #!/usr/bin/perl -w use strict; use Tk; my $open; # declare global variable, my $mw = MainWindow->new(); $mw-> Button( -text =>'Open', -command => \&open_file )->pac +k(); $mw-> Button( -text =>'Get Statistics', -command => \&get_statistics ) +->pack(); MainLoop(); sub open_file { $open = $mw->getOpenFile(); # set it, } sub get_statistics { my $fastafile = $open; # and use it. print STDERR "selected file: $fastafile\n"; }
      It doesn't work in the sense that the global $open varibale is not initialized and not recognized. In fact your script works fine alone but when i do the same in my script (your simply version) it doesn't print out the name of the file: "Use of uninitialized value $fastafile in concatenation (.) or string at Desktop/script_perl/MIRNAWdelta.pl line 171. selected file: " (blank). Probably the problem is that the windows where are all the buttons considered since now are a new windows (soubbpoutin of a initial Get->start button) I mean:
      # Entry Window $init = my $mw = new MainWindow; $label = $mw -> Label(...) # The START button: my $button = $mw -> Button(...,-command=>\push_button); + sub push_button { my $mw = new MainWindow; $mw -> geometry ("1200x600"); # Menu bar $mw->configure(-menu => my $menubar = $mw->Menu, -background => 'white'); my $file = $menubar->cascade(-label => '~File'); my $edit = $menubar->cascade(-label => '~Edit'); my $help = $menubar->cascade(-label => '~Help'); my $open; $mw-> Button (-text =>'Open', -command =>\&open_file)->place(-x=>240, +-y=>35); $mw-> Button (-text =>'Get Statistics', -command =>\&get_statistics)-> +place(-x=>320, -y=>35); MainLoop; sub open_file {$open = $mw->getOpenFile( -filetypes => $types_OPEN, -defaultextension => '.sff'); $te->Load( "$open");} sub get_statistics { my $fastafile= $open; print STDERR "selected file: $fastafile\n"; }; $te = $mw->Scrolled( .....); }
      Maybe this the problem ?

        Always use strict and warnings.  With that, you'd have gotten warnings like Variable "$open" will not stay shared, which might have been a hint that there's still something wrong with the scoping of the variable $open.

        The problem is in particular that you've declared $open locally to the callback function push_button, and that you've declared the other callback functions locally to that function (which I had only realized after having indented your code reasonably...).   Perl does not have local subroutines, and if you declare named subs within other subs, they're essentially parsed as if they were declared top-level. Hence the scope issue.

        Here's a stripped down version (with "minor" edits to make it actually runnable...), which uses real global variables for $open and $te (for which the same issues apply), so that they can be shared across various callback routines.

        #!/usr/bin/perl -w use strict; use Tk; my $open; my $te; # Entry Window my $mw = new MainWindow; # The START button: my $button = $mw -> Button(-text => "Start",-command=>\&push_button)-> +pack(); MainLoop; sub push_button { my $mw = new MainWindow; $mw -> geometry ("1200x600"); # Menu bar $mw->configure(-menu => my $menubar = $mw->Menu, -background => 'white'); my $file = $menubar->cascade(-label => '~File'); my $edit = $menubar->cascade(-label => '~Edit'); my $help = $menubar->cascade(-label => '~Help'); $mw-> Button (-text =>'Open', -command =>\&open_file)->place(-x=>2 +40, -y=>35); $mw-> Button (-text =>'Get Statistics', -command =>\&get_statistic +s)->place(-x=>320, -y=>35); $te = $mw->Scrolled('TextUndo')->place(-x=>240, -y=>70); MainLoop; } sub open_file { $open = $mw->getOpenFile( #-filetypes => $types_OPEN, -defaultextension => '.sff' ); $te->Load($open); } sub get_statistics { my $fastafile= $open; print STDERR "selected file: $fastafile\n"; }

        A few more comments:

        • Indenting your code in some sane way already gets you a long way to better understanding it.
        • Always try to provide code samples that actually run. That way you significantly increase the chances of people looking into your problem. In fact, I only went to the trouble of editing your sample code because I just happened to have a few idle minutes.
        • I think (but I'm no Tk expert) that having another MainLoop within a callback function is somewhat "unusual" style.
        • And, at the risk of repeating myself: use strictures/warnings — they have been invented to help you.