Search code examples
perlperltk

Unable to print arguments in perl tk callback


When submit button is clicked, a callback function should be called. printing all arguments passed during callback doesn't work. But it enters callback function. How can I access passed variables so that I can insert into table.

sub registerStu {
    use DBI;
    use strict;
    my $reg = $mw->Toplevel();
    $reg->title("Registration");
    $reg->geometry("500x500+0+0");
    #$button -> grid(-row=>5, -column=>5);
    my $name = $reg->Label( -text => "Name", -width => 20 )->pack( -side => "top" );
    my $ename = $reg->Entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" );
    my $uid = $reg->Label( -text => "User name", -width => 20 )->pack( -side => "top" );
    my $euid = $reg->Entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" );
    my $pwd = $reg->Label( -text => "Password", -width => 20 )->pack( -side => "top" );
    my $epwd = $reg->Entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" );
    my $mail = $reg->Label( -text => "Email", -width => 20 )->pack( -side => "top" );
    my $email = $reg->Entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" );
    my $dept = $reg->Label( -text => "Department", -width => 20 )->pack( -side => "top" );
    my $edept = $reg->Entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" );
    my $gname = $ename->get();
    my $guid  = $euid->get();
    my $gpwd  = $epwd->get();
    my $gmail = $email->get();
    my $gdept = $edept->get();
    my $submit = $reg->Button(
        -text    => "Register",
        -command => sub { &InsertStu( $gname, $guid, $gpwd, $gmail, $gdept ); }
    )->pack( -side => "top" );
}

sub InsertStu {
    print "hello";
    print "@_\n";
    my $driver   = "mysql";
    my $database = "course";
    my $dsn      = "DBI:$driver:database=$database";
    my $userid   = "root";
    my $password = "pwd";

    my $dbh = DBI->connect( $dsn, $userid, $password, { AutoCommit => 1 } ) or die $DBI::errstr;
    #my $sth = $dbh->prepare("INSERT into student(sid,name,password,email,dept) values('$guid','$gname','$gpwd','$gmail','$gdept')");
    #$sth->execute() or die $DBI::errstr;
}

Solution

  • You're pulling the values of your text fields prematurely.

    # These values aren't set yet, so your callback function will always be
    # passed an empty string for each of these values
    my $gname = $ename->get();
    my $guid  = $euid->get();
    my $gpwd  = $epwd->get();
    my $gmail = $email->get();
    my $gdept = $edept->get();
    

    Instead, pass the text fields to the callback function, so you can then pull the values that are set when the submit button is actually pressed:

    sub registerStu {
        use DBI;
        use strict;
        my $reg = $mw->Toplevel();
        $reg->title("Registration");
        $reg->geometry("500x500+0+0");
        #$button -> grid(-row=>5, -column=>5);
        my $name = $reg->Label( -text => "Name", -width => 20 )->pack( -side => "top" );
        my $ename = $reg->Entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" );
        my $uid = $reg->Label( -text => "User name", -width => 20 )->pack( -side => "top" );
        my $euid = $reg->Entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" );
        my $pwd = $reg->Label( -text => "Password", -width => 20 )->pack( -side => "top" );
        my $epwd = $reg->Entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" );
        my $mail = $reg->Label( -text => "Email", -width => 20 )->pack( -side => "top" );
        my $email = $reg->Entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" );
        my $dept = $reg->Label( -text => "Department", -width => 20 )->pack( -side => "top" );
        my $edept = $reg->Entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" );
        my $submit = $reg->Button(
            -text    => "Register",
            -command => sub { InsertStu( $ename, $euid, $epwd, $email, $edept ); }
        )->pack( -side => "top" );
    }
    
    sub InsertStu {
        my ( $ename, $euid, $epwd, $email, $edept ) = @_;
    
        my $gname = $ename->get();
        my $guid  = $euid->get();
        my $gpwd  = $epwd->get();
        my $gmail = $email->get();
        my $gdept = $edept->get();
    
        my $driver   = "mysql";
        my $database = "course";
        my $dsn      = "DBI:$driver:database=$database";
        my $userid   = "root";
        my $password = "pwd";
    
        my $dbh = DBI->connect( $dsn, $userid, $password, { AutoCommit => 1 } ) or die $DBI::errstr;
        my $sth = $dbh->prepare("INSERT into student(sid,name,password,email,dept) values(?,?,?,?,?)");
        $sth->execute( $guid, $gname, $gpwd, $gmail, $gdept ) or die $DBI::errstr;
    }