Perl equivalent of ksh if / echo statement

Is there an equivalent perl statement for the following ksh statement ?

example

if [ `echo $ans |wc -c` = 2 ]
then
...
else
...
fi

Hmm ... something along the lines of:

if (length($ans) == 2) {
# ...
} else {
# ...
}

length() returns the number of characters. If the text is in plain ASCII, this equals the number of bytes. Perl usually treats multibyte string as ASCII so length() on a multibyte string will appear to give you the number of bytes too.

If you really want bytes instead of the number of characters, you can also try this:

{
require bytes;
if (bytes::length($ans) == 2) {
# ...
} else {
# ...
}
}

In ksh I would just do:
if [[ ${#ans} -eq 2 ]]
anyway.