在 Powershell 2.0 中使用模块范围的变量
我编写了一个模块,可以通过 Powershell 与 IBM ClearCase 配合使用。起初它只包含几个常用的功能,但现在我正在扩展它。大多数命令必须使用 ClearTool 对象($ct = new-object ClearCase.ClearTool
),但我不想在每个函数调用中重新创建该对象,因为这会带来一些开销。
我还在其中许多函数中创建了 ClearCase 视图,但我可以简单地检查视图是否存在并决定不重新创建它。
我的问题是,最好的模式是什么?我可以有一个“创建 ct 对象”函数,并将维护它的责任放在调用代码上,但我认为我不喜欢这种方法。是否可以为 ClearTool 对象提供一个模块范围的变量,并让 Powershell 在每次尝试重新创建它之前检查它是否已填充?
谢谢!
I've written a module to work with IBMs ClearCase through Powershell. At first it just contained a couple of often used functions, but now I'm expanding it. Most of the commands have to use a ClearTool object ($ct = new-object ClearCase.ClearTool
), but I'd rather not have to recreate that object in every function call as it's a bit of overhead.
I also create a ClearCase view in many of these functions, but I can simply check for existence of the view and decide not to recreate it.
My question is, what's the best pattern for this? I can have a "create ct object" function and put the onus on the calling code to maintain it, but I don't think I like that method. Is it possible to have a module-wide variable for the ClearTool object and have Powershell check to see if it's filled before trying to recreate it each time?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后我创建了几个模块范围的变量。如有必要,我可以隐藏它们,尽管我还没有明确这样做。我有一个函数来创建一个视图,在执行任何实际工作之前必须调用该函数,并且该函数中包含用于创建 ClearTool 对象的代码。我还有代码来使用正确的 ClearTool 对象设置模块范围的变量,以便在其他函数和视图名称中使用。
在每个函数的代码中,如果尚未设置 ClearTool 对象 ($ct),则它们会返回错误条件。
In the end I created a couple of module-wide variables. I could hide them if necessary, although I haven't explicitly done that yet. I have a single function to create a view which must be called before doing any actual work and included in that is code to create the ClearTool object. I also have code to set the module-wide variables with the correct ClearTool object for use in other functions and the name of the view.
In the code of each of the functions if the ClearTool object ($ct) has not yet been set, they return an error condition.