spin($j) increments $j by editing its $_[0]. As perlvar says,
A function is free to do in-place modifications of @_ and change its caller's values.
upcase_in($v1, $v2); # this changes $v1 and $v2
sub upcase_in {
for (@_) { tr/a-z/A-Z/ }
}
Scalars are already passed by reference, so you can modify scalar arguments without using typeglob mechanism (skipped) by
referring explicitly to $_[0] etc.
Sorry if my advice was wrong.
| [reply] [d/l] [select] |
Sorry, there is one more thing: suppose, the process is just in one statement, not a loop:
copy ("$source", "$destination");
It does take long because the source is a big file on a network resource.
I tried it this way:
while (my $result = copy ("$source", "$destination")) {
spin($j);
}
But I don't quite like what is going on: it spins, then makes a long pause, then spings again... Is there any trick I could use to make them work as if in parallel?
| [reply] [d/l] [select] |
You can fork a new process and make it run the spinner, or create a thread running a spinning function. Link to an example solution was posted in Re: Spinning cursor while waiting for search/copy process to be finished.
Forking spinner can be easier to write, but it must be noted that you may have problems killing the spinner process on platforms not supporting fork syscall natively (win32, for example). Search safe signals win32 for more information. Update: child is not blocked in writing operation continiousely, so signals should not be a problem.
my $j = 0;
if ((my $pid = fork()) == 0) {
# this runs in child process
do {sleep 0.5; spin($j)} while 1;
# normally we should put some kind of exit statement here, but the lo
+op above is infinite
} elsif ($pid > 0) {
# parent/main process
...;
# put your loop here
kill 1, $pid; # stop the spinner process
waitpid $pid, 0; # this should help against zombies
} else {
# fork returned undef
die "fork: $!\n";
}
Sorry if my advice was wrong.
| [reply] [d/l] |