in reply to solved: beginners trap: one liner

I tried this in Windows XP, and it worked fine:

C:\>perl -le "print('aaa'.chr(10));" aaa C:\>

I honestly don't understand the point of one liners. If I want to run a short perl instruction, I open Notepad2, type, save, and press Ctrl+L. That runs the perl script.

Replies are listed 'Best First'.
Re^2: beginners trap: one liner
by perlancar (Friar) on Jan 04, 2020 at 04:53 UTC
    With one-liners, you can:
    1. Edit previous command and re-execute quickly (Up arrow, edit, enter instead of Alt-Tab to Notepad2, edit, F2 or whatever, Ctrl-L). Okay, that's not much keystroke saving, but there's less windows/view switching.
    2. Utilize shell's tab completion to complete filenames, etc.
    3. Utilize shell's other features like brace expansion, environment variable expansion, etc.
    4. Pipe the output of script to other command; or receive other command's output as script's input.
    5. Easily redirect output of script to file.
    6. Store the previous commands in the shell history file.

    Among others :-)

Re^2: beginners trap: one liner
by LanX (Cardinal) on Jan 04, 2020 at 02:09 UTC
    with standard CMD Win10, and less cargo cult

    C:\>perl -e"print 'aaa'" aaa C:\>perl -E"say 'aaa'" aaa C:\>perl -E"say qq(with interpolation $])" with interpolation 5.024001 C:\>perl # OS independent, Perl reads input till __END__ print "Perl Version $]"; __END__ Perl Version 5.024001 C:\>

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^2: beginners trap: one liner
by Marshall (Canon) on Jan 04, 2020 at 07:41 UTC
    I very seldom use one liners. I don't know what Notepad2 is, but it probably offers the same kind of functions as my ancient Textpad 4. I use Textpad 4 for C and Perl development.

    I have a single F key to: save all open .pl and .pm files, compile current window, run it, capture stdout/error. I am not doing concurrent Unix and Windows development at the moment, but there are different ADDED: "command line" quoting rules that sometimes "hurt my brain".

    If I am testing something to determine the behaviour of Perl, I seldom do one test case. Usually one test will be followed by others. Instead of multiple one liners, I put those tests in a single .pl file and document the output after an __END__ tag or other methods as appropriate.

    I think Perl one liners are very appropriate for Perler's who are writing Perl everyday and who are experienced and fluent in the language. And where the code is some special "one shot" deal. I don't think any kind of one liner is appropriate for beginners.

    I am lazy in the sense that I don't like writing code more than once. If the code in the one liner is going to have continued use, then I prefer to put that code in a file with an appropriate name.

Re^2: beginners trap: one liner
by Laurent_R (Canon) on Jan 04, 2020 at 22:31 UTC
    I honestly don't understand the point of one liners.
    Really? Then I must assume that you haven't worked very much with real life data. You get all the time data that must be fixed. Like CSV data where all fields are within quote marks and your program did not expect that (or the opposite). Or simply Windows/Linux line endings mismatch. And so on.

    Besides, I don't know what notepad2 is exactly, but it seems to be a Windows application. Probably not very useful when you're working on a Linux server.

    To me, Perl one-liners are often a benediction to solve this kind of problems in just a few minutes.

    And, just this past week, I used Perl one-liners to modify in just a few minutes all the chapters of a full book in GitHub. To do it with the find-and-replace function of any editor would have taken at least five times more, and would have been much more error-prone.

Re^2: beginners trap: one liner
by Your Mother (Archbishop) on Jan 05, 2020 at 04:24 UTC

    I’m going to echo what Laurent_R said. I checked my history from today and I’ve got 257 one liners there. Everything from–

    perl -E 'sleep 60 * 115; qx{say "Dishes are done."}' # to ack '(quote|trade)_time' ~/trade.stream | perl -nE '/(\d+)/ and say $1 +' | sort > ~/sorted

    I think the disconnect is because WIN is a terribly crippled environment. I personally cannot understand how anyone puts up with it. Even the PowerShell… The funniest part is how often jeffa used to accuse me of being a M$FT shill because I defended Perl use on WIN as being good for Perl. I doubt he hates WIN even as much as I do but it’s none of my business what other people use and the more chairs at the table the better. But I digress; widely, wildly. :P Point is, I’m not even that good with the shell or with sysadmin stuff in general and knowing just a handful of commands and pipe techniques makes me drastically more productive than someone who doesn’t.

    TL;DR: I encourage you to explore one-liners and for sure try ack.