Chinaunix首页 | 论坛 | 博客
  • 博客访问: 589935
  • 博文数量: 207
  • 博客积分: 10128
  • 博客等级: 上将
  • 技术积分: 2440
  • 用 户 组: 普通用户
  • 注册时间: 2004-10-10 21:40
文章分类

全部博文(207)

文章存档

2009年(200)

2008年(7)

我的朋友

分类:

2009-03-28 09:08:13

用 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;
    }

}

阅读(767) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~