"Paul \"LeoNerd\" Evans" <leonerd@leonerd.org.uk> writes: > On Fri, 24 Jun 2022 16:44:27 +0100 > "Paul \"LeoNerd\" Evans" <leonerd@leonerd.org.uk> wrote: > >> I was thinking that maybe it should be just exposed as a builtin:: >> function itself, perhaps as >> >> builtin::lexically_export $name, \&func; > > I've had a bit of a go at implementing this: > > https://github.com/leonerd/perl5/tree/lexically-export > >> Perhaps it should take a whole kv-list of name/ref pairs, so you can >> export lots in one go: >> >> builtin::lexically_export >> one => \&one, >> two => \&two, ... > > It currently doesn't do this yet, but shouldn't be hard to add. I think it should support this, since it's common to export multiple things, and build up data structures of things to be exported, e.g.: my %exportables = ( ... ); sub import ($class, @items) { # [ validation here ] lexically_export %exportables{@items}; } instead of having to do lexically_export $_ => $exportables{$_} for @items; > In the process of writing it I've come up with some more questions, to > which I don't have an easy answer: > > * Should it support things other than functions? Should we support > > lexically_export VAR => \my $scalar; Yes. > * If so, should names be sigil-prefixed? > > lexically_export '$VAR' => \my $scalar, '&func' => sub {...}; Yes, but … > Instead, perhaps a compromise idea taken from the way Exporter.pm > works; which is to say that sigils would be required for > scalar/hash/array variables, but not required for regular functions. > > lexically_export > name => \&function, # a regular function > '$VAR' => \my $scalar, # a scalar variable > '@VAR' => \my @arr, # an array variable > '%VAR' => \my %hash, # a hash variable > > '&func' => \&func, # also permitted for symmetry, but not > # required > > I.e. the model being "if no sigil is specified, presume &"; and > additionally, error if the sigil doesn't match the thing being > referenced. Yes. > I think this feels sane - does anyone disagree? I think this is a good approach, for reasons of convenience, consistency, and safety. - ilmariThread Previous | Thread Next