The following script:
#!/usr/bin/env perl
#mytest.pl
no warnings;
$bar = "this";
@bar = qw/ 1 2 3 4 5 /;
%bar = qw/ key value /;
open bar, '<', 'mytest.pl' or die $!;
sub bar {
return "Sub defined as 'bar()'";
}
$main::{foo} = $main::{bar};
print "The scalar \$foo holds $foo\n";
print "The array \@foo holds @foo\n";
print "The hash \%foo holds ", %foo, "\n";
my $line = <foo>;
print "The filehandle 'foo' is reads ", $line;
print 'The function foo() replies "', foo(), "\"\n";
Outputs:
The scalar $foo holds this
The array @foo holds 1 2 3 4 5
The hash %foo holds keyvalue
The filehandle 'foo' is reads #!/usr/bin/env perl
The function foo() replies "Sub defined as 'bar()'"
So if *main::foo = *main::bar;
doesn't do the same thing as $main::{foo} = $main::{bar};
, I'm at a loss as to how to detect a practical difference. ;) However, from a syntax perspective, there may be situations where it's easier to use one method versus another. ...the usual warnings about mucking around in the symbol table always apply.
{no strict 'refs'; *{'some::pkg'.$new} = *{'some::pkg'.$old}
when$some::pkg::{$new} = $some::pkg::{$old}
would work just as well. Intuitively they seem the same, but things like ties/overloads/other magic could have "interesting" consequences if they handled the copy differently, either as a bug or an intended behavior. – Mackinnon