我们可以在 Perl 中同时运行两个非嵌套循环吗? [英] Can we run two simultaneous non-nested loops in Perl?

查看:34
本文介绍了我们可以在 Perl 中同时运行两个非嵌套循环吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的部分代码是这样的:

Part of my code goes like this:

while(1){
        my $winmm = new Win32::MediaPlayer;   
        $winmm->load('1.mp3'); $winmm->play; $winmm->volume(100);
        Do Some Stuff;
        last if some condition is met;
    }

问题是:当我在 while 循环中的 Do Some Stuff 阶段时,我希望音乐始终打开.但是音乐的长度太短以至于在我进入下一阶段之前它会完全停止,所以我希望音乐自我重复,但是Win32::Mediaplayer模块似乎没有重复模式,所以我正在考虑为音乐播放部分做一个无限循环.像这样:

Problem is: I want the music to be always on when I'm in the Do Some Stuff stage in the while loop. But the length of the music is so short that it will come to a full stop before I go to the next stage, so I want the music to repeat itself, but the Win32::Mediaplayer module does not seem to have a repeat mode, so I'm thinking of doing an infinite loop for the music playing part. Like this:

while(1){
 my $winmm = new Win32::MediaPlayer;   
 $winmm->load('1.mp3'); $winmm->play; $winmm->volume(100);
}
while(2){
Do some stuff;
last if some condition is met
}

但是根据我目前的 Perl 知识,如果我在 while(1) 部分,我永远无法进入 while(2) 部分.即使涉及到嵌套循环,在进入外循环的另一部分之前,我也必须做一些事情来跳出内循环.

But based on my current Perl knowledge if I'm in the while(1) part, I can never go to the while(2) part. Even if it comes to a nested loop, I have to do something to break out of the inside loop before going to the other part of the outside loop.

我的问题的答案我们可以在 Perl 中同时运行两个非嵌套循环吗?"可能不是,但我认为有一些方法可以处理这种情况.如我错了请纠正我.

The answer to my question "Can we run two simultaneous non-nested loops in Perl?" may be a NO, but I assume there is some way of handling such situation. Correct me if I'm wrong.

一如既往地感谢您的任何评论/建议:)

Thanks as always for any comments/suggestions :)

更新

非常感谢大家的帮助.谢谢:) 所以我的问题的答案是肯定的,而不是否定的.我很高兴我学会了如何使用 fork() 和线程来解决实际问题:)

I really appreciate the help from everyone. Thanks :) So the answer to my question is a YES, not a NO. I'm happy that I've learned how to use fork() and threads to solve a real problem :)

推荐答案

您可以在不同的线程中运行两个不同的进程.

You can run the two different processes in separate threads.

大致如下:

use strict;
use warnings;
use threads;
use threads::shared;
use Win32::MediaPlayer;


my $killAudio :shared = undef;
my $audio = threads->create(&playAudio);
my $condition = threads->create(&doSomething,$audio);
$condition->join();

sub playAudio {

    my $winmm = new Win32::MediaPlayer;
    $winmm->load('1.mp3') or die 'Could not load file: $!';
    $winmm->volume(100);
    $winmm->play until $killAudio;
}

sub doSomething {
    my $thread = shift;
    my $conditionMet = undef;

    while (1) {
        ($conditionMet,$killAudio) = doSomeStuff();    # set doSomeStuff() to
                                                       # return only when 
                                                       # conditions are met

        $thread->join() if $killAudio;        # This line will terminate $audio
        last if $conditionMet;
    }
}

更新

根据 Mike 在下面的评论,playAudio() 子程序可以重写为:

Based on Mike's comment below, the playAudio() subroutine can be rewritten as:

sub playAudio {
    my $winmm = new Win32::MediaPlayer;
    $winmm->load('1.mp3') or die 'Could not load file: $!';
    while (1) {
        $winmm->play;
        $winmm->volume(100);
        sleep($winmm->length/1000);
        last if $killAudio;
    }
}

这篇关于我们可以在 Perl 中同时运行两个非嵌套循环吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆