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

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

问题描述

我的部分代码如下:

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天全站免登陆