in reply to Re^2: Why is "any" slow in this case?
in thread Why is "any" slow in this case?
That's how magic variables works. Every time you can read a variable with get magic, a getter function is called to populate it first. Every time you write to a variable with set magic, a setter function is called to process the new value afterwards.
use v5.40; use Variable::Magic qw( cast wizard ); my $wiz = wizard( get => sub { say sprintf 'getter called for %X', refaddr( $_[0] ); ${ $_[0] } = int( rand( 100 ) ); }, ); my $var; say sprintf '`$var` is %X', refaddr( \$var ); cast $var, $wiz; for ( 1 .. 4 ) { say "Loop: $_"; say "`\$var` has value $var"; }
`$var` is 5F5668128E20 Loop: 1 getter called for 5F5668128E20 `$var` has value 22 Loop: 2 getter called for 5F5668128E20 `$var` has value 62 Loop: 3 getter called for 5F5668128E20 `$var` has value 44 Loop: 4 getter called for 5F5668128E20 `$var` has value 70
The alternative to magic would be to preemptively copy substrings of the matched string into $`, $&, $' and $n, $+{name} and $-{name}.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Why is "any" slow in this case?
by LanX (Saint) on Jul 28, 2025 at 16:54 UTC | |
by ikegami (Patriarch) on Jul 28, 2025 at 18:18 UTC | |
by LanX (Saint) on Jul 28, 2025 at 20:33 UTC | |
by ikegami (Patriarch) on Jul 29, 2025 at 11:35 UTC |