develooper Front page | perl.perl5.porters | Postings from June 2021

Re: RFC 0004 - defer {} syntax

Thread Previous | Thread Next
From:
Paul "LeoNerd" Evans
Date:
June 17, 2021 20:41
Subject:
Re: RFC 0004 - defer {} syntax
Message ID:
20210617214133.70bc7e6f@shy.leonerd.org.uk
On Thu, 17 Jun 2021 22:32:46 +0200
Branislav ZahradnĂ­k <happy.barney@gmail.com> wrote:

> words like __PACKAGE__, 

__PACKAGE__, along with __FUNC__, __LINE__, __FILE__, etc.. are all
dunder-shouties, as they are constants. The parser literally replaces
them with a string (or number, in the case of __LINE__):

  $ perl -MO=Deparse -ce 'my $file = __FILE__';
  my $file = '-e';

> AUTOLOAD

AUTOLOAD is one of those weird things that probably should be specified
with `sub`; it's supposed to be

  sub AUTOLOAD {
    ...
  }

just that the parser lets you get away without the `sub`.

> or taken from Ovid's Corinna, ADJUST

Ah; but ADJUST _is_ a phaser. It's a block of code whose _mere
presence at compile time_ is sufficient:

  LOOP: {
    class AClass {
      has $slot;
    
      last LOOP;

      ADJUST { $slot = 123; }
      method greet { say "Slot is $slot" }
    }
  }

  AClass->new->greet;
  Slot is 123

Even though the line on which the ADJUST phaser was declared was never
dynamically reached (because we `last`ed out before it got there),
nevertheless the mere declaration of it was sufficient for it to be
active; much like the subsequent `method`.

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About