You are not logged in.

#1 2010-06-11 16:24:54

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

[solved] writing a simple perl condition

Hey,

I know next to nothing about perl but I've found this handy perl script that uses the accelerometer in my thinkpad to notice movement and then use sox to produce a siren sound as kind of a theft protection.

Now I want to add two simple conditions to the script.
a) check if a process uses /dev/dsp and if so, kill that process and b) check if the kernel module for the accelerometer is loaded and if not, load it.

This is how i do it in bash.
THEFTMODULE=$(lsmod | grep hdaps)
[[ -z $THEFTMODULE ]] && modprobe hdaps && sleep 1

DSPUSED=$(fuser /dev/dsp)
[[ ! -z $DPSUSED ]] && fuser -k /dev/dsp

Could you help me translate that to perl?

This is what i came up with, but it interprets $module just as plain text and doesn't evaluate.

my $module='lsmod | grep hdaps';

if (!$module) {
  print $module;
  system "modprobe hdaps";
}

my $dsp='fuser /dev/dsp';

if (!$dsp) {
  system "fuser -k /dev/dsp";
}

Regards,
demian

Last edited by demian (2010-06-13 18:13:52)


no place like /home
github

Offline

#2 2010-06-11 16:48:25

andresp
Member
Registered: 2010-05-29
Posts: 62

Re: [solved] writing a simple perl condition

Would have to be system("lsmod | grep hdaps")

but:

a. perl has a grep() function
b. the assignment will go to stdout

edit:

That was a bit unfair of me wink

Another hint: try using ` in the assignment, sans system. man perlvar

Last edited by andresp (2010-06-11 17:04:38)

Offline

#3 2010-06-11 20:13:14

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: [solved] writing a simple perl condition

Why are you using perl for this at all? If you're just going to shell out, it doesn't make sense.

#!/bin/bash
lsmod | grep -q hdaps || modprobe hdaps
fuser /dev/dsp && fuser -k /dev/dsp

Offline

#4 2010-06-11 20:54:38

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: [solved] writing a simple perl condition

i am already using bash as a wrapper to do the above and call the perl script.
i just want the perl script to do those two checks for the kernel module and /dev/dsp too, since it needs those to run anway.

i'll check the manual(s) tomorrow, andresp. thanks for the hints.

Last edited by demian (2010-06-11 20:55:40)


no place like /home
github

Offline

#5 2010-06-13 01:50:59

juster
Forum Fellow
Registered: 2008-10-07
Posts: 195

Re: [solved] writing a simple perl condition

andresp wrote:

Another hint: try using ` in the assignment, sans system. man perlvar

Sweet, be condescending and give bad advice...

The perlvar manpage describes the built-in variables that affect perl's behavior.  What you should read, demian, is the perlop manpage which describes perl's operators.  In particular, search for qx/STRING/ under quote-like operators and also lookup I/O Operators which references the qx// operator.

The problem you are having is you are using the ' (single quote) character when you should use the ` (backticks) character.  The ` character is right above tab on US keyboards and to the left of backspace on german keyboards (according to wikipedia).  `` and the qx// operator are one and the same, by the way.  The qx// just lets you use any quoting character.

# Here `lsmod` is in list context and returns the output of lsmod as a list of lines
# grep filters lists, returning only the line that matches the /hdaps/ regexp
unless ( grep { /hdaps/ } `lsmod` ) {
    system 'modprobe', ( 'hdaps' ) and die 'modprobe hdaps failed';
}

if ( `fuser /dev/dsp` ) {
    system 'fuser', ( '-k' => '/dev/dsp' ) and die 'fuser -k /dev/dsp failed';
}

# system returns 0 on success which is the same as false in perl
# which is why we use 'and die ...' to report an error with the system call

Offline

#6 2010-06-13 18:13:29

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: [solved] writing a simple perl condition

Thank you, juster. The wikipedia is right and your code works perfectly.


no place like /home
github

Offline

Board footer

Powered by FluxBB