I saw such code:
my $fh = gensym;
open $fh, ">$name" or die "Can't create $name: $!";
which can be written as :
open my $fh, ">$name" or die "Can't create $name: $!";
Is gensym
just legacy or still useful in some occasions?
Legacy. Globs rather than lexicals sometimes required by older modules, but that's it.
use IPC::Open3 qw( open3 );
open(local *CHILD_STDIN, '<', '/dev/null') or die $!;
my $pid = open3(
'<&CHILD_STDIN',
my $CHILD_STDOUT = gensym(),
my $CHILD_STDERR = gensym(),
$cmd, @args,
);
On second thought, you can also use them to create aliases (though Data::Alias can do this with lexicals).
my $foo;
our $bar; local *bar = \$foo;
$foo = 123; say $bar; # 123
$bar = 456; say $foo; # 456
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With