not /pattern/

M

MattJ83

Quick question
if i was to write code to match a /pattern/ but then wanted the script
to exit if it didn't find the pattern is it as simple as (!/pattern/) ?


Code:
{
if (/word/)
{
print "$_\n";
$info = $_;
} elsif /!word/ { exit; } # ????

Thanks
 
T

Tad McClellan

MattJ83 said:
Quick question


It is a quick question that you could answer yourself by
writing a quick program to test your theory.

if i was to write code to match a /pattern/ but then wanted the script
to exit if it didn't find the pattern is it as simple as (!/pattern/) ?


What happened when you tried it?

Code:
{
if (/word/)
{
print "$_\n";
$info = $_;
} elsif /!word/ { exit; } # ????
^^^^^^^

That is not Perl code. (and you put the ! in the wrong place)

This is Perl code:

----------------------
#!/usr/bin/perl
use warnings;
use strict;

$_ = 'this has a Word in it';

if (/word/) {
print "$_\n";
}
elsif ( ! /word/ ) {
print "here I would be calling exit()\n";
}
----------------------


See how easy that was?

No need to ask hundreds of people to help with something that simple...


But I would write it this way instead:

----------------------
#!/usr/bin/perl
use warnings;
use strict;

$_ = 'this has a Word in it';

exit unless /word/;
print "$_\n"; # must have matched for control to reach this statement
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top