Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

solved: beginners trap: one liner

by toohoo (Beadle)
on Jan 03, 2020 at 18:43 UTC ( [id://11110903]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Perlmonks,

I tried to start write a Perl one liner. But I stumbled right on the first step.

perl -e 'print("aaa");'

So it does not print anything there must be an error. What did I oversee or forgot?

Thanks for the help in forehand, regards


Just to add the solution in 1st post:

Solution:

Under Windows use double quotes ",

perl -e "print('aaa');"

under Linux use single quotes'.

perl -e 'print("aaa");'

Replies are listed 'Best First'.
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');"

      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.

        I'm not aware of any native port of bash. The two you mention specifically run in unix emulations (MSYS and WSL specifically), not Windows.

      Hello @pryrt,

      Many thanks, this was it. Finally it will run under Linux. But now I develop under Win10.

      Best regards

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:  <%-{-{-{-<

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! :)

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

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.

      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 :-)

      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

      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.

      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.

      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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-23 17:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found