#!/usr/bin/perl use warnings; use strict; use Math::BigInt; use Tk; sub factorial { my ($n) = @_; return 1 if $n == 0; no warnings 'recursion'; return factorial($n-1) * $n } my $mw = MainWindow->new(-title => 'Factorial Calculator'); my $result; my $value; my $v = $mw->Entry( -width => 6, -relief => 'sunken', -textvariable => \$value, ); my $calculate = sub { # Stringify to prevent the conversion to int or float! $result = "" . factorial(Math::BigInt->new($value)); }; $v->bind('<Return>', $calculate); my $desc = $mw->Label(-text => 'Factorial is:',); my $r = $mw->Label(-textvariable => \$result,); my $calc = $mw->Button(-text => 'Calculate', -command => $calculate); my $q = $mw->Button(-text => 'QUIT', -command => sub { $mw->DESTROY + },); Tk::grid($v, $desc, $r, -padx => 10, -pady => 10); Tk::grid($calc, '-', '-', -padx => 10, -pady => 5); Tk::grid($q, '-', '-', -padx => 10, -pady => 5); Tk::MainLoop();
It happily computes and displays 100!.
In reply to Re: Perl/Tk int overflow in Label widget
by choroba
in thread Perl/Tk int overflow in Label widget
by perlboy_emeritus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |