http://qs1969.pair.com?node_id=11137700


in reply to OS agnostic C&P CLI snippets with -E

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