tags:

views:

230

answers:

4

What does this expression in Perl programming do?

$variable =~ /^\s*$/;
+17  A: 

That's looking for a line start (^), followed by zero or more whitespace (\s*) characters, followed by a line end ($).

It's essentially looking for blank/empty lines.

I'd expect the expression to be used in something like:

if ($v =~ /^\s*$/) {
   # found a blank line
}

in order to perform the check and a following action.

Brian Agnew
The phrase "any number" is a little vague, I would say zero or more whitespace characters instead.
Chas. Owens
I've edited to be a little more explicit
Brian Agnew
+1  A: 

Nothing, since you are not assigning the result of this operation to any variable, and since it has no side effects that you are likely to test for.

However, if you said

if ($variable =~ /^\s*$/) { print "something"; }

You would be answering the question:

Does the value in variable consist of an empty line or a line consisting of nothing but non printing whitespace characters such as spaces and tabs?

-Alex

true alex,the actual statement is in a loop which reads data from the file and has the condition next if ($variable =~ /^\s*$/);
swapnil
In that case, it means"skip blank lines in the file"
Igor Krivokon
While you are correct that $
+1  A: 

=~ is a match operator.

The statement returns true if $variable only consists of spaces, or is simply empty.

So it just checks if the string is empty or not.

Berzemus
A: 
 if ( $variable =~ /^\s*$/ )

is the same as

 unless ( $variable =~ /\S/ )

or

 if ( $variable !~ /\S/ )
Sinan Ünür