Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Symbol::gensym still useful any more after perl 5.6?

Tags:

perl

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?

like image 730
new_perl Avatar asked Dec 26 '12 16:12

new_perl


1 Answers

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
like image 151
ikegami Avatar answered Nov 08 '22 21:11

ikegami