×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Conditional Branching in Perl?

Conditional Branching in Perl?

Conditional Branching in Perl?

(OP)
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:

CODE --> 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!

RE: Conditional Branching in Perl?

(OP)
Addendum:

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

CODE --> Perl

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

???

RE: Conditional Branching in Perl?

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?

RE: Conditional Branching in Perl?

(OP)
@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!

CODE --> Perl

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

from the OP code should read

CODE --> 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 ;)



RE: Conditional Branching in Perl?

From the manual:

Quote (man perlfunc)

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.

RE: Conditional Branching in Perl?

(OP)

Quote (LiteYear)

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources