可以使用switch / case或given / when来模拟这个Perl行为吗?

时间:2009-05-21 02:39:18

标签: perl

我想知道是否有人提出改进以下代码的建议(如果可能的话)以便它不需要重复(我的@a = $ time =〜...),可能使用case / switch或给出/何时或其他一些我不知道的想法?

my $time = '12:59pm';

if( my @a = $time =~ m/^(\d\d?)(am|pm)$/ )        { tell_time( $a[0], 0, $a[1] ) }
if( my @a = $time =~ m/^(\d\d?):(\d\d)(am|pm)$/ ) { tell_time( @a ) }
if( my @a = $time =~ m/^(\d\d?):(\d\d)$/ )        { tell_time( @a ) }

sub tell_time
{
    my $hour    = shift;
    my $minute  = shift || '00';
    my $ampm    = shift || ( $hour > 12 ) ? 'pm' : 'am';

    print "Hour: $hour, Minute: $minute, AMPM: $ampm\n";
}

我尝试过使用Switch和5.10给出/什么时但似乎无法做到这样的事情:

given( $time )
{
    when( /^(\d\d?)(am|pm)$/ )        { tell_time( $_[0], 0, $_[1] ) }
    when( /^(\d\d?):(\d\d)(am|pm)$/ ) { tell_time( @_ ) }
    when( /^(\d\d?):(\d\d)$/ )        { tell_time( @_ ) }
}

由于@_似乎存储了$ time。

还要注意我对问题的语法比代码解决的问题更感兴趣。我很清楚我可以使用Time :: ParseDate来找出格式化为时间或日期的字符串的各个部分。

6 个答案:

答案 0 :(得分:10)

您的正则表达式使用()来提取匹配项,但您不必将它们存储在数组中。如果需要,它们会存储在$1$2$3中,依此类推。 Lookie:

given( $time )
{
    when( /^(\d\d?)(am|pm)$/ )        { tell_time( $1, 0, $2 ) }
    when( /^(\d\d?):(\d\d)(am|pm)$/ ) { tell_time( $1, $2, $3 ) }
    when( /^(\d\d?):(\d\d)$/ )        { tell_time( $1, $2 ) }
}

我认为你想做什么。

如果你想添加语法,我会写tell_time()来简单地把时间作为一个字符串,并让函数解析结果本身,而不是让你的代码的用户自己解析它。或者,您可以使用此given()块作为执行此功能的新函数的开头 - 解析时间字符串并将其正确传递给tell_time()。但那只是我。我不知道你需要你的代码做什么,所以一定要去做。

答案 1 :(得分:1)

好吧,如果不使用开关/外壳,我只需使用一个正则表达式捕获所有变量...

#!/usr/bin/perl

tell_time ("12:59am");    # matches time format 1
tell_time ("2:59pm");     # matches time format 1
tell_time ("12am");       # matches time format 2
tell_time ("12:59");      # matches time format 3
tell_time ("14:59");      # matches time format 3
tell_time ("12:59:59am"); # produces no output, does not match any known time formats.

sub tell_time
{
    my $timearg = shift;

    # note: (?: ... ) creates a non-capturing group, which is not reflected in 
    # the returned array.
    my ($hour , $minute, $ampm) = ( $timearg =~ m/^(\d\d?)(?::(\d\d?))?(am|pm)?$/ ) ;

    # only continue if we captured all required fields (i.e. hour)
    if($hour)
    {
        # set default values for optional fields (i.e. minute, ampm) if necessary
        $minute ||=  '00';
        $ampm ||=  ( $hour > 12 ) ? 'pm' : 'am';

        print "Hour: $hour, Minute: $minute, AMPM: $ampm\n";
    }

}

如果有必要,我可以进一步解释,但我想如果你能阅读perl它应该清楚它在做什么......

答案 2 :(得分:1)

由于您使用的是5.10,因此您可以在正则表达式中使用named captures

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

my $hour24   = qr/(?<hour>[1-9]|1[0-9]|2[0-3])/;
my $hour12   = qr/(?<hour>[1-9]|1[0-2])/;
my $minute   = qr/(?<minute>[0-5][0-9])/;
my $meridiem = qr/(?<meridiem>am|AM|pm|PM)/;

for my $time (qw(5pm 10am 5:59pm 10:00pm 5:00 22:00 24:00)) {
    given($time) {
        when(/ ^ $hour12 $meridiem $ /x) { 
            my $hour = $+{hour};
            $hour += 12 if 'pm' eq lc $+{meridiem};
            tell_time($hour, "00") 
        }
        when(/ ^ $hour12 : $minute $meridiem $ /x) { 
            my $hour = $+{hour};
            $hour += 12 if 'pm' eq lc $+{meridiem};
            tell_time($hour, $+{minute}) 
        }
        when(/ ^ $hour24 : $minute $ /x) { 
            tell_time($+{hour}, $+{minute}) 
        }
        default {
            say "bad time: $time";
        }
    }
}

sub tell_time {
    my ($hour, $minute) = @_;
    say "it is $hour:$minute";
}

答案 3 :(得分:0)

Chris Lutz已经使用Perl 5.10覆盖了切换语法。在Perl的版本中,您可以使用循环别名来模拟一个:

for ($time) {
  /^(\d\d?)(am|pm)$/        && do { tell_time( $1, 0, $2 );  last };
  /^(\d\d?):(\d\d)(am|pm)$/ && do { tell_time( $1, $2, $3 ); last };
  /^(\d\d?):(\d\d)$/        && do { tell_time( $1, $2 );     last };
}

答案 4 :(得分:0)

我不确定给定/何时方面是否重要。我只想在一个正则表达式中组合可能的模式。结合特殊变量%+和defined-or运算符,我们可以使代码更简洁。

#!/usr/bin/perl

use strict;
use warnings;

my @times = qw( 12:59pm 12 1pm 13:11 11 11pm);

my $hour_pat   = '(?<hour>[0-9]{1,2})';
my $minute_pat = '(?<minute>[0-9]{2})';
my $ampm_pat   = '(?<ampm>am|pm)';

my $re = qr{
    \A
    (?:$hour_pat : $minute_pat $ampm_pat)
    |
    (?:$hour_pat : $minute_pat)
    |
    (?:$hour_pat $ampm_pat)
    |
    (?:$hour_pat)
    \z
}x;

for my $time ( @times ) {
    if ( $time =~ $re ) {
        tell_time( %+ );
    }
}

sub tell_time {
    my %time = @_;
    printf( "Hour: %2.2d, Minute: %2.2d, AMPM: %s\n",
        $time{hour},
        $time{minute} // 0,
        $time{ampm} // ( $time{hour} >= 12 ? 'pm' : 'am' ),
    );
    return;
}

答案 5 :(得分:0)

我使用块标签创建开关,如下所示:

my $time = '12:59pm';
SWITCH: {
    $time =~ /^(\d\d?)(am|pm)$/ && do { 
        tell_time($1,0,$2);
        last SWITCH;
    };
    $time =~ /^(\d\d?):(\d\d)(am|pm)$/ && do {
        tell_time($1,$2,$3);
        last SWITCH;
    };
    $time =~ /^(\d\d?):(\d\d)$/ && do {
        tell_time($1,$2);
    };
}