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

Re: Pre-RFC: A built-in ability for lexical import/export

Thread Previous | Thread Next
From:
Dagfinn Ilmari Mannsåker
Date:
June 27, 2022 14:00
Subject:
Re: Pre-RFC: A built-in ability for lexical import/export
Message ID:
87czeukxzx.fsf@wibble.ilmari.org
"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.

- ilmari

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