Changing $*DISTRO values for testing

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Changing $*DISTRO values for testing




I need to test a feature that includes this line:


if $translate-nl && $*DISTRO.is-win



I have tried to reassign a value to $*DISTRO,


$*DISTRO='Windows 10';



but it says:


Cannot modify an immutable Distro (debian (9.stretch))␤



$*DISTRO is a dynamic variable, and it makes sense that it's not modified. That said, is there any other way that code can be tested (other than going to Windows, of course)


$*DISTRO




3 Answers
3


my $*DISTRO = ...



Hopefully modifying the original is unnecessary. It's generally unreasonable action-at-a-distance -- almost certainly so if someone has arranged for it to be an immutable value. This is the reason why global variables got such a bad reputation.



To elaborate on raiph's answer: the * in $*DISTRO marks it as a dynamic variable. You can re-declare it any scope, and code called from there will see the redeclared value:


*


$*DISTRO



my $*DISTRO = ...;
# coded called from here sees the redeclared value

# code called from here sees the original value



Now, the question remains, what do you put in place of these pesky ...?


...



In the simplest case, a mock that just has whatever the code under test needs:



my class Distro has $.is-win
my $*DISTRO = Distro.new( :is-win );
# call your test code here



If the code needs more attributes in Distro, just add them to the mock Distro class.


Distro


Distro



If the code needs a "real* Distro object, for some reason, you can instantiate the built-in one. The constructor .new isn't really documented, but the source code makes it pretty obvious what arguments it expects.


Distro


.new




OK, I got the answer relatively fast. $*DISTRO is actually a read-only alias of PROCESS::<$DISTRO>


$*DISTRO


PROCESS::<$DISTRO>



So we only need to do:


my $*DISTRO = Distro.new(:is-win,:release<11>,:path-sep('|||'),:auth<unknown>,:name<mswin32>,:desc<Test>,:version<v11>);
say $*DISTRO.is-win; #OUTPUT: «True␤»





No, I wouldn't do that as that is the very definition of "action-at-a-distance": this would then apply to all code running for all threads. The correct way to do this is by doing my $*DISTRO = Distro.new(:is-win,:release<11>,:path-sep('|||'),:auth<unknown>,:name<mswin32>,:desc<Test>,:version<v11>); in the scope that you need to have $*DISTRO pretend that it's a Windows system.
– Elizabeth Mattijsen
Aug 8 at 9:44



my $*DISTRO = Distro.new(:is-win,:release<11>,:path-sep('|||'),:auth<unknown>,:name<mswin32>,:desc<Test>,:version<v11>);


$*DISTRO





Fixed, thanks for tuhe suggestion.
– jjmerelo
Aug 8 at 12:36






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS