Note: Beware! Default reply-to is to the list.

 On 05/02/11 04:47, Anne Wainwright wrote:



Hi Anne

I've just started learning perl, and found your contact details here (http://www.pm.org/groups/522.html) while looking for community help. It seems a bit out of date, so please forgive me if I'm knocking on the wrong door.

B::Deparse's -p option is helpful here:

I was exploring the "/x" regexp modifier, and came upon some unexpected results. Upon closer inspection, my problem lies with the ternary operator. I can't see why the following results are inconsistent ..

say '1:'.( 'hello' =~ /h e l l o/ )?'true':'false'; # no '/x', no match

say((('1:' . ('hello' =~ /h e l l o/)) ? 'true' : 'false'));

it evaluates the string '1:' concated with the return of the regex evaluation, and '1:' is true. This can be seen by replace '1:' with '0' which is false.

say '2:'.( 'hello' =~ /h e l l o/x )?'true':'false'; # '/x' ignores spaces, produces '1'

say((('2:' . ('hello' =~ /h e l l o/x)) ? 'true' : 'false'));

While this is 'correct' as 'hello' does match /h e l l o/x, it isn't why it returns true, it is again a concatenation of '2:' with the result of the regex match, i.e. '2:1' which is also true.

say '3:'.( 'h e l l o' =~ /h e l l o/x )?'true':'false'; # '/x' ignores spaces, no match

say((('3:' . ('h e l l o' =~ /h e l l o/x)) ? 'true' : 'false'));

The same as 1:

say '1:'.( 'hello' =~ /h e l l o/ ).'.';        # no '/x', no match

say '2:'.( 'hello' =~ /h e l l o/x ).'.'; # '/x' ignores spaces, produces '1'

say '3:'.( 'h e l l o' =~ /h e l l o/x ).'.'; # '/x' ignores spaces, no match

say '1:'.( 'hello' =~ /h e l l o/ ) && 'true.'; # no '/x', no match

say '2:'.( 'hello' =~ /h e l l o/x ) && 'true.'; # '/x' ignores spaces, produces '1'

say '3:'.( 'h e l l o' =~ /h e l l o/x ) && 'true.'; # '/x' ignores spaces, no match

More of the same:

say '1:'.(( 'hello' =~ /h e l l o/ ) + 0) .'.';        # no '/x', no match

say '2:'.(( 'hello' =~ /h e l l o/x ) + 0) .'.'; # '/x' ignores spaces, produces '1'

say '3:'.(( 'h e l l o' =~ /h e l l o/x ) + 0) .'.'; # '/x' ignores spaces, no match

say '1:'.(( 'hello' =~ /h e l l o/ ) + 0)?'true':'false'; # no '/x', no match

say '2:'.(( 'hello' =~ /h e l l o/x ) + 0)?'true':'false'; # '/x' ignores spaces, produces '1'

say '3:'.(( 'h e l l o' =~ /h e l l o/x ) + 0)?'true':'false'; # '/x' ignores spaces, no match

.. where only the highlighted sections above yield the expected results. All the others always return 'true'.

If you can't help, then please point me in the right direction for community support.

Many thanks and kind regards

DT

'.' is binding closer than the ternary operator, you want something like:
say '2:'.('hello' =~ /h e l l o/x ? 'true' : 'false');
to evaluate the regex match with the ternary operator, not the result of the concatenation.


_______________________________________________
Za-pm mailing list
Za-pm@pm.org
http://mail.pm.org/mailman/listinfo/za-pm

posts also archived on Mail Archive
http://www.mail-archive.com/za-pm@pm.org/

_______________________________________________
Za-pm mailing list
Za-pm@pm.org
http://mail.pm.org/mailman/listinfo/za-pm

posts also archived on Mail Archive
http://www.mail-archive.com/za-pm@pm.org/

Reply via email to