Perl Regular expression

I have a perl script that I want to  take 2 options just like Unix grep

-v negates the match; print only lines that do not match the regex
-i makes it case sensitive; match regardless of case

Either or both options can be present or not, in any order, but if present, must occur before the rest of the arguments.
For example: .
/ -v perl ./*.pl
or ./ -i -v Test ./*.test.

I have tried the attached script, however it is not working properly.

Can anyone give me the correct script or modify existing one to make it work.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

What does -i patteren -v patteren mean?
clock_crazyAuthor Commented:
I have provided examples in commented part ( IN script )

#Example: ./ -v perl ./*.pl
 #Example: ./ -i -v Test ./*.test.

-v pattern : negates the match; print only lines that do not match the pattern ( e.g perl or any word )
-i : should not have any pattern; makes it case sensitive; match regardless of case

( Just like Unix grep command with grep -v and grep -i )
Assuming that you have only one pattern and that the -i and -v take no arguments
so that -i patteren -v patteren filename would mean that -v patteren filename are all considered to be file names
use strict;
use warnings;
use Getopt::Std;
use vars qw( $opt_v $opt_i );

#my %opts;

if( !getopts('vi') )
  die "Usage: [-iv] patteren filename\n";
  #Example: ./ -v perl ./*.pl
  #Example: ./ -i -v Test ./*.test.

my $regex = shift;
while( <> ){
    print if /$opt_i$regex/ ^ !!$opt_v;

Open in new window

Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

clock_crazyAuthor Commented:
would you explain the code to me;

my $regex = shift;                                 # Take Argument from command prompt
($opt_i&&='(?i)')||='';                            #  ??  ( assigning default value of ' ' to $opt_i  && ??????????
while( <> ){                                             #  read default $_
    print if /$opt_i$regex/ ^ !!$opt_v;          # ????????????????????

Also, what was wrong about my script ?
clock_crazyAuthor Commented:
Can you give me a script that anyone can understand ?
($opt_i&&='(?i)')||=''; is equivalent to $opt_i = $opt_i?'(?i)':'';
!!$opt_v is like ($opt_v||'') in that it does not throw a Use of uninitialized value warning when $opt_v is undefined
what was wrong about my script
When $opt_v is set, it ignores $opt_i, and when neither is set, it acts as if  $opt_v was set
It also gives a misleading Usage: message
clock_crazyAuthor Commented:
I am going to accept your solution and take a bow !!!!

I have last question: can you please explain $opt_i = $opt_i?'(?i)':'' , is this combination of ternary operator and positive look ahead expression  and how come caret ( ^ ) symbol before !!$opt_v ?
$opt_i = $opt_i?'(?i)':''
if( $opt_i ){
   $opt_i = '(?i)';
    $opt_i = '';
when used in /$opt_i$regex/, (?i) means do case-insensitive pattern matching
see `perldoc perlre`

^ is the Exclusive Or operator
see `perldoc perlop`

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
clock_crazyAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.