Operating System - HP-UX
1834029 Members
1911 Online
110063 Solutions
New Discussion

perl - how to use a variable as part of a command

 
SOLVED
Go to solution
John Kittel
Trusted Contributor

perl - how to use a variable as part of a command


I'm trying to do something like this: a variable to hold the character > or < and then use the variable in the find command to control whether it finds mtime + or mtime -

for example:

use File::Find ();
sub find(&@) { &File::Find::find }
*name = *File::Find::name;

$DIRECTION = '>';
find{-f $_ && (int(-M _) $DIRECTION $DAYS) && unshift (@DEVnames,$name)}
"/tmp/";

foreach $FNAME (@DEVnames) {
print $FNAME;
}

But it blows up:

Scalar found where operator expected at ./tdir line 10, near ") $DIRECTION"
(Missing operator before $DIRECTION?)
Scalar found where operator expected at ./tdir line 10, near "$DIRECTION $DAYS"
(Missing operator before $DAYS?)
syntax error at ./tdir line 10, near ") $DIRECTION "
Execution of ./tdir aborted due to compilation errors.


- Thanks,
John
3 REPLIES 3
John Kittel
Trusted Contributor

Re: perl - how to use a variable as part of a command

when I entered the example, I cut and pasted, but I left out where I set a value for $DAYS. Something like this:

$DAYS = 1;

- John
Rodney Hills
Honored Contributor
Solution

Re: perl - how to use a variable as part of a command

Syntactically-
int(-M _) $DIRECTION $DAYS is not valid

You could use "eval"
eval(int(-M _) $DIRECTION $DAYS)
But it would have extra overhead to "compile" this line each time.

You could use "?" operator
$DIRECTION eq ">" ? int(-M _) > $DAYS : int(-M _) < $DAYS

HTH

-- Rod Hills
There be dragons...
John Kittel
Trusted Contributor

Re: perl - how to use a variable as part of a command

Thanks Rod.

(I'm a little surprised there's not a more direct way of doing what I want; seems like with all the cool syntactic features of perl they'd have invented a way to do this...)

- John