Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

OS agnostic C&P CLI snippets with -E

by LanX (Sage)
on Oct 17, 2021 at 18:25 UTC ( #11137651=perlquestion: print w/replies, xml ) Need Help??

LanX has asked for the wisdom of the Perl Monks concerning the following question:

Hi

we often exchange one-liners here or short snippets like the following.

That's very handy for quick demos without the need of a temp file. ( like from kcott here)

$ perl -E ' use strict; use warnings; use feature "declared_refs"; no warnings "experimental::declared_refs"; say ref my \$scalar; say ref my \@array; ' SCALAR ARRAY

unfortunately this sucks on Win/CMD because it doesn't like simple quotes°

D:\tmp\pm>perl -E'say $]' Can't find string terminator "'" anywhere before EOF at -e line 1.

The best OS agnostic approach - well the only I'm aware of - is typing to STDIN with a final __END__

D:\tmp\pm>perl -M5.012 use strict; use warnings; use feature "declared_refs"; no warnings "experimental::declared_refs"; say ref my \$scalar; say ref my \@array; __END__ SCALAR ARRAY

So far so good ... (well it's not so good for in-place editing but then a temp file might be better anyway)

But did you notice the -M5.012 I had to add? It means use 5.012 and it's there to compensate the -E which activated all current features. But simply using -E will deactivate reading from STDIN and say was added with 5.12 ... (details perlrun )

Question: Is there a shorter way to do this?

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

UPDATE

°) the truth is even far worse. One needs to replace the surrounding ' with " and all internal "..." with qq(...) and worry about the right delimiter.

Replies are listed 'Best First'.
Re: OS agnostic C&P CLI snippets with -E
by Corion (Patriarch) on Oct 17, 2021 at 18:46 UTC

    How about using the - magic file to tell Perl to read the script from STDIN? This saves you the __END__, and you can keep the -M5.012 to enable features:

    perl -M5.012 - use strict; use warnings; use feature "declared_refs"; no warnings "experimental::declared_refs"; say ref my \$scalar; say ref my \@array;

    You need to press ^Z on Windows and ^D on unixish OSes to end your input though.

      I'm afraid you misunderstood the question:

      I need a short way to translate given *n*x snippets into a Win compatible form °

      And -M5.012 already works, but its

      • far longer than -E and requires more typing
      • it's not accurate because -E actually means -Mfeature=:all which is even longer

      FWIW I can't combine -E with -

      D:\tmp\pm>perl -E - syntax error at -e line 1, at EOF Execution of -e aborted due to compilation errors.

      update

      The only way I see is to write a local pseudomodule E.pm which does the imports with -ME ...

      But this would only work for me and wouldn't help people trying out my snippets.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      °) well actually before all I want snippets here to be as much low barrier as possible.

Re: OS agnostic C&P CLI snippets with -E
by Fletch (Chancellor) on Oct 17, 2021 at 20:58 UTC

    Must . . . resist. Urge to link . . . to obligatory relevant Dilbert . . . overwhelming . . .

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      LOL! xD

      I know you didn't mean it that way, but I didn't intend to bash any OS in any way.

      I'm primarily interested in using Perl as universal and low barrier as possible.

      I'm surprised how many people here are very vocal about their tribal instincts regarding OS, IDE, other languages (including P6/Raku) or even REPL/Debugger.

      There was a time where I thought the defining quality of the Perl community was its tolerance and open mindedness, but maybe the composition changed to the worse.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      PS: FWIW: My job requires me to work on Win, but I'm still considering me very much a Linux person.

        but I'm still considering me very much a Linux person.

        If that's a consolation, so was Stanley Kubrick (ahead of his time as usual): https://retrocomputingforum.com/t/stanley-kubrick-wanted-a-unix-machine-for-christmas/2183 (via https://retrocomputingforum.com - no affiliation, just a non-retro follower)

        My job requires me to work on Win

        LanX, I am not a lawyer but that's probably a health hazard, like working with asbestos in the old days :)

        I'm surprised how many people here are very vocal about their tribal instincts regarding OS, ... other languages

        Count me in (*n[ui]x, !python) and I well hope it is not just a "tribal instinct". My motivation is simply to keep the world sanity count at Age_of_Enlightenment levels. No man is an island, someone out there using m$ affects me. e.g. when my internet banking supported only that dreaded 666, the internet explorer. Albeit, all above served with a big smiling face, I am not condensending yet Fletch++.

        Untested theory: using m$ increases entropy. Although keeping track on all of us seems lke it is reducing it (re: spying,filing&archiving).

        bw, bliako

Re: OS agnostic C&P CLI snippets with -E
by kcott (Bishop) on Oct 18, 2021 at 19:47 UTC

    G'day Rolf,

    For the most part, I provide complete scripts. Generally, you can use the [download] link, save the code, and run it as is; sometimes it may be necessary to change a local value, e.g. my $path = '...' (or similar).

    For code that is mostly simplistic and provided for demonstration purposes, I will, in the main, use this type of format: $ perl -E '...'

    In "Re: POD for use feature 'declared_refs' wrong", you'll see a whole series of these built up using very small changes. There was no way that I was going to create five separate (almost identical) scripts for this.

    The demonstration code is really just that; however, it's perfectly acceptable and reasonable to check that the results are the same on your OS, Perl version, or whatever. I'm certainly no MS-DOS expert; but the following seemed like a very easy way to achieve this.

    Get the version to which -E would refer; e.g.

    C:\Users\ken\tmp>perl -v This is perl 5, version 26, subversion 3 (v5.26.3) built for MSWin32-x +64-multi-thread ...

    Create a Perl script (e.g. pm_11137651_os_agnostic.pl) that starts with use <-E version>; (that's use 5.026; in this instance). Copy the ... part of perl -E '...' exactly and paste it after the first line. Save the file. Using the first piece of code from the OP, that should look like this:

    C:\Users\ken\tmp>more pm_11137651_os_agnostic.pl use 5.026; use strict; use warnings; use feature "declared_refs"; no warnings "experimental::declared_refs"; say ref my \$scalar; say ref my \@array;

    Then run it:

    C:\Users\ken\tmp>perl pm_11137651_os_agnostic.pl SCALAR ARRAY

    A verbatim copy of pm_11137651_os_agnostic.pl, on a Unix-style OS (Cygwin), acted in exactly the same way (except I needed cat instead of more).

    ken@titan ~/tmp $ cat pm_11137651_os_agnostic.pl use 5.026; use strict; use warnings; use feature "declared_refs"; no warnings "experimental::declared_refs"; say ref my \$scalar; say ref my \@array;
    ken@titan ~/tmp $ perl pm_11137651_os_agnostic.pl SCALAR ARRAY

    As I indicated, I'm no expert on the MS-DOS front. There may be quicker or better ways to do this; although, this method literally only took a few seconds. Perhaps, if this is needed often, some sort of script might be a valid choice.

    — Ken

      Hi Ken

      > but the following seemed like a very easy way to achieve this.

      Well, these are a handful more steps than just running

      perl COPIED CODE __END__

      As Corion pointed out you can even avoid the __END__ by typing C-z (C-c resp. Win).

      But when posting I prefer __END__ because C&P easily works for the reader.

      Even better this doesn't come anymore with your restriction of not using the single-quote in the code, because of your -E'...' delimiter.

      The purpose of this thread was finding a way to avoid typing use feature ':all' to replicate -E

      I think my 'alias' solution does it well for me locally to run -E code.

      And when posting code here, I can still expand for easy reproducibility on readers side.

      perl -Mfeature=:all CODE __END__

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re: OS agnostic C&P CLI snippets with -E (alias)
by LanX (Sage) on Oct 17, 2021 at 19:04 UTC
    Here at least a workaround by alias to make -E snippets locally work

    D:\tmp\pm>doskey perle=perl -Mfeature=:all D:\tmp\pm>perle say $] __END__ 5.032001

    update

    and here a (git)bash version

    lanx@MDXN00024043 MINGW64 ~ $ alias perle='perl -Mfeature=:all ' lanx@MDXN00024043 MINGW64 ~ $ perle say $] __END__ 5.026002

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: OS agnostic C&P CLI snippets with -E
by jo37 (Hermit) on Oct 17, 2021 at 18:56 UTC
    The best OS agnostic approach - well the only I'm aware of - is typing to STDIN with a final __END__

    IMHO this sucks even more than a debugger session.

    Greetings,
    -jo

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
      > IMHO this sucks even more than a debugger session.

      Well, as long as we can communicate here in OS agnostic ways, I'm afraid I don't care much what sucks you or not.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re: OS agnostic C&P CLI snippets with -E
by Anonymous Monk on Oct 18, 2021 at 13:19 UTC

    Small correction: say() was added in Perl 5.10.

    $ perl -M5.010  
    say $^V;
    __END__
    v5.10.1
    
Re: OS agnostic C&P CLI snippets with -E
by karlgoethebier (Abbot) on Oct 18, 2021 at 18:42 UTC

    Let me guess once more. What you really want is something like an interactive shell, right?

    But i fear this doesn't exist. Anyway, you might a look at Devel::REPL by the notorious Matt Trout and currently maintained by Ether.

    N.B.: The dependencies are inferior.

    Best regards, KGB

    «The Crux of the Biscuit is the Apostrophe»

        That's O.K. But from the friendly manual ibidem: «Reply is a lightweight, extensible REPL for Perl.» I’m a bit confused what you really want. Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Re: OS agnostic C&P CLI snippets with -E
by Anonymous Monk on Oct 18, 2021 at 09:23 UTC
    Question: Is there a shorter way to do this?

    Sure. Write your one liner, then post the output of -MO=Deparse

      how does that help?

        bliako: how does that help?

        puzzling. how does it not help?

        question was portable oneliners, Is there a shorter way to do this?

        yeah, enjoy your one liner, then let deparse write the portability version you share

        files are good

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11137651]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2022-01-29 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (74 votes). Check out past polls.

    Notices?