Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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

by Eliya (Vicar)
on Jan 30, 2012 at 12:19 UTC ( #950759=note: print w/replies, xml ) Need Help??

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

As has already been mentioned, your problem is that $open is scoped locally to the sub open_file. This doesn't work, because that way the variable is not accessible outside of the sub (when you click the Get Statistics button).

Make it a global variable

my $open; # variable with file scope # The sub open : sub open_file {$open = $mw->getOpenFile( # no my here! ...

and then simply use the variable inside of get_statistics:

sub get_statistics { ... my $fastaFile = $open; ...

Alternatively, use the variable $current_file.  You do have assigned $open to it, but you are not using it anywhere...

sub get_statistics { ... my $fastaFile = $current_file; ...

Replies are listed 'Best First'.
Re^6: Perl tk - How to integrate external scripts
by Giorgio C (Novice) on Jan 31, 2012 at 10:26 UTC
    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"; }};

      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/ 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 ?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://950759]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2018-05-24 01:30 GMT
Find Nodes?
    Voting Booth?