-My one liners

    < cjac> I've been using -Mv5.10 to mean please define sub say ;
            is there a better way to do that?
    < cjac> what I really mean is use any version of perl with a
            pre-defined say and load that symbol into main::
    < thrig> perl -E 'say "yes"'
    < cjac> > like -e, but enables all optional features
    < cjac> but... do I want *all* optional features?

Turning on only 'use feature "say"' requires a bit more work; one method is to put a "y.pm" file somewhere in the Perl library search path and therein, among other things, shove "say" into main, along with any other imports you want, and the other code you want.

    # y.pm - for -My one liners
    package y;
    use Import::Into;

    sub import {
        feature->import::into( scalar caller, 'say' );
        File::Spec::Functions->import::into( scalar caller,
            qw{catdir catfile} );
        File::Temp->import::into( scalar caller, qw{tempdir tempfile} );
    }

    *main::hkv = sub (+) {
        my ($href) = @_;
        my @s;
        for my $k ( sort keys %$href ) {
            push @s, join $, // "\t", $k, $href->{$k};
        }
        join $/, @s;
    };

    1;

And then you can run -My one liners:

    $ perl -e 'say "foo"'
    String found where operator expected (Do you need to predeclare "say"?) at -e line 1, near "say "foo""
    syntax error at -e line 1, near "say "foo""
    Execution of -e aborted due to compilation errors.
    $ perl -My -e 'say "foo"'
    foo

There are various other shortly named modules on CPAN ("ojo.pm" for Mojo one-liners via -Mojo, etc).

There's also a PERL5OPT environment variable where you can say you want -My or whatever set with every Perl command, but I don't use that as I figure I'd forget about it and then get surprised, and remembering to add -My when I need that extra stuff is easy.

Proxy Information
Original URL
gemini://thrig.me/blog/2024/12/20/My.gmi
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
1011.570266 milliseconds
Gemini-to-HTML Time
0.268078 milliseconds

This content has been proxied by September (ba2dc).