in reply to Calling a subroutine when part of call is a variable Contant

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

  • Comment on Re: Calling a subroutine when part of call is a constant variable (symbolic method names) <2 updates>
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Calling a subroutine when part of call is a constant variable (symbolic method names) <2 updates>
by Anonymous Monk on May 27, 2017 at 01:27 UTC
    Sorry don't think this addresses my question

      In what way does it not address your question?


      Give a man a fish:  <%-{-{-{-<

      It answers it perfectly.

      Dear Monks, Thanks for all your suggestions. I appreciate the time taken to help out. The Perl community really is the best! I figured it out, this is all I needed to get my code to execute correctly.

      foreach my $val (keys ${\( STUFF() ) } ){ my $subroutine = ${\( STUFF() ) }->{ $val }; my $value = $self->xml()->$subroutine(); }
        Why not  my $subroutine =  STUFF->{ $val } ?
        foreach my $val (keys ${\( STUFF() ) } ){ ... }

        Here, keys is iterating over a hash reference. Quoth the docs (from 5.14):
            Starting with Perl 5.14, "keys" can take a scalar EXPR, which
            must contain a reference to an unblessed hash or array. The
            argument will be dereferenced automatically. This aspect of
            "keys" is considered highly experimental. The exact behaviour
            may change in a future version of Perl.
        And indeed, this feature was tried, found wanting, and finally cast into Outer Darkness with Perl version 5.24. (And similarly for values, each, push, pop and, I think, some others.) See also Postfix Dereference Syntax and circumfix dereference syntax, discussed therein.

        c:\@Work\Perl\monks>perl -wMstrict -le "print qq{perl version: $] \n}; ;; use constant { STUFF => { 'bizz' => 'foe', 'bazz' => 'fie', 'bozz' => 'fee', }, }; print ${\( STUFF() ) }; print STUFF; ;; foreach my $val (keys ${\( STUFF() ) } ){ my $subroutine = ${\( STUFF() ) }->{ $val }; print qq{A: '$val' -> '$subroutine'}; } ;; for my $k (keys %{ STUFF() }) { my $value = STUFF->{$k}; print qq{B: '$k' -> '$value'}; } " perl version: 5.014004 HASH(0x4340ac) HASH(0x4340ac) A: 'bozz' -> 'fee' A: 'bazz' -> 'fie' A: 'bizz' -> 'foe' B: 'bozz' -> 'fee' B: 'bazz' -> 'fie' B: 'bizz' -> 'foe'
        (The circumfix dereference syntax is more widely supported — and I can't give an example of postfix dereference syntax anyway!)


        Give a man a fish:  <%-{-{-{-<