in reply to Dynamically Calling a Subroutine

Thanks. It works! But I want to understand why it works. Can you explain what is happening in this line? *{'update_' . $n }->(); download I thought glob is only used for directories.

The glob() function is used to search for files.

On the other hand, perl has many different types: scalars, arrays, hashes, etc. And a perl program can happily have variables named $x, @x, and %x, and it will run without error. A typeglob is one of the types in perl. The typeglob *x represents all variables named 'x' in your program, e.g. $x, @x, %x, sub x {}, etc. In order to understand typeglobs, you need to read perlreftut first, so that you know what references are.

The code fragment:

*{'update_' . $n }

resolves to the typeglob: *update_test. The arrow following the brackets:

*{'update_' . $n }->

...dereferences the reference on the left of the arrow. The type of dereference is determined by what's on the right of the arrow:

*{'update_' . $n }->()

In this case, the left side of the arrow, i.e. the typeglob, is treated as a reference to a subroutine, and the subroutine is called. Compare to this:

#####use strict; use warnings; use 5.010; sub greet { my $greeting = shift; say $greeting; } %greet = ( a => 'hello', b => 'goodbye', ); my $code_ref = \&greet; $code_ref->('hello'); my $hash_ref = \%greet; say $hash_ref->{b}; *greet->('goodbye'); say *greet->{a}; --output:-- hello goodbye goodbye hello