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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.