why no syntax error?

R

Ronald Fischer

I recently wrote the following nonsense:

if($arg = ~ /^\d{4}$/) { ... }

In case you don't see it immediately: I had erroneously typed a space between
the equal sign and the tilde.

To my surprise, this statement was executed without error message. $arg was set
to 294967295.

The tilde did bitwise complement of course, but on what? On a regexp? This
does not seem to make sense to me....

This was executed under Perl 5.6.1

Ronald
 
S

Sherm Pendley

Ronald said:
I recently wrote the following nonsense:

if($arg = ~ /^\d{4}$/) { ... }

The tilde did bitwise complement of course, but on what? On a regexp? This
does not seem to make sense to me....

Pattern matching implicitly uses $_ if no other variable is used. I
expect you'd get the same results from this:

if ($arg = ~ ($_ =~ /^\d{4}$/)) { ... }

sherm--
 
A

Anno Siegel

Ronald Fischer said:
I recently wrote the following nonsense:

if($arg = ~ /^\d{4}$/) { ... }

In case you don't see it immediately: I had erroneously typed a space between
the equal sign and the tilde.

To my surprise, this statement was executed without error message. $arg was set
to 294967295.

The tilde did bitwise complement of course, but on what? On a regexp? This
does not seem to make sense to me....

A regex that isn't bound by =~ applies to $_. That is what happens
here. The result of that match in scalar context is inverted and
assigned to $arg.

Anno
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top