Problem with Perl/Tk

Discussion in 'Perl Misc' started by Orion93, Feb 16, 2004.

  1. Orion93

    Orion93 Guest

    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';
    Orion93, Feb 16, 2004
    #1
    1. Advertising

  2. Orion93

    Ben Morrow Guest

    Orion93 <> wrote:

    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

    --
    'Deserve [death]? I daresay he did. Many live that deserve death. And some die
    that deserve life. Can you give it to them? Then do not be too eager to deal
    out death in judgement. For even the very wise cannot see all ends.'
    :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:
    Ben Morrow, Feb 16, 2004
    #2
    1. Advertising

  3. Orion93

    Orion93 Guest

    Ben Morrow <> wrote in
    news:c0r9gi$mqe$:

    >
    > Orion93 <> wrote:
    >
    > 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
    >


    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!
    Orion93, Feb 17, 2004
    #3
  4. Orion93

    Ben Morrow Guest

    Orion93 <> wrote:
    > 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

    --
    We do not stop playing because we grow old;
    we grow old because we stop playing.
    Ben Morrow, Feb 17, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,773
  2. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    288
    PerlFAQ Server
    Jan 23, 2011
  3. PerlFAQ Server
    Replies:
    0
    Views:
    658
    PerlFAQ Server
    Feb 3, 2011
  4. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Feb 27, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    280
    PerlFAQ Server
    Feb 27, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    664
    PerlFAQ Server
    Apr 4, 2011
Loading...

Share This Page