locked
Emulating Korn Shell "Autoload" Functionality RRS feed

  • Question

  • Hello.  In the Unix/Korn shell world, there is a feature available to "autoload" user-defined functions from function definitions maintained in files external to the general or application script.  It is essentially a function declaration that allows, for example, keeping utility functions in a separate library, but then make them known to a script without actually calling them then and there.  Is this something that WPS supports, or is there an accepted "best practice" to emulate?

    Thanks for any help or insight on this...
    Tuesday, April 13, 2010 12:39 PM

Answers

  • Of course you can do that...

    For starters, any script is basically a non-preloaded function. So if you put Get-NeatStuff.ps1 in your PATH, it will show up in Get-Command as an ExternalScript (with the .ps1 extension), but you can call it as Get-NeatStuff ... it supports all the usual function features like declared PARAM()eters and [CmdletBinding()] and even begin/process/end blocks ... but it gets reparsed on each invocation.

    I don't think you'll find any reason to worry about the fact that it doesn't get loaded only once. However ...

    If you want it to stay loaded, you have two options:

    1) define the function using the function keyword and "global:" prefixed to the function name ... in a script with the same name, and CALL the function from the bottom of the script, so when the script is called the first time, it defines the function and calls it -- from then on, just the function is called. PowerBoots does this currently with it's functions.

    2) Use my PowerShell Autoload function (there are a few caveats, read the comments at the top of it).


    Tuesday, April 13, 2010 8:27 PM

All replies

  • Interesting...  There's no such functionality out of the box though in the default PowerShell host.

    Even emulating this couldn't be done in the default host.

    You would have to have all of your functions defined in a library or module, and have that fully pre-loaded before you could invoke any of them.

    • Marked as answer by Marco Shaw Tuesday, April 13, 2010 3:11 PM
    • Unmarked as answer by Marco Shaw Tuesday, April 13, 2010 11:35 PM
    Tuesday, April 13, 2010 3:11 PM
  • Of course you can do that...

    For starters, any script is basically a non-preloaded function. So if you put Get-NeatStuff.ps1 in your PATH, it will show up in Get-Command as an ExternalScript (with the .ps1 extension), but you can call it as Get-NeatStuff ... it supports all the usual function features like declared PARAM()eters and [CmdletBinding()] and even begin/process/end blocks ... but it gets reparsed on each invocation.

    I don't think you'll find any reason to worry about the fact that it doesn't get loaded only once. However ...

    If you want it to stay loaded, you have two options:

    1) define the function using the function keyword and "global:" prefixed to the function name ... in a script with the same name, and CALL the function from the bottom of the script, so when the script is called the first time, it defines the function and calls it -- from then on, just the function is called. PowerBoots does this currently with it's functions.

    2) Use my PowerShell Autoload function (there are a few caveats, read the comments at the top of it).


    Tuesday, April 13, 2010 8:27 PM