Sunday, 15 January 2012

perltk - Unable to print arguments in perl tk callback -



perltk - Unable to print arguments in perl tk callback -

when submit button clicked, callback function should called. printing arguments passed during callback doesn't work. enters callback function. how can access passed variables can insert table.

sub registerstu { utilize dbi; utilize strict; $reg = $mw->toplevel(); $reg->title("registration"); $reg->geometry("500x500+0+0"); #$button -> grid(-row=>5, -column=>5); $name = $reg->label( -text => "name", -width => 20 )->pack( -side => "top" ); $ename = $reg->entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" ); $uid = $reg->label( -text => "user name", -width => 20 )->pack( -side => "top" ); $euid = $reg->entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" ); $pwd = $reg->label( -text => "password", -width => 20 )->pack( -side => "top" ); $epwd = $reg->entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" ); $mail = $reg->label( -text => "email", -width => 20 )->pack( -side => "top" ); $email = $reg->entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" ); $dept = $reg->label( -text => "department", -width => 20 )->pack( -side => "top" ); $edept = $reg->entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" ); $gname = $ename->get(); $guid = $euid->get(); $gpwd = $epwd->get(); $gmail = $email->get(); $gdept = $edept->get(); $submit = $reg->button( -text => "register", -command => sub { &insertstu( $gname, $guid, $gpwd, $gmail, $gdept ); } )->pack( -side => "top" ); } sub insertstu { print "hello"; print "@_\n"; $driver = "mysql"; $database = "course"; $dsn = "dbi:$driver:database=$database"; $userid = "root"; $password = "pwd"; $dbh = dbi->connect( $dsn, $userid, $password, { autocommit => 1 } ) or die $dbi::errstr; #my $sth = $dbh->prepare("insert student(sid,name,password,email,dept) values('$guid','$gname','$gpwd','$gmail','$gdept')"); #$sth->execute() or die $dbi::errstr; }

you're pulling values of text fields prematurely.

# these values aren't set yet, callback function # passed empty string each of these values $gname = $ename->get(); $guid = $euid->get(); $gpwd = $epwd->get(); $gmail = $email->get(); $gdept = $edept->get();

instead, pass text fields callback function, can pull values set when submit button pressed:

sub registerstu { utilize dbi; utilize strict; $reg = $mw->toplevel(); $reg->title("registration"); $reg->geometry("500x500+0+0"); #$button -> grid(-row=>5, -column=>5); $name = $reg->label( -text => "name", -width => 20 )->pack( -side => "top" ); $ename = $reg->entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" ); $uid = $reg->label( -text => "user name", -width => 20 )->pack( -side => "top" ); $euid = $reg->entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" ); $pwd = $reg->label( -text => "password", -width => 20 )->pack( -side => "top" ); $epwd = $reg->entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" ); $mail = $reg->label( -text => "email", -width => 20 )->pack( -side => "top" ); $email = $reg->entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" ); $dept = $reg->label( -text => "department", -width => 20 )->pack( -side => "top" ); $edept = $reg->entry( -width => 20, -background => 'white', -foreground => 'black' )->pack( -side => "top" ); $submit = $reg->button( -text => "register", -command => sub { insertstu( $ename, $euid, $epwd, $email, $edept ); } )->pack( -side => "top" ); } sub insertstu { ( $ename, $euid, $epwd, $email, $edept ) = @_; $gname = $ename->get(); $guid = $euid->get(); $gpwd = $epwd->get(); $gmail = $email->get(); $gdept = $edept->get(); $driver = "mysql"; $database = "course"; $dsn = "dbi:$driver:database=$database"; $userid = "root"; $password = "pwd"; $dbh = dbi->connect( $dsn, $userid, $password, { autocommit => 1 } ) or die $dbi::errstr; $sth = $dbh->prepare("insert student(sid,name,password,email,dept) values(?,?,?,?,?)"); $sth->execute( $guid, $gname, $gpwd, $gmail, $gdept ) or die $dbi::errstr; }

perl perltk

No comments:

Post a Comment