http://www.perlmonks.org?node_id=1226880

perl -we "package API{$|++;$'__=__PACKAGE__;$\"=','}print/J$__/?exit:q +q($_\r)for glob qq{{@{[A..P,I]}}}x4"

Note: Win32 double quotes!

Thanks Tux, thanks Eily

L*

PS changed the title from empty API package -- oneliner to the current

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re: empty API package -- oneliner
by bliako (Vicar) on Dec 07, 2018 at 12:42 UTC

    In unix, creating a string in double quotes will cause interpolation of whatever the shell thinks are shell variables, e.g.

    perl -we "$b=14; print $b" syntax error at -e line 1, near "=" Execution of -e aborted due to compilation errors.

    or:

    echo hello world perl -we "if($_=~/World/){print 111}" Unquoted string "world" may clash with future reserved word at -e line + 1. 111

    Although your posted one-liner causes no complains in my linux/bash, maybe is not working as expected since $_ has a special meaning in bash (and possibly other shells): it is the last parameter of the last command (more special variables here: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_05_02)

    So, Unquoted string "world" is caused by the interpolation of $_ by bash shell

    bw, bliako

Re: perl: we package API -- oneliner
by bliako (Vicar) on Dec 14, 2018 at 10:24 UTC
    Trick #8: $ENV{}
    
        When you're writing a one-liner using -e in the shell, you generally want to quote it with ', so that dollar signs inside the one-liner aren't expanded by the shell. But that makes it annoying to use a ' inside your one-liner, since you can't escape a single quote inside of single quotes, in the shell.
    
        Let's suppose we wanted to print the username of anyone in /etc/passwd whose name included an apostrophe. One option would be to use a standard shell-quoting trick to include the ':
    
        perl -F: -lane 'print $F[0] if $F4 =~ /'"'"'/' /etc/passwd
    
        But counting apostrophes and backslashes gets old fast. A better option, in my opinion, is to use the environment to pass the regex into perl, which lets you dodge a layer of parsing entirely:
    
        env re="'" perl -F: -lane 'print $F[0] if $F4 =~ /$ENV{re}/' /etc/passwd
    
        We use the env command to place the regex in a variable called re, which we can then refer to from the perl script through the %ENV hash. This way is slightly longer, but I find the savings in counting backslashes or quotes to be worth it, especially if you need to end up embedding strings with more than a single metacharacter.
    

    from https://blogs.oracle.com/linux/the-top-10-tricks-of-perl-one-liners-v2

      Or just use escapes, or if it's about passing something from the shell to Perl, the -s option...

      $ perl -sle 'print qq{\047\x27$x}' -- -x="'" ''' $ FOO='`()/~?*+#"@$%&'"'"; perl -sle 'print $x' -- -x="$FOO" `()/~?*+#"@$%&'