tags:

views:

139

answers:

3

Hi, How to detect if there is ( = ) sign in current line? thank you.

$_ = $currentLine;
if (Include =)
{
# do some thing
}
else
{
# do another thing
}
+7  A: 
local $_ = $currentLine;
if (/=/) {

or

if ($currentLine =~ /=/) {
Anonymous
+7  A: 
 my $currentLine; # presumably this has a value from something earlier

if ($currentLine =~ /=/)
{
    # line has an = in it
}
else
{
    # it doesn't
}

Read about the =~ operator at perldoc perlop, and regular expressions at perldoc perlre.

Ether
Thank you for the reference Links.
Nano HE
+11  A: 

The simplest way is to use index:

if ( index( $line, '=' ) > -1 ) {

It's faster than a regex, because it's done on the C-level, without any compiliation. If you're looking in Perl code, you probably don't care if there is an equals sign on a comment line, therefore there's this:

$line =~ m/^[^#]*=/;

If that doesn't suit your needs, then use the first.

Axeman
Not all `#` characters on a line denote comments. They might be embedded in strings, or they might be used as quote characters with `q##`, `s###`, and other related operators. So might `=`, for that matter.
Rob Kennedy
@Rob Kennedy: Good point, but it's not that likely that a literal would be on the left hand side. Of course you could be assigning to `$#some_array`, which is a more likely problem. True though, that the problem is a bit more complex and a full solution for code almost requires a full parser.
Axeman
+1 for mentioning `index`.
Sinan Ünür