Problem with Perl/Tk

O

Orion93

Hi!

I try to make an interface for this script in Perl/Tk but it's the first
time i try to do it and i don't know how to use my variable in the sub. The
script below doesn't work and i don't know why! Please, i need help!

Thanks

use Tk;

$main = MainWindow -> new;
$main->title("Test 1");
$libelF=$main->Label(-text=>'Chemin:')->pack();
$montantF->Entry(-textvariable=>\$nomFic)->pack(-padx=>5);
$valid=$main->Button(-text=>'Ok',-command=>\&recupPages)->pack(-
side=>'left', expand=>1);
$end=$main->Button(-text=>'Fermer',-command=>sub {exit})->pack(-
side=>'right', expand=>1);
MainLoop();

sub recupPages
{
my $rep= $montantF->get();
my $result = shift;
open(F,'$nomFic');
open(SORTIE,'$result');
$i = 0;
while(<F>)
{
$i ++;
}
print SORTIE " $nomFic $i\n";
close F;
close SORTIE;
}
my $emplacement = $nomFic;
my $ficResultat = "e:\\result.txt";
recupPages($_, $ficResultat) for glob '$nomFic';
 
B

Ben Morrow

use strict;
use warnings;
use Tk;

$main = MainWindow -> new;

my $main = ...;
$main->title("Test 1");
$libelF=$main->Label(-text=>'Chemin:')->pack();

my $libelF = ...;

&c.
$montantF->Entry(-textvariable=>\$nomFic)->pack(-padx=>5);
$valid=$main->Button(-text=>'Ok',-command=>\&recupPages)->pack(-
side=>'left', expand=>1);

It would be a lot easier to see what is going on if you put some
whitespace in here; also, you never set $montantF:

my $nomFic;
my $montantF = $main->Entry (-textvariable => \$nomFic)
->pack (-padx => 5);
my $valid = $main->Button(-text => 'Ok', -command => \&recupPages)
->pack (-side => 'left', -expand => 1);

As an aside, I *really* hate apps that call buttons 'Ok': not only is it
inconsistent with every OS I've ever used, it's also Wrong. It's an
abbreviation, so it's spelt 'OK'.
$end=$main->Button(-text=>'Fermer',-command=>sub {exit})->pack(-
side=>'right', expand=>1);
MainLoop();

sub recupPages
{
my $rep= $montantF->get();
my $result = shift;
open(F,'$nomFic');
open(SORTIE,'$result');

Use lexical filehandles: they close automatically, which makes your life
easier. Check the return value of open: yes, *every* time. Those single
quotes won't interpolate, so you're trying to open a file called
'$nomFic'. You don't need quotes at all.

open my $F, $nomFic or die "can't open $nomFic: $!";
open my $SORTIE, $result or die "can't open $result: $!";
$i = 0;
while(<F>)
{
$i ++;
}
print SORTIE " $nomFic $i\n";
close F;
close SORTIE;
}

Sort out your indentation: it makes things much easier:

sub recupPages {
my $rep = $montantF->get();
my $result = shift;

I'm not sure what you think this does, but I doubt it's what you mean.
Do you not just mean
my $result = $montantF->get();
? Or, indeed, just use $nomFic, since you've set that up to contain the
value of the entry box... no, hang on, $nomFic is the input file. Where
do you want the name of the output file to come from?

open my $F...
...
while (<$F>) {
$i++;
}

Or, neater:
$i++ while <$F>;

Or use $. instead of $i:
1 while <$F>;
print $SORTIE " $nomFic $.\n";
See perldoc perlvar.

print $SORTIE " $nomFic $i\n";
# no need to close the FHs: they will close at the end of the scope.
}
my $emplacement = $nomFic;
my $ficResultat = "e:\\result.txt";
recupPages($_, $ficResultat) for glob '$nomFic';

I'm not sure when you want this to execute, but as things stand it
won't, ever. MainLoop never returns, so Perl will never get here. If you
want it to be executed when the OK button is pressed, it needs to go
inside recupPages; if you want it to be executed at the end of the
program (ie. when the Fermer button is pressed) it needs to go in an END
block:

END {
my $emplacement = $nomFic; # why? you never use this variable.
my $ficResultat = 'e:/result.txt'; # yes, use / even on win32

recupPages($_, $ficResultat) for glob $nomFic;
# again, the '' quotes won't interpolate the variable.
}

I get the feeling you're not entirely clear about what you want this
program to do... or, at any rate, *I'm* not.

Ben
 
O

Orion93

use strict;
use warnings;


my $main = ...;


my $libelF = ...;

&c.


It would be a lot easier to see what is going on if you put some
whitespace in here; also, you never set $montantF:

my $nomFic;
my $montantF = $main->Entry (-textvariable => \$nomFic)
->pack (-padx => 5);
my $valid = $main->Button(-text => 'Ok', -command => \&recupPages)
->pack (-side => 'left', -expand => 1);

As an aside, I *really* hate apps that call buttons 'Ok': not only is
it inconsistent with every OS I've ever used, it's also Wrong. It's an
abbreviation, so it's spelt 'OK'.


Use lexical filehandles: they close automatically, which makes your
life easier. Check the return value of open: yes, *every* time. Those
single quotes won't interpolate, so you're trying to open a file
called '$nomFic'. You don't need quotes at all.

open my $F, $nomFic or die "can't open $nomFic: $!";
open my $SORTIE, $result or die "can't open $result: $!";


Sort out your indentation: it makes things much easier:

sub recupPages {
my $rep = $montantF->get();
my $result = shift;

I'm not sure what you think this does, but I doubt it's what you mean.
Do you not just mean
my $result = $montantF->get();
? Or, indeed, just use $nomFic, since you've set that up to contain
the value of the entry box... no, hang on, $nomFic is the input file.
Where do you want the name of the output file to come from?

open my $F...
...
while (<$F>) {
$i++;
}

Or, neater:
$i++ while <$F>;

Or use $. instead of $i:
1 while <$F>;
print $SORTIE " $nomFic $.\n";
See perldoc perlvar.

print $SORTIE " $nomFic $i\n";
# no need to close the FHs: they will close at the end of the
scope.
}


I'm not sure when you want this to execute, but as things stand it
won't, ever. MainLoop never returns, so Perl will never get here. If
you want it to be executed when the OK button is pressed, it needs to
go inside recupPages; if you want it to be executed at the end of the
program (ie. when the Fermer button is pressed) it needs to go in an
END block:

END {
my $emplacement = $nomFic; # why? you never use this
variable. my $ficResultat = 'e:/result.txt'; # yes, use / even on
win32

recupPages($_, $ficResultat) for glob $nomFic;
# again, the '' quotes won't interpolate the variable.
}

I get the feeling you're not entirely clear about what you want this
program to do... or, at any rate, *I'm* not.

Ben

From the script below I try to make an interface in Perl/Tk to be able to
choose the repertory directly. The initial script count lines for all pl
files in one directory.

Initial Script:
#!/usr/bin/perl
sub recupPages
{
my $nomFic = shift;
my $result = shift;

# Récupération des lignes du fichier
open(F,$nomFic);
open(SORTIE,">> $result");
$i = 0;
while(<F>)
{
$i ++;
}
print SORTIE " $nomFic $i\n";

close F;
close SORTIE;}

my $emplacement = "e:\\Dossier_travail\\stat\\";
my $ficResultat = "e:\\result.txt";
recupPages($_, $ficResultat) for glob 'e:\\Dossier_travail\\stat\\*.pl';


Current script:

#!/usr/bin/perl
use strict;
use warnings;
use Tk;
use diagnostics;

my $main = MainWindow -> new;
$main -> title("Test 1");
$main -> Label(-text=>"Chemin:")->pack();
$main -> Entry()->pack(-padx=>5);
$main->Button(-text=>'Ok',-command=>\my $recupPages)->pack(-side=>'left',
expand=>1);
$main->Button(-text=>"Fermer",-command=>sub {exit;})->pack(-side=>'right',
expand=>1);
MainLoop();

sub recupPages
{
# my $rep= my $resupPages->get();
my $result = shift;
# Récupération des lignes du fichier
open (F,"$recupPages");
open (SORTIE,">> my $result");
my $i = 0;
while(<F>)
{
$i ++;
}
print SORTIE " my $recupPages $i\n";
close F;
close SORTIE;
}
#my $emplacement = @rep;
my $ficResultat = "e:\\result.txt";
recupPages($_, $ficResultat) for glob '$nomFic\*.pl';

I don't understand where are my errors! Please Help me again!
 
B

Ben Morrow

Orion93 said:
From the script below I try to make an interface in Perl/Tk to be able to
choose the repertory directly. The initial script count lines for all pl
files in one directory.

Initial Script:
#!/usr/bin/perl
sub recupPages
{

SORT OUT YOUR INDENTING.
my $nomFic = shift;
my $result = shift;

# Récupération des lignes du fichier
open(F,$nomFic);
open(SORTIE,">> $result");

There's no point opening this file anew every time: open it once, and
pass a filehandle in.
$i = 0;
while(<F>)
{
$i ++;
}
print SORTIE " $nomFic $i\n";

close F;
close SORTIE;}

my $emplacement = "e:\\Dossier_travail\\stat\\";
my $ficResultat = "e:\\result.txt";
recupPages($_, $ficResultat) for glob 'e:\\Dossier_travail\\stat\\*.pl';

Current script:

#!/usr/bin/perl
use strict;
use warnings;
use Tk;
use diagnostics;

my $main = MainWindow -> new;
$main -> title("Test 1");
$main -> Label(-text=>"Chemin:")->pack();
$main -> Entry()->pack(-padx=>5);

The original script had two parameters: $emplacement and $ficResultat.
This box has only one text entry: which of the two parameters is it
supposed to set?
$main->Button(-text=>'Ok',-command=>\my $recupPages)->pack(-side=>'left',
^^^^^^^^^^^^^^^^
Might I ask what you *thought* that would do? You can't just make shit
up and expect the computer to understand. The -command option takes a
reference to a sub: what you have there is a reference to a scalar
variable.

I would suggest that you probably want to leave the sub recupPages as it
is (well, except for fixing the indentation, using lexical FHs and not
reopening the file every time :), and write a new sub (to be given as
the -command parameter) that extracts the required data from the form,
does the glob and calls recupPages on the results.
expand=>1);
$main->Button(-text=>"Fermer",-command=>sub {exit;})->pack(-side=>'right',
expand=>1);
MainLoop();

sub recupPages
{
# my $rep= my $resupPages->get();
my $result = shift;
# Récupération des lignes du fichier
open (F,"$recupPages");

What are you thinking here? I just don't understand. What value were you
expecting to be in the variable $recupPages?
open (SORTIE,">> my $result");
my $i = 0;
while(<F>)
{
$i ++;
}
print SORTIE " my $recupPages $i\n";
close F;
close SORTIE;
}
#my $emplacement = @rep;
my $ficResultat = "e:\\result.txt";
recupPages($_, $ficResultat) for glob '$nomFic\*.pl';

Again: when did you think this code would be executed? It seems fairly
clear now that you want it to execute when the OK button is pressed; but
for that to happen it would have to be in or called from the sub you
passed as the -command parameter to that button.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top