perl Surprises me Yet Again
I have said it in many other posts over the years perl just never ceases to surprise me.
Well I was down and dirty with some code today doing an ongoing migration from Informix to Prostgres of a very very large perl code-base.
Needless to say one of the most important tools in this migration is the good old 'inline if' or ' ternary if' or just plain old '? :'
The code base has a largely hash based so it is a simple matter to find the offending lines of Informix SQL and using the '? :' to add in the Postgres SQL. Of course only part of the client base is migrating so it is mandated that both DB must work with the same code hence the need for all the inline ifs.
So there are allot of lines like this
$self->filter({
field_name=>'xxxx'
field_value=>$self->isInformix ? 'TODAY' : 'CURRENT_DATE',
...
Indispensable really and I think the proper use for this IMHO.
Now the fun part.
Occasionally some of the date~time functions where already extracted out into variables for other reasons and at one of these I cut and pasted some code incorrectly (I though) like this.
my $date =$self->isInformix ? $sql_today : $sql_current_date = "TODAY";
Rather than this
my $date = $self->isInformix ? $sql_today : $sql_current_date;
To my surprise the first code ran and correctly worked at least for informix.
I wondered what was going on?
Seems perl allows this reverse syntax of an inline if as long as both arguments are assignable.
So this
$self->isInformix ? $sql_today : $sql_current_date = "TODAY";
Turned out to be valid perl code.
Haven't found a use for this but it is nice to know about it.
Funny even though I have playing with this language for over 10 years every once and a while it throws out something new to me even at the basic syntax level.
A related construct can be used for sometimes pushing to one array and sometimes to another: