Gossamer Forum
Home : General : Perl Programming :

=~ || in an if() statement?

Quote Reply
=~ || in an if() statement?
Hi,

This little thing has been bugging me for a while now <G>

Basically, if you use;

Code:
$_ =~ /regex-string/ and do_something();
$_ =~ m/regex-string/ and do_something();
|regex-string| and do_something();
m|regex-string| and do_something();
/regex-string/ and do_something();
m/regex-string/ and do_something();

These work fine (although the m isn't really needed).

However, something like this;

Code:
if ($_ =~ /regex-string/) { do_something(); }WORKS
if ($_ =~ m/regex-string/) { do_something(); }WORKS
if (|regex-string|) { do_something(); }DOESN'T WORK
if (m|regex-string|) { do_something(); } DOESN'T WORK
if (/regex-string/) { do_something(); } WORKS
if (m/regex-string/) { do_something(); } WORKS

I get a syntax error. Basically, anything that uses the format of if (|regex|) (i.e using || as the delimiter, mainly for automatic escaping of / , which would get messed up if using m//).

Is there a reason/workaround for this?

Today, for example.. I wanted to do;

Code:
if ($_ !~ |\Q<Topic r:id="Top/$cat/\E(.*?)\Q">\E| && $flag && $_ =~ |\Q<Topic r:id="Top/|i) { $stop = 1; }

.. but ended up having to use the 'm';

Code:
if ($_ !~ m|\Q<Topic r:id="Top/$cat/\E(.*?)\Q">\E| && $flag && $_ =~ m|\Q<Topic r:id="Top/|i) { $stop = 1; }

.. as it kept coming up with synax errors Frown

TIA for any comments= )

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!