I am having a hard time with the literature on this. I am hoping someone can explain the difference here so that I can better understand the flow of my scripts.
function select-bin {
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Select a Bin"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$x = #()
# Create $OKButton and $objListBox ... removed code as not relevant.
$OKButton.Add_Click({
$x+=$objListBox.SelectedItems
$objForm.Close()
})
$objForm.ShowDialog()
if ($x) {
return $x
}
else {
return $null
}
}
In the code sample above, it works great in Powershell V2, however in V4 the add_click section doesn't work. It successfully closes the form (created in the functions scope) but fails to update $x.
So I guess here are my questions.
In V2, was the add_click section considered in the same scope as the function? (only way I see it having been able to update $x)
What is the proper way to have an event like this alter data? I feel like declaring $x in the global scope is a bit much seeing as I only need it in the function.
In V4 what scope is add_click running in? It is clearly different from what it was in V2, but is it running in the global? is it relative to the $OKButton or the function? I am assuming its a child of either the global or the function but I truly do not know.
Any clarity that anyone could offer would be greatly appreciated. I have a lot of updating to do before my company moves to V4, seeing as I have not been following best practices for scoping (my bad).
In V2, a ScriptBlock, when converted to a delegate, would run dot sourced in whatever scope happened to be the current scope.
Often, this was the scope that created the script block, so things worked naturally. In some cases though, the scope it ran in had nothing to do with the scope it was created in.
In V4, these script blocks run in their own scope - a new scope that is the child of the current scope, just as they were a function and you called the function normally (not dot sourcing.)
I think your best bet is to use one of the following (in roughly best to worst):
$script:x
$x = Get-Variable -Scope 1 -Name x
$global:x
Related
I'm curious to know if there is there a prettier way of correctly scoping objects that are initialised inside of if statements
say you have the following script:
if (x){
create object_A
}
print(object_A)
the above will fail because object A is out of scope for the print so the obvious thing to do is initialise a null variable before the if statement like this:
variable_to_reference_object_A = null
if (x){
variable_to_reference_object_A = create object_A
}
print(variable_to_reference_object_A)
This just always makes me feel like a silly billy because I'm basically saying is:
variable_to_reference_object_A = i haven't a clue because i haven't created it yet
very curious to see what you guys have to say about this, as id love to know if I'm completely doing things wrong (and possibly why it seems to show up in almost all languages). TIA
Sometimes I have difficulty in understanding the logic behind JavaScript and this is one example of something I often see. Here is a simple function that will be passed to a timer:
function myCounter() {
//do something...
}
You can just pass it like this:
setInterval(myCounter, 1000);
But, often we need to have the means to stop it so we assign it to a variable:
var t = setInterval(myCounter, 1000);
This allows it to be cleared later like this:
clearInterval(t);
I use this all the time but it bothers me that I don't understand exactly what I am putting into the variable, and why typeof t returns a number. What exactly have I put in t? Can anyone explain the logic behind this?
When you assign an interval to a variable, you are actually assigning an ID. That ID is then used when you use clearInterval to select which timer to clear. The reason typeof returns a number, is because an ID is a number.
Here is a detailed explanation.
MDN is a fantastic resource for this sort of thing, I highly recommend it
Sorry if I had this stupid question...I've been trying to google for answer but couldn't find one. :(
I have a problem assigning a variable to a virtual interface. For example:
Param.sv
...
string MyInput[3];
MyInput[0] = Signal_CLK; //Storing SignalName to in an Array.
MyInput[1] = Signal_Tx;
MyInput[2] = Signal_Rx;
...
MyInterface.sv
...
Signal_CLK = dut.MicroController.Source.clk; //Signal destination
Signal_Tx = dut.MicroController.Tx_01;
Signal_Rx = dut.MicroController.Rx_01;
...
Test.sv
virtual MyInterface my_vif
logic [7:0] read_value;
....
for (i = 0; i <3; i++ )
begin
read_value = my_vif.My_Input[i];
..
//some logic to compare read_value with spec//
..
end
The problem is when compiling, it doesn't translate my_vif.My_Input[0] into my_vif.***dut.MicroController.Source.clk***. Instead, it thinks that the path is my_vif.***My_Input[i]***.
The reason the compiler thinks you are trying to access my_vif.My_Input[i] is because you are. The My_Input[] array is a completely separate string array; not part of the virtual interface. When using the "thing.thing.thing" syntax, the compiler will loyally follow it, so it will expect there to be something called My_Input that has some elements (as its an array) as a member of the interface given by my_vif.
However, looking over youre code, you are trying to have My_Input[i] replaced at compile time, which is very different. The compiler will not run your loop, look in My_Input[i] and find the string "Signal_CLK" and replace that as part of the path to get the path my_vif.Signal_CLK. Nor can it do that at run time.
I dont know of a generic solution to looking over any variables in an interface; though Im also not sure if thats really what you want. IF you provide more details on the rest of your checker, we might be able to help you more.
You cannot use strings to look up identifiers by name within SystemVerilog. There are tool specific and C interfaces that may let you do this, but that would be very inefficient. The best way to do this by using a combination of abstract/concrete classes and the bind construct. See these references: http://events.dvcon.org/2012/proceedings/papers/01P_3.pdf and http://www.doulos.com/knowhow/sysverilog/DVCon08/DVCon08_SysVlog.php
I'm using some external libraries intended to be used in a browser and they set global variables implicitly like a='a' (without the var).
It seems like when I require certain scripts that do this, sometimes the variable will be accessible outside its scope just like in a browser, but for other scripts the global variable is not accessible outside its own script.
Anyone know how nodejs handles implicit global variables, and why I'm seeing somewhat random behavior? I found surprisingly little on the internet.
I can go into the scripts. write something like
if(typeof exports !== 'undefined' && this.exports !== exports){
var GLOBAL=global;
}
else{
var GLOBAL=window;
}
and then change all implicit references to GLOBAL.reference but these scripts are not my own and every time I want to get the latest version of them I would have to do this over again, which is clearly not desirable.
Using module.exports would be cleaner because then I don't have change all the references, but just add a section of the top of every file that exports the globals, but my original question about how node handles implicit globals is still relevant
I am not sure if this answer will help you, since it is hard to diagnose what is going on with your code, but maybe, some of this reasonings can help you diagnose the actual problem in your code.
The behavior in node is actually similar to that of the browser. If you would declare a variable without the var keyword the variable will be accesible through the global object.
//module foo.js
a = 'Obi-wan';
//module bar.js
require('./foo');
console.log(global.a); //yields Obi-wan
console.log(a); //yields Obi-wan
It is not clear why you say this behavior is not consistent in your code, but if you think about it, the use of global variables is precisely subject to this kind of problems since they are global and everyone could overwrite them at any time, causing as a result this unexpected conditions.
There is one aspect in which node is different from the browser though and that could be affecting the behavior that you see.
In the browser, if you do something like this directly in a JavaScript file:
console.log(this==window); //yields true
But if you do the same thing in a Node.js module:
console.log(this==global); //yields false
Basically, in the outer scope of a Node.js module the this reference points to the current module.exports object.
console.log(this==exports); //yield true
So, chances are that if you are putting data in the global scope (window) in the browser through the use of this, you may end up with a module scope in Node.js instead.
Interestingly, the code inside a function in Node.js behaves pretty much as in the browser, in terms of the use of the global scope.
(function what(){
console.log(this==global); //yields true
})();
This does not directly answer your question but it provides a solution since I don't think it is possible.
I love regexp. They are so powerful:
js = js.replace(/^(\t|\s{4})?(var\s)?(\w+)\s=/gm, function () {
if (arguments[1] || arguments[2]) return (arguments[1] || '') + (arguments[2] || '') + arguments[3] + ' =';
return 'exports.' + arguments[3] + ' =';
});*
JSFiddle here
How does it work? I will retrace my work:
/(\w+)\s=/g will take any var, return 'exports.' + arguments[1] + ' ='; will turn them into an export. Not very good.
/(var\s)?(\w+)\s=/g will take any var, but in the callback we examined first group (var\s). Is it undefined? Then we should export it, else nothing should happen. But what about scopes?
/^(\t|\s{4})?(var\s)?(\w+)\s=/gm now we use indent to determine the scope :)
You should be able to run this regex on your file. Be careful, you need it properly indented, and be aware that I might have forgotten some things.
Ah, the problem was that the global variable that was being declared globally in the browser wasn't being declared via a='a', but with var a='a'. In a browser if the var keyword is used not inside a function it will still declare a global variable. It only declares a local variable if the var keyword is inside a function. Node.js doesn't behave this way, and all var declarations are considered local.
Its ashame node.js does this, it makes it less compatible with browser scripts, for no real reason. (other than allowing people not to have to wrap all their scripts in a function).
I have a problem with my puppet script.
I would like to get a value set in my resource file. I declare a resource like that
define checkxml(
$account = '',
$pwd = template('abc/abc.erb'),
){
if(empty($pwd)){
fail('pwd empty')
}
}
I call it via :
checkxml{"$agtaccount":
account => $agtaccount,
}
I want to get the value of $pwd. The $pwd will get is value by Template. If i try to show the value in my resource definition it's ok, I get the right value, so the Template works fine.
My problem is to get access this value after calling the ressource. I saw the getparam of stdlib but doesn't work for me.
getparam(Checkxml["$agtaccount"],"pwd")
If i try to get the account parameters instead of pwd it's ok. I think as i doesn't declare the pwd i can't get him back
How can i get him ?
Thanks for your help
Ugh, this looks dangerous. First off I'd recommend to steer clear of that function and the concept it embodies. It faces you with evaluation order dependencies, which can always lead to inconsistent manifest behavior.
As for the retrieval of the value itself - that will likely not work if the default is used. That's because on a catalog building level, there is not yet a value that is being bound to the parameter, if that makes any sense.
The resolution of final parameter values is rather involved, so there are lots of things that can go wrong with a manifest that relies on such introspective functionality.
I recommend to retrieve the desired value in a more central location (that depends on your manifest structure) and use it both when declaring the Checkxml["$agtaccount"] resource as well as its other uses (for which you are currently trying to extract it).