All this eval stuff looks overly complicated.

You can simply put a methods name into a variable, here $meth and call it dynamically

use strict; use warnings; use Data::Dump; my $o = new Class; my $count=0; for my $meth (qw/foo bar baz/) { $o->$meth($count++); } package Class; sub new { return bless {} } sub foo { my $self =shift; warn "foo: @_\n" } sub bar { my $self =shift; warn "bar: @_\n" } sub baz { my $self =shift; warn "baz: @_\n" }

out

foo: 0 bar: 1 baz: 2

If you have problems using a constant in this syntax, then why not simply copy it first into a variable? This will certainly improve readability.

NB: Readonly should work anyway!

HTH! :)

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

UPDATE:

and if you rather prefer unreadable syntax

my %meth = ( foo => "foo" ); $o->${\ $meth{foo} }($count++);

UPDATE2

this answers your original question:

use constant { STUFF => { 'BAR' => 'bar', }, }; my $key = "BAR"; $o->${ \ STUFF->{$key} }($count++);

Perl needs to see a $-sigil to accept a symbolic method name, that's why deref of a scalar ref ${ \ "bar" } is a workaround.

But you forgot to put the $key part inside the deref


In reply to Re: Calling a subroutine when part of call is a constant variable (symbolic method names) <2 updates> by LanX
in thread Calling a subroutine when part of call is a variable Contant by Anonymous Monk

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.