3

Is it possible to have a function return the name of the variable that stores it?

i.e $a = myFunction

and have the function know that it is held in $a, when $a is dynamically assigned to it in a shell?

benignadmin
  • 53
  • 1
  • 1
  • 7
  • 3
    What exactly are you trying to accomplish here?!? It doesn't seem to make a lot of sense... – Massimo Sep 15 '15 at 15:52
  • 1
    @Massimo If someone runs a function, they may have to pass some other output into a different function. Being able to access the variable the original function is stored in will 'remind' them of what they need to type :-) – benignadmin Sep 15 '15 at 17:52
  • 2
    This... makes even less sense. – Massimo Sep 15 '15 at 18:38
  • 1
    @benignadmin = Can you show us an example, even if in pseudocode, of what you are trying to accomplish? – Get-HomeByFiveOClock Sep 15 '15 at 19:10

3 Answers3

3
function test() {
    write-Host "Called by: $($MyInvocation.Line)"
}

$a = test

Which outputs

Called by:     $a = test

?

TessellatingHeckler
  • 5,726
  • 3
  • 26
  • 44
  • 1
    That is EXACTLY what I needed. Thanks! I upvoted you. Not enough rep points to make it show, unfortunately. – benignadmin Sep 15 '15 at 23:18
  • 1
    @benignadmin http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – jscott Sep 16 '15 at 00:19
  • V.nice! I imagine that may be quite a useful little trick for both debugging and logging. – ErikE Sep 16 '15 at 01:13
  • FYI, this tells you the *line* of code where the function was called; which might or might not include assigning its result to a variable, or could contain anything else entirely; just try calling that function with `if(test -eq $null) { ... }`, or similar constructs. – Massimo Sep 16 '15 at 10:50
  • And also please keep in mind that, even in your case, $a will not be assigned until the function returns; any action on $a while inside the function *will operate on the previous value of $a* (if any), and $a will be overwritten with the actual return value of the function after it ends. – Massimo Sep 16 '15 at 10:53
  • Thanks for the warning. You can save $MyInvocation is a $global variable to keep it more permanent. – benignadmin Sep 17 '15 at 14:47
1

I don't think this is possible at all, nor would it make much sense.

It's like expecting the integer 42 to know if it has been stored in the variable $answer.


Once a function returns, only its return value (if any) is stored in a variable (if an assignment is actually used). If Get-Answer returns 42 and you issue the command $answer = Get-Answer, then $answer will indeed contain 42, but no record will be kept of the fact that it was stored there by having it returned from a function; for all intents and purposes, the end results of $answer = 42 and $answer = Get-Answer are identical, if Get-Answer does indeed return 42.

Also, the assignement (if any) only happens after a function returns; the function only provides a return value (if it does); it doesn't and shouldn't care what PowerShell is going to do with this value after it returns; and its return value could very well be discarded instead of being assigned to anything. There is no direct link between $answer and Get-Answer: what PowerShell does when faced with a command like $answer = Get-Answer is:

  • Execute Get-Answer
  • Grab the return value from Get-Answer
  • Store this value in $answer

None of the players has any knowledge of this link; the function doesnt know what its return value will be used for, and the variable doesn't know where its assigned value comes from.

And even if some record was kept of this assignment having ever happened, it definitely wouldn't have happened yet while Get-Answer was still being executed.

Massimo
  • 70,200
  • 57
  • 200
  • 323
0

With the minimal info you've shared, this is the best guess I could make.
I'm going largely on your statement of "pass some other output into a different function"

myFunction1 {
    ...code...
    return $someThing
}

myFunction2 {
    param ($param1)
    ...code...
}

Usage

$a = myFunction1
myFunction2 $a

Be aware that echo's or write-hosts cmds within a function are ALSO return values. Use write-debug for debug info, or if you really need that info "returned" return is as an array (or a custom object)

return @($var1, $va2, $var3)
Clayton
  • 4,523
  • 17
  • 24
  • That sounds different that what you first asked about, no mention of a 2nd function. Or is it single function that needs to remember config state settings from the last run? Try having the function use "$cachedSomething = import-clixml" at the top and "export-clixml $cachedSomething X:\network\config.xml" at the end. //poster appears to have deleted comment this response was for// – Clayton Sep 15 '15 at 19:43
  • Thanks for trying. I guess the best way to say it in terms of powershell is I want something that does this: Get-ParentVariable which would be run inside of a function and simply tell the function what variable it is stored in, which could be returned out of the function. – benignadmin Sep 15 '15 at 19:44
  • 1
    @benignadmin this still doesn't make sense: the assignment of the function's return value to a variable is processed only *after* the function returns, thus it doesn't even *exist* until the function completes its execution. – Massimo Sep 15 '15 at 19:46
  • 1
    even it you could do this exactly what would be the point of knowing the variable name you are about to add an object to? – Jim B Sep 15 '15 at 21:09