Re: beginners trap: one liner
by pryrt (Abbot) on Jan 03, 2020 at 18:48 UTC
|
If you are in linux, you might want to add a newline; for example, perl -le 'print("aaa");' -- adding the -l auto-adds newline; useful for oneliners. (edit: without the newline, the aaa and the next command-line prompt will be next to each other, like aaa[##]Here'sMyCommandLinePrompt:)
If you are in windows, single quote ' is not a valid quote mark for the command line, so swap quotes around: perl -le "print('aaa');"
| [reply] [d/l] [select] |
|
Actually, the behavior on Windows (10, 1903 edition) appears to depend on one's shell.
I ran toohoo's test in different "command windows" on my Windows laptop.
Here's what I found:
- Command prompt (CMD): printed nothing with a line feed.
- Git bash: printed the message and added a line feed.
- WSL: bash prompt: printed the message with no line feed.
- PowerShell prompt1: printed nothing.
I don't think these results surprise anyone familiar with how Windows has implemented some of these things; however, the variation may surprise a few others.
(Oh, should mileage vary [likely], I suppose it's worth mentioning that I have two copies of perl installed on my laptop: the one provided with git bash and a relatively recent copy of Strawberry perl.)
Hope this helps...
-- f
1 - There are multiple versions of PowerShell available; I use generally use Core, but the same behavior was seen in the Desktop version as well.
| [reply] |
|
| [reply] |
|
|
|
|
| [reply] |
Re: beginners trap: one liner
by AnomalousMonk (Archbishop) on Jan 03, 2020 at 19:19 UTC
|
See perlrun for command line invocation and switch info. That's perldoc perlrun from the command line.
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] [select] |
Re: beginners trap: one liner
by LanX (Saint) on Jan 03, 2020 at 21:57 UTC
|
You are most likely using another CLI than a Unix terminal.
I know it's annoying but Perl has no power over the external quoting rules.
If you're primarily interested in testing short code try starting the debugger with perl -de0
HTH! :)
| [reply] [d/l] |
Re: beginners trap: one liner
by harangzsolt33 (Chaplain) on Jan 04, 2020 at 02:01 UTC
|
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. | [reply] [d/l] |
|
With one-liners, you can:
- 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.
- Utilize shell's tab completion to complete filenames, etc.
- Utilize shell's other features like brace expansion, environment variable expansion, etc.
- Pipe the output of script to other command; or receive other command's output as script's input.
- Easily redirect output of script to file.
- Store the previous commands in the shell history file.
Among others :-)
| [reply] |
|
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:\>
| [reply] [d/l] |
|
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.
| [reply] |
|
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.
| [reply] [d/l] |
|
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.
| [reply] [d/l] [select] |