Page 1 of 2
Deref Anywhere is Major Break in Backward Compatibility
Posted: 14 Jul 2014 11:49
by FluxTorpedoe
Hi'
@Don
Not really a

but a disrupting side-effect has been introduced in the use of the
Like operator with variables (because of changes with dereferenced operators, I presume):
::$b="b"; if ("abc" Like "*$b*"){msg "ok";}
doesn't work anymore, the
* before
$b must now be escaped:
::$b="b"; if ("abc" Like "**$b*"){msg "ok";}
I discovered this while searching why my implementation of Marco's Everything script wasn't working with an updated XY — so most of you probably had empty results when you tried it.
Don't know how that should be handled... Good luck with that!
Have a nice day,
Flux
Re: [β] Everything in XYplorer — Rev. 0.94 / 2014/07/09
Posted: 14 Jul 2014 11:55
by admin
I guess you have to do this now:
Code: Select all
::$b="b"; if ("abc" Like "*".$b."*"){msg "ok";}
Re: [β] Everything in XYplorer — Rev. 0.94 / 2014/07/09
Posted: 14 Jul 2014 12:09
by PeterH
admin wrote:I guess you have to do this now:
Code: Select all
::$b="b"; if ("abc" Like "*".$b."*"){msg "ok";}
OK:
Code: Select all
::$b="b"; if ("abc" Like "*"."$b*"){msg "ok";}
should work to.
While FluxTorpedoes solution only works by chance: as $b doesn't contain a
defined variable name, *$b is resolved by the contents of $b (and the * is 'ignored')

Re: Deref Anywhere is Major Break in Backward Compatibility
Posted: 14 Jul 2014 13:06
by TheQwerty
Guys this breaks a hell of a lot more than just the Like operator.
'*$var' is going to be common in regular expressions, filters, renames, and anywhere else that wildcards are supported.
Looking at my own script files I have 312 lines that now need to be debugged because of this haphazardly adding dereferencing everywhere.

Re: Deref Anywhere is Major Break in Backward Compatibility
Posted: 14 Jul 2014 13:11
by admin
Well, it's not too late. I was in doubt about this anyway, and since there is no dereferencing like this in PHP I have no model to look it up.
So, does everybody agree that dereferencing should NOT take place in Interpolation? (Interpolation means that variables that are embedded in double-quoted or unquoted strings are resolved.) I agree.
Re: Deref Anywhere is Major Break in Backward Compatibility
Posted: 14 Jul 2014 13:16
by TheQwerty
I agree - at least until we can think of a better way to handle this.
That includes HEREDOCs correct?
Re: Deref Anywhere is Major Break in Backward Compatibility
Posted: 14 Jul 2014 13:28
by admin
Yep.
Re: Deref Anywhere is Major Break in Backward Compatibility
Posted: 14 Jul 2014 13:38
by FluxTorpedoe
admin wrote:So, does everybody agree that dereferencing should NOT take place in Interpolation? (Interpolation means that variables that are embedded in double-quoted or unquoted strings are resolved.) I agree.
Fine by me.
BTW,
admin wrote:I guess you have to do this now:
Code: Select all
::$b="b"; if ("abc" Like "*".$b."*"){msg "ok";}
That's the first thing I tried and why it took me so long to figure out the source of the dereferencing problem: it gives false positives (or am I missing something?)…
::$b="b"; if ("xyz" Like "*".$b."*"){msg "ok";} // OK

Re: Deref Anywhere is Major Break in Backward Compatibility
Posted: 14 Jul 2014 13:39
by admin
Re: Deref Anywhere is Major Break in Backward Compatibility
Posted: 14 Jul 2014 13:54
by admin
Okay, no bug. It's caused by operator precedence. Need to add some parentheses:
Code: Select all
::$b="b"; if ("xyz" Like ("*".$b."*")){msg "ok";} // OK
Re: Deref Anywhere is Major Break in Backward Compatibility
Posted: 14 Jul 2014 13:59
by FluxTorpedoe
Hmmm, so the "Like" operator has precedence over the "dot concatenator"…
Good to know!
BTW, thanks for being so responsive (as always)…

Re: Deref Anywhere is Major Break in Backward Compatibility
Posted: 14 Jul 2014 14:18
by PeterH
admin wrote:Well, it's not too late. I was in doubt about this anyway, and since there is no dereferencing like this in PHP I have no model to look it up.
So, does everybody agree that dereferencing should NOT take place in Interpolation? (Interpolation means that variables that are embedded in double-quoted or unquoted strings are resolved.) I agree.
I
*don't* like it, but I see the need.
Especially inside double quotes.
But also in "unquoted strings"? They "should" not exist at all?
I'm just afraid this could make problems with other situations, as I don't know exactly how to differentiate "unquoted strings" from other situations...
Re: Deref Anywhere is Major Break in Backward Compatibility
Posted: 14 Jul 2014 14:23
by admin
Unquoted strings are tolerated. I don't see particular problems with regard to dereferenciation.
Re: Deref Anywhere is Major Break in Backward Compatibility
Posted: 14 Jul 2014 14:26
by TheQwerty
A possible compromise...
Add a deref interpolation SC:
deref(string, [flags=0])
string should be a literal (in single quotes) to avoid interpolation of the function's input (see examples).
Flags:
0 = Interpolate derefs and then other variables.
1 = Interpolate derefs only.
Code: Select all
"Examples"
$g = 'Hello';
$v = '$n';
*$v = 'world';
echo deref('$g *$v!'); // 'Hello world!'
echo deref('$g *v!', 1); // '$g world!'
// Examples of why you'll want to avoid the initial interpolation.
echo deref("$g *$v!", 1); // 'Hello *$n!'
// Became deref('Hello *$n', 1);
// Flag is 1, so '$n' is untouched & there is no '*$n'.
echo deref("$g *$v!"); // 'Hello *world!'
// Became deref('Hello *$n');
// Flag is 0, there is no '*$n' so '$n' is resolved.
Since that's a new SC it shouldn't break anything - it's not as legible but it's much safer.
For HEREDOC support you introduce a new syntax '<<<*FOO' and '<<<*#FOO'. Unfortunately, this will break backwards compatibility, but I see no other way. Hopefully this won't actually affect anyone since it seems most people stick with alpha characters when naming their HEREDOCs. (I have zero matches for '<<<*'.)
Fair tradeoffs?
----
If you wanted to go even further - I know we've seen some requests for fine tuning which variables are resolved so this deref SC could really be an interpolate SC with flag:
-1 - All (default)
0 - None
1 - XY variables ('<...>')
2 - Environment variables ('%...%')
4 - Local variables
8 - Permanent variables
16 - Global variables
32 - Derefed variables.
Re: Deref Anywhere is Major Break in Backward Compatibility
Posted: 14 Jul 2014 14:47
by admin
Wow, little overkill maybe?
