Perldoc perlobj states:
Sometimes you want to call a method when you don't know the method name ahead of time. You can use the arrow form, replacing the method name with a simple scalar variable containing the method name
But what about when you know part of the method name ahead of time, just not all of it? I want to call a method using some known text plus a scalar variable. For example, see the method called on "$caller" in either of the following, neither of which works:
$$database_handle = DBI->connect(map {exists $arguments{$_} ? $argume +nts{$_} : $caller->"database_${_}"()} qw(data_source_name username p +assword taint)) or die "Could not connect to the database: $DBI::errs +tr";
... produces the error ...
String found where operator expected at Storage.pm line 46, near "->"d +atabase_${_}"" (Missing operator before "database_${_}"?)
... or maybe this will work ...
$$database_handle = DBI->connect(map {exists $arguments{$_} ? $argum +ents{$_} : $caller->database_$_()} qw(data_source_name username pas +sword taint)) or die "Could not connect to the database: $DBI::errst +r";
... nope ...
Scalar found where operator expected at Storage.pm line 47, at end of +line (Missing operator before ?) syntax error at Storage.pm line 47, near "->database_$_"
The only way I have found to make this work -- after several SuperSearches and trying perl5.6 and perl5.8 -- is to create a new simple scalar variable and use this as the method call, as such:
$$database_handle = DBI->connect(map {my $s="database_$_";exists $ar +guments{$ _} ? $arguments{$_} : $caller->$s()} qw(data_source_name username pass +word taint)) or die "Could not connect to the database: $DBI::errstr" +;
The above code compiles fine. It is a bit kludgy though. Is there any way to accomplish a method call that mixes an interpolated variable and some known text, sort of like a double quoted string?
In reply to Double quoted string as method call by ryantate
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |