Perl“如果”陈述

发布于 2024-12-04 21:43:59 字数 287 浏览 0 评论 0原文

现在,我有以下 Perl 代码

my $tmpl1="download1_video.html"
 if $file->{file_name}=~/\.(avi|divx|mkv|flv|mp4|wmv)$/i;
$tmpl1||="download1.html";

,因此它会检查该文件是否是视频,如果是,则将其定向到特定页面。尽管我只是想知道如何在其中添加另一个 if 语句来检查扩展名是否为 .mp3,如果是,则将其定向到 download1_audio.html

Right now, I have the following Perl code

my $tmpl1="download1_video.html"
 if $file->{file_name}=~/\.(avi|divx|mkv|flv|mp4|wmv)$/i;
$tmpl1||="download1.html";

so it's checking to see if the file is a video, and if so it directs it to the certain page. Although I'm just wondering how I can add another if statement in there to check if the extension is .mp3, and if so direct it to download1_audio.html.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

似狗非友 2024-12-11 21:43:59
if ( $file->{file_name} =~  m/\.(avi|divx|mkv|flv|mp4|wmv)$/i ){
     ## Download video
}
elsif($file->{file_name} =~  m/\.(mp3)$/i){
     ## Download Audio
}

这是您所需要的吗?

if ( $file->{file_name} =~  m/\.(avi|divx|mkv|flv|mp4|wmv)$/i ){
     ## Download video
}
elsif($file->{file_name} =~  m/\.(mp3)$/i){
     ## Download Audio
}

Is this what you needed ?

メ斷腸人バ 2024-12-11 21:43:59
if ($file->{file_name} =~ /\.(avi|divx|mkv|flv|mp4|mp3|wmv)$/i )
{
    if ($1 eq "mp3")
    {
        # mp3 stuff
    }
    elsif ($1 eq "mp4")
    {
       # mp4 stuff
    }
    else
    {
       # all other file types
    }
}
else
{
    # It didn't match
}

一种更奇特的方法是预先创建一个由文件类型键入的哈希值,其中包含下一页所需的信息;我猜的文件名?

my %pageHash = ( "mp3" => "mp3Page.html", "divx" => "divxPage.html", ... );
...
$file->{file_name} =~ /\.(.*)$/i;
if (exists $pageHash{$1})
{
     $page = $pageHash{$1};
}
else
{
     # unknown file extension 
}
if ($file->{file_name} =~ /\.(avi|divx|mkv|flv|mp4|mp3|wmv)$/i )
{
    if ($1 eq "mp3")
    {
        # mp3 stuff
    }
    elsif ($1 eq "mp4")
    {
       # mp4 stuff
    }
    else
    {
       # all other file types
    }
}
else
{
    # It didn't match
}

A fancier way would be to create a hash keyed by your file types in advance with the info you needed for your next page; the filename I guess?

my %pageHash = ( "mp3" => "mp3Page.html", "divx" => "divxPage.html", ... );
...
$file->{file_name} =~ /\.(.*)$/i;
if (exists $pageHash{$1})
{
     $page = $pageHash{$1};
}
else
{
     # unknown file extension 
}
南城旧梦 2024-12-11 21:43:59

刚刚被这个问题困扰,我必须建议您不要使用条件修饰符声明变量。如果条件不成立,它将运行另一个子句的no部分,这意味着您没有声明$tmpl1,但是因为它已经通过了严格,它允许您分配到内存中未定义的位置。

有一种更安全的方法可以完成您的前任在这里所做的事情,这仍然可以说明解决方案。

my $tmpl1
    = $file->{file_name} =~ /\.(avi|divx|mkv|flv|mp4|wmv)$/i 
        ? 'download1_video.html'
        : $file->{file_name} =~ m/\.mp3$/i
            ? 'download1_audio.html'
            : 'download1.html'
    ;

因此,

  1. $tmpl1 始终被声明
  2. $tmpl1 始终被赋值

Having just been burnt by this, I must advise you against declaring a variable with a conditional modifier. If the condition does not hold true, it runs no part of the other clause, which means that you are not declaring $tmpl1, but since it's already passed strict, it allows you to assign to an undefined position in memory.

There is a safer way to do what your predecessor is doing here, that can yet illustrate a solution.

my $tmpl1
    = $file->{file_name} =~ /\.(avi|divx|mkv|flv|mp4|wmv)$/i 
        ? 'download1_video.html'
        : $file->{file_name} =~ m/\.mp3$/i
            ? 'download1_audio.html'
            : 'download1.html'
    ;

Thus,

  1. $tmpl1 is always declared
  2. $tmpl1 is always assigned a value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文