in reply to compiletime checking of valid method calls

Figured maybe it was possible to use fields, but only got it to work for functions, not methods... Strangely enough the error occurs at line 8.

package BOGUS ; use fields qw( new ) ; package TEST ; my BOGUS $b ; $b = fields::new("BOGUS") ; @{$b}{keys %TEST::} = @TEST::{keys %TEST::} ; sub new { my $class = shift ; my TEST $self = {} ; bless $self, $class ; print "Runtime!\n" ; bloeb() ; return $self ; } package main ; use strict ; use warnings ; sub tst { my TEST $x ; $x = TEST->new() ; $x->blo() ; } tst() ;

Replies are listed 'Best First'.
Re^2: compiletime checking of valid method calls
by LanX (Saint) on Apr 08, 2019 at 22:56 UTC
    Many thanks! :)

    I have to admit that the fields pragma has always been one of the most obscure parts of Perl for me.

    From the documentation it's supposed to only limit hash access. From the synopsis:

    # this will generate a compile-time error my Foo $foo = Foo->new; $foo->{zap} = 24;

    There is no mention of restricting methods, as far as I can see.

    And the mainstream says that directly accessing attributes should be totally avoided and better be done via getter and setter methods. ( Well I know that this rule is mostly ignored in Python)

    I have problems imagining what for the fields pragma was introduced

    Edit

    Oh I think I understand now what you are trying to do, limiting the hash access to the STASH of the class?

    That's ... Courageous! :)

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