Hi, I am beginner perl input I just want to know if I want to make a program that let user input a function such as input " < value " List all files that are less than value The current directory is used. eg input " < 5" list all file that less than 5 in current directory until user input "quit"
how can I get sperate value from < in user input
so here my code for user input two sperate input in once line
#!/usr/bin/perl -w
print " Welcome to advance search function";
$func = "";
while (( $func ne "quit\n" ) && ( $func2 = "exit\n" ))
{
print " Enter advance search function :";
$func = <STDIN>; $func2 = <STDIN>;
}
print " return to main menu \n";
$
$
$ # list the name and sizes of all text files in the current directory
$
$ wc *.txt
0 0 0 a.txt
1 1 3 b.txt
1 1 5 c.txt
1 11 51 d.txt
3 13 59 total
$
$
$ # display the contents of the Perl program
$
$ cat -n search.pl
1 #perl -w
2 print "Enter expression: "; # prompt for user input
3 chomp ($input = <STDIN>); # assign input to $input
4 while ($input ne "quit") { # loop until user enters "quit"
5 print "Expression to test: Filesize $input\n"; # print message
6 print "It is TRUE for the following files:\n\n"; # print message
7 foreach $file (glob "*.txt") { # loop through all text files in current dir
8 $size = -s $file; # find out the size of current file in loop
9 $expr = "-s \"$file\" $input"; # cook up the expression for testing filesize
10 $output = eval $expr; # evaluate the expression
11 die "Oops, something went wrong: $@" if $@; # halt the program if something went wrong
12 if ($output) { # otherwise
13 print "File Name = $file; File size = $size\n"; # print the file name if the expression
14 } # could be evaluated successfully
15 } # finish looping through all files
16 print "\nEnter expression: "; # so prompt the user again
17 chomp ($input = <STDIN>); # and set the input to $input
18 }
$
$
$ # Now run the Perl program
$
$ perl search.pl
Enter expression: < 5
Expression to test: Filesize < 5
It is TRUE for the following files:
File Name = a.txt; File size = 0
File Name = b.txt; File size = 3
Enter expression: >= 0
Expression to test: Filesize >= 0
It is TRUE for the following files:
File Name = a.txt; File size = 0
File Name = b.txt; File size = 3
File Name = c.txt; File size = 5
File Name = d.txt; File size = 51
Enter expression: > 0
Expression to test: Filesize > 0
It is TRUE for the following files:
File Name = b.txt; File size = 3
File Name = c.txt; File size = 5
File Name = d.txt; File size = 51
Enter expression: != 5
Expression to test: Filesize != 5
It is TRUE for the following files:
File Name = a.txt; File size = 0
File Name = b.txt; File size = 3
File Name = d.txt; File size = 51
Enter expression: < 0
Expression to test: Filesize < 0
It is TRUE for the following files:
Enter expression: > 1000
Expression to test: Filesize > 1000
It is TRUE for the following files:
Enter expression: quit
$
$ # Another trial run
$
$ perl search.pl
Enter expression: < 50
Expression to test: Filesize < 50
It is TRUE for the following files:
File Name = a.txt; File size = 0
File Name = b.txt; File size = 3
File Name = c.txt; File size = 5
Enter expression: blah
Expression to test: Filesize blah
It is TRUE for the following files:
Bareword found where operator expected at (eval 10) line 1, near ""a.txt" blah"
(Missing operator before blah?)
Oops, something went wrong: syntax error at (eval 10) line 2, near ""a.txt" blah
"
$
$
$