nysus has asked for the wisdom of the Perl Monks concerning the following question:

Got this:

sub _run_chain { my $s = shift; my $sub = shift; for my $r ( @{ $s->{_classes} } ) { no strict 'refs'; $r->$sub if $r->can($sub); } }

But still getting: died: Can't use string ("File::Collector::Date::Classifie"...) as a HASH ref while "strict refs" in use

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Unable to turn off strict refs
by LanX (Saint) on Sep 04, 2019 at 00:51 UTC
    On a side note:

    strict "refs" has no influence whatsoever on method calls.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: Unable to turn off strict refs
by chromatic (Archbishop) on Sep 03, 2019 at 21:46 UTC

    It's probably failing on $s->{_classes}, but we'd have to see the calling code to know for sure.

      If I comment out the line the for loop, the script exits without error. But here is the calling code:

      $s->_run_chain('_run_processes');

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

        Then $s is probably a class name, not an object.

Re: Unable to turn off strict refs
by jcb (Parson) on Sep 04, 2019 at 02:43 UTC

    Based on our previous conversations, I will guess that you want to call every implementation of a method in the inheritance tree instead of only the most-specific?

    There is probably a module on CPAN somewhere for this, and perhaps another monk will be able to cite it, but I do not know. I suggest trying: (untested)

    sub _run_chain { my $s = shift; my $sub = shift; for my $r ( @{ $s->{_classes} } ) { $s->($r.'::'.$sub) if $s->can($r.'::'.$sub); } }

    This will override the method search to directly call each method. Because Perl is a highly dynamic language, I have removed the no strict and expect that this will not contain a symref.

    Your problem was not in this code at all — it was looking up $sub as a class method for each $r and then, finding that a sub by that name does indeed exist in that package, invoking it as a class method even though it is written as an instance method. You have been actually calling into File::Collector::Date::Classifie...::$sub and that is where the error is occurring. I think that running your code under the debugger would give you a nice backtrace showing the successful call and the unsuccessful dereference. (Simply run the script with perl -d and type c at the prompt to let the program run.)

    And another comment on OO design: subclass relationships and the package hierarchy are independent in Perl. It might make more sense to have a File::Collector::Sorter::Date::ByMonth mixin class that groups files by month, or your sorting classes may all make more sense somewhere in your application's local package hierarchy.

Re: Unable to turn off strict refs
by tybalt89 (Monsignor) on Sep 03, 2019 at 22:38 UTC

    What is the value of $s ?

      It's a big hairy object. I have the expected values for $r and $sub.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

        That's what you think it is. What your error message says is it's the string "File::Collector::Date::Classifie"...