文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>用 Perl 生成随机密码 - Win32::GUI 版

用 Perl 生成随机密码 - Win32::GUI 版

时间:2009-03-28  来源:cobrawgl

用 perl 生成随机密码是很简单的,只要几行 code 就可以了,这里主要是加上了 GUI 界面,并且用了新的 Coding Style :) 本来还想用 Moose 的,一犯懒就没动,以后试试。 perl 传递函数的方式让我很不爽,不知道 perl6 里会怎么样 。。。  

use strict;
use warnings;

Class_Main: {
    MyApp->new->run;
}

Class_MyApp: {
    package MyApp;
    use Win32::GUI qw( MB_ICONINFORMATION MB_OK );;

    sub new {
        my $class = shift;
        my $self = {@_};

        bless $self, $class;

        $self->interface;

        return $self;
    }

    sub run {
        my $self = shift;

        $self->{win}->Show();
        Win32::GUI::Dialog();

    }

    sub display_password {
        my $self = shift;

        use String::MkPasswd qw(mkpasswd);

        my $passwd = mkpasswd(
            -length => 9,
            -minnum => 2,
            -minlower => 2, # minlower is increased if necessary

            -minupper => 2,
            -minspecial => 2,
            -distribute => 1,
        );

        $self->{win}->MessageBox(
            $passwd,
            "您的密码",
         MB_ICONINFORMATION | MB_OK,
        );
    }

    sub display_help {
        my $self = shift;

        my $message = "密码长度:您需要的密码位数。默认是9位。\r\n" .
                    "数字个数:您的密码中最少有几个数字。默认最少2个。\r\n" .
                    "小写字母:您的密码中最少有几个小写字母。默认最少2个。\r\n" .
                    "大写字母:您的密码中最少有几个大写字母。默认最少2个。\r\n" .
                    "特殊符号:您的密码中最少有几个特殊符号。默认最少1个。\r\n" .
                    "键盘分布:如果选'是',密码会均匀分布。默认为否。\r\n";

        $self->{win}->MessageBox(
            $message,
            "帮助",
         MB_ICONINFORMATION | MB_OK,
        );

    }

    sub interface {

        my $self = shift;

        $self->{win} = Win32::GUI::Window->new(
                        -name => 'Main',
                        -width => 200,
                        -height => 200,
                        -text => '随机密码生成器',
                );

        $self->{win}->AddButton(
                -name => "Button_Generate",
                -text => "生成",
                -pos => [ 30, 140 ],
                -onClick => sub { $self->display_password;},
                );

        $self->{win}->AddButton(
                -name => "Button_Exit",
                -text => "退出",
                -pos => [ 80, 140 ],
                -onClick => sub { return -1; },
                );

        $self->{win}->AddButton(
                -name => "Button_Help",
                -text => "帮助",
                -pos => [ 130, 140 ],
                -onClick => sub { $self->display_help; },
                );

        $self->{win}->AddTextfield(
                -align => 'right',
                -name => "length",
                -left => 80,
                -top => 5,
                -width => 80,
                -height => 20,
                -prompt => "密码长度:",
                -number => 1,
                -text    => 9,
            );

        $self->{win}->AddTextfield(
                -align => 'right',
                -name => "minnum",
                -left => 80,
                -top => 25,
                -width => 80,
                -height => 20,
                -prompt => "数字个数:",
                -number => 1,
                -text    => 2,
            );

        $self->{win}->AddTextfield(
                -align => 'right',
                -name => "minlower",
                -left => 80,
                -top => 45,
                -width => 80,
                -height => 20,
                -prompt => "小写字母:",
                -number => 1,
                -text    => 2,
            );

        $self->{win}->AddTextfield(
                -align => 'right',
                -name => "minupper",
                -left => 80,
                -top => 65,
                -width => 80,
                -height => 20,
                -prompt => "大写字母:",
                -number => 1,
                -text    => 2,
            );

        $self->{win}->AddTextfield(
                -align => 'right',
                -name => "minspecial",
                -left => 80,
                -top => 85,
                -width => 80,
                -height => 20,
                -prompt => "特殊符号:",
                -number => 1,
                -text    => 1,
            );

        $self->{win}->AddLabel(
              -text => "键盘分布:",
              -pos => [21, 112],
            );

        $self->{win}->AddRadioButton(
              -name => "distribute_yes",
              -text => '是',
              -pos => [85, 110],
              -size => [50, 20],
              -checked => 0
            );
        $self->{win}->AddRadioButton(
              -name => "distribute_no",
              -text => '否',
              -pos => [125, 110],
              -size => [50, 20],
              -tabstop => 1,
              -checked => 1,
            );

        return $self;
    }

}

相关阅读 更多 +
排行榜 更多 +
坦克战争世界

坦克战争世界

模拟经营 下载
丛林反击战

丛林反击战

飞行射击 下载
几何飞行安卓版

几何飞行安卓版

飞行射击 下载