Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional Branching in Perl?

Status
Not open for further replies.

AlbertG

Industrial
Aug 9, 2005
42
Greetings!

Well, I dug myself in over my head with a basic syntax debacle in Perl for my Linux box.

Here's the code sample which I wish to fix:

Perl:
#!/usr/bin/perl

$value = '$(pgrep some-process | wc) -gt 0';

TEST:
if ($value)
{
  sleep 2;
  zenity --warning --text="some-process is running.";
  goto STANDBY;
}
else
{
  goto TEST;
}

STANDBY:
if ($value)
{
  goto STANDBY;
}
else
{
  goto TEST;
}

Simply, this code is to lurk in the background until "some-process" is running on the system, pop a warning box at that point, stand by after the user clicks "OK", and then lurk again until the next round.

All "goto" style matters aside, where might I have missed the mark syntaxwise in this rustic example? Of course, any help is thoroughly appreciated...


Thanks so much!
 
Replies continue below

Recommended for you

Addendum:

Considering pgrep a little more, would something like this work just as well (or better):

Perl:
$value = '$(pgrep some-process > /dev/null)';

???
 
What problem are you having?

One problem might be that the loop:

STANDBY:
if ($value)
{
goto STANDBY
}

is infinite once $value is true. You probably want to re-evaluate $value once per loop (with some appropriate sleep to slow things down).

But I'm just guessing. Again, what's the problem you're having?
 
@LiteYear:

Thanks for stopping by! Sorry to be away so long.

Evaluating the $value variable was part of the problem. As a throwback from my OO days, I thought the variable would be reevaluated without direct placement. Thanks for the heads up.

Here's one back!

Perl:
'$(pgrep some-process | wc) -gt 0'

from the OP code should read

Perl:
`$(pgrep some-process | wc) -gt 0`

to enable that code segment to pass through Perl and execute as bash. Life is full of these cute little things...

While I've got you here, how does one express a value representing a fraction of a second in Perl's sleep method? Couldn't find anything which made ready sense...


Thanks again, and have a great weekend ;)



 
From the manual:

man perlfunc said:
For delays of finer granularity than one second, the Time::HiRes
module (from CPAN, and starting from Perl 5.8 part of the standard
distribution) provides usleep(). You may also use Perl's four-
argument version of select() leaving the first three arguments
undefined, or you might be able to use the "syscall" interface to
access setitimer(2) if your system supports it. See perlfaq8 for
details.

Best you have a look there first so we don't end up duplicating the manual in this forum.
 
LiteYear said:
From the manual... Best you have a look there first so we don't end up duplicating the manual in this forum.

To varying degrees of context/scope, this could be said of most queries posted throughout Eng-Tips.

Thank you for sharing your expertise.

Good day.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor