Page 1 of 1

Tip searched: in a function get the *real* name of a parm

Posted: 08 Jul 2023 01:16
by PeterH
Imagine a script like:

Code: Select all

"Test"
   $a = "a|b|x";
   debugv($a);
   ...
function debugv($var) {
   ...
}
debugv() writes some info about the supplied variable, and it would be *very* helpful to do this under a title with the *real* var name, here '$a'.
No idea, if this is possible at all.
But
debugv($variablename, '$variablename');
and
function debugv($var, $name) {
is rather boring - especially if you need to call it quite often...

So: if not possible it will be a wish :-)

Re: Tip searched: in a function get the *real* name of a parm

Posted: 18 Jul 2023 11:27
by admin
I don't want to get too crazy about this, but I thought the idea was kind of interesting, so I added varname() to get the name of a variable in the calling scope.

Code: Select all

// get the name of a variable in the calling scope
   $a[0] = "foo";
   // nothing special here; no caller, so varname() falls back to the current name
   echo varname($a[0], 2) . " = " . $a[0];  //$a = foo
   // go down to a user function
   godowntowork($a[0]);
   
function godowntowork($var) {
   echo '$var' . " = " . $var;  //$var = foo
   // here's the interesting part; varname() returns the original name in the calling scope
   echo varname($var, 2) . " = " . $var;  //$a[0] = foo
   echo "Array name in caller: " . varname($var, 3); //$a
}

Re: Tip searched: in a function get the *real* name of a parm

Posted: 18 Jul 2023 11:57
by PeterH
:shock:
To be true: I hadn't expected this :whistle:
So it seems I have to update XY.

If all is well there will be a function to debug array variables by showing the contents of each element.
(And even allow to change it.)
OK: you must include function calls in the script - but it *can* be very helpful. At least for me.

So, one more time: thanks a lot! :cup: :om:

(By the way: the name 'varname()' can be a bit misleading, as it doesn't have a hint to the 'callers' varrname.)

Re: Tip searched: in a function get the *real* name of a parm

Posted: 18 Jul 2023 12:05
by admin
varname is a nice name, everything else seemed clumsy... but yes, so what about varnamecaller or varnamecalling? (did not find anything to steal from in other languages)

Got a better idea. Now varname() has flags. Updated the code in viewtopic.php?p=212660#p212660

Next beta.

Re: Tip searched: in a function get the *real* name of a parm

Posted: 18 Jul 2023 23:38
by PeterH
OK: tested, :tup:
In combination with my the private function caller() you once made for me it's even better!
Sad that it's still inofficial - it works OK since maaany years, and it would be bad to loose it somewhen :ninja:

Also sad: now I have some self-made problems, most about builtin debug stmts.
But I'm working on it...

By the way: the idea with the option making varname to varnamecaller was great - that really was a reason for a :biggrin:

So thanks for all!

Re: Tip searched: in a function get the *real* name of a parm

Posted: 19 Jul 2023 08:23
by admin
Concerning SC caller. I should have made it a parameter to SC self:

Code: Select all

Syntax: self(info, [level])
level: 0 = self (default), 1 = caller, 2 = caller of caller, etc

Re: Tip searched: in a function get the *real* name of a parm

Posted: 19 Jul 2023 11:54
by PeterH
The idea is good...
...but a change would bring problems for me: I use it in a routine that needs compatibility with (very) old and with new XY.

(OK: per version() it *could* be done)
And: if the most outer caller would be "XY", and caller of more outer levels were "" it even would be better than now.
(Note: must work for subs and for functions!)

So if you want to do it: OK.
But then: please let caller() remain for quite some time.

Re: Tip searched: in a function get the *real* name of a parm

Posted: 19 Jul 2023 12:00
by admin
This would be even better:

Code: Select all

level: 
  0 = self (default)
  1 = 1st level (top level)
  2 = 2nd level, etc
 -1 = caller
 -2 = caller of caller, etc

Re: Tip searched: in a function get the *real* name of a parm

Posted: 19 Jul 2023 12:14
by PeterH
I would see it the other way - first of all I need the direct caller.
(Though I could live with it.)

If I get "Top Level", I wouldn't know how many levels it's away.

So I would always use -1, and sometimes expand (loop) up to the end.
I think it would feel more natural if this is 1, 2, ...
But again: not a real problem to be negative to work with negative numbers.

But for going up or down: it's necessary to have a soft response (like "") if 'out of range'.

Re: Tip searched: in a function get the *real* name of a parm

Posted: 19 Jul 2023 16:33
by PeterH
New beta: (in a script *without* "strange" dependencies) tested

Code: Select all

   $cbase = Self('base', -1);   $ccaption = Self('caption', -1);  // get info about caller
:arrow: :tup:

So: looks good! [cake] [tea]