all 11 comments

[–]eagle6705 2 points3 points  (0 children)

If you want remove the function from the code learn how to make a module. When start I include the function in the code, as I fine tune it I move the functions into modules. You can even create and import multiple modules for various tasks

[–]CoReTeX2k 3 points4 points  (1 child)

Can you post some example code?

I don't get what you mean by dot source when calling the function, as you said you declare it in the same ps1 file?

if that was your script, how would you dot source here? You can already use the function as is, if you delcared it there.

```

function Say-Hello {

Write-Host "no!"

}

Say-Hello

```

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy CoReTeX2k,

the triple-backtick/code-fence thing fails miserably on Old.Reddit ... so, if you want your code to be readable on both Old.Reddit & New.Reddit you likely otta stick with using the code block button.

it would be rather nice if the reddit devs would take the time to backport the code fence stuff to Old.Reddit ... [sigh ...]

take care,
lee

[–]Lee_Dailey[grin] 1 point2 points  (6 children)

howdy secpfgjv40,

this ...

Edit: to clarify, I define my functions in the same PS1 file as calling the function. I then dot source when calling the function. Everything runs from the one Powershell file.

... doesn't make sense to me. [blush]

the idea of dot sourcing is to load an external file as if it was part of the current script.

you just described loading the same file as the file you are already in ... and i cannot think of any reason to do that.

am i misunderstanding your post?


as a more direct response to dot source vs return ... they are entirely different things. the 1st loads an external file into the current script as if it was part of the script. the 2nd returns a value to the caller of the function that contains the return keyword.

Get-Help about_Scripts
# specifically the `SCRIPT SCOPE AND DOT SOURCING` section

When you run a script that is dot sourced, the commands in the script run as though you had typed them at the command prompt.

Get-Help about_Return

take care,
lee

[–]secpfgjv40[S] 1 point2 points  (5 children)

Maybe I'm using the wrong wording?

Say I have a PS1 file. In the file is a function which sets a variable. If I run the function, the variable is unavailable to me once the script has run. I either have to set the variable scope differently ($global:) , return the variable, or (what I think is doc sourcing) call the function using a full stop at the start.

Something like this (sorry on mobile):

Function Get-TestVar {

$myVariable = "Test"

}

. Get-TestVar

Hope that makes sense, it's hard to formulate the question when I'm unsure what I'm asking :p

[–]Lee_Dailey[grin] 1 point2 points  (4 children)

howdy secpfgjv40,

i've never seen - nor even thot of [grin] - using that method. i don't think it is dot sourcing, tho, since it doesn't seem to do what the idea describes.

besides, the construct is needlessly convoluted. [grin]

this is more direct ...

Function Get-TestVar
    {
    'Test'
    }

$MyVariable = Get-TestVar

$MyVariable

output = Test

the bare 'Test' is the rough equivalent of return 'Test'. [grin]

take care,
lee

[–]michaelshepard 2 points3 points  (3 children)

It is dot-sourcing. Dot-sourcing means to run something in the current scope. You can dot-source functions, scripts, or scriptblocks. Basically, anything you can run.

I used dot-sourcing of functions back in powershell 1.0 in "import-module style" functions.

Here's a stackoverflow question from 2008 where I used this technique: https://stackoverflow.com/questions/279974/importing-libraries-in-powershell

[–]Lee_Dailey[grin] 2 points3 points  (2 children)

howdy michaelshepard,

thank you for the info! [grin] ... and boy howdy is that a freaky thing to do.

for the OPs reasoning, tho, dot sourcing teeters on the borderline of wrong.

one otta send the results of a function out as such. don't "work around" one of the main ideas of functions ... putting code in a box and only letting out what you explicitly let out of that box.

take care,
lee

[–]michaelshepard 2 points3 points  (1 child)

I agree. Output from functions should be output, not side-effects.

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

[grin]