Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

oneliner cat like capability

by gradius85 (Novice)
on Oct 29, 2019 at 17:35 UTC ( #11108074=perlquestion: print w/replies, xml ) Need Help??

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

I am looking at doing and getting better with oneliners from the command line. I am trying to do the following for a 'cat' like replacement.

perl -pe 'print' dataoutput.txt

However, it prints out each lines twice, which is in the same order as in the file. What am I doing wrong? In addition, I did noticed the following works, but puts a new line between each output

perl -pe 'print "\n"' dataoutput.txt

But that is not what I want as well. This is to understand and learn the concepts of oneliners and get better with Perl

Replies are listed 'Best First'.
Re: oneliner cat like capability
by choroba (Archbishop) on Oct 29, 2019 at 17:49 UTC
    As documented in perlrun, -p already prints the input, so you can just use
    perl -pe "" dataoutput.txt

    Or, if you want to print explicitly, use -n which loops over the input lines but doesn't print:

    perl -ne 'print' dataoutput.txt

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

        ... or  -pe; (update: but see this!)


        Give a man a fish:  <%-{-{-{-<

      I see what I did wrong or why it works the way I have. However, why is it important to have the 'n' or 'p' before the 'e'? Thank you for the help.

        ... why is it important to have the 'n' or 'p' before the 'e'?

        Per perlrun, the  -e switch expects source text to follow, either with or without a space. The switch sequence  -en means that Perl is trying to execute "n". The effect of this is made more clear with strictures enabled (see strict):

        c:\@Work\Perl\monks>perl -wMstrict -en "print" foo get_bar() Bareword "n" not allowed while "strict subs" in use at -e line 1. Execution of -e aborted due to compilation errors. c:\@Work\Perl\monks>perl -wMstrict -ep foo get_bar() Bareword "p" not allowed while "strict subs" in use at -e line 1. Execution of -e aborted due to compilation errors.

        Update: See jwkrahn's example here of  -pe1 which gives  -e the source 1 (that's a digit 1) to evaluate within the source code framework established by the  -p switch.


        Give a man a fish:  <%-{-{-{-<

        It's not important. You can write the loop yourself:
        perl -e 'print while <>' -- dataoutput.txt

        It's just helpful: if you want to iterate over the input and print always, use -p; if you want to iterate but not print, use -n; otherwise, use neither.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: oneliner cat like capability
by Fletch (Chancellor) on Oct 29, 2019 at 18:27 UTC

    Another handy thing to know about is B::Deparse which will dump out the code as perl interprets it. If you use it with your oneliner you can see both print calls (from your argument to -e and the implicit one from -p in the generated continue block).

    $ perl -MO=Deparse -pe 'print' LINE: while (defined($_ = <ARGV>)) { print $_; } continue { die "-p destination: $!\n" unless print $_; } -e syntax OK

    Depending on what you're trying to figure out there's options to B::Deparse which can be useful like -p to add explicit parens everywhere (-MO=Deparse,-p,-q is another handy variant).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: oneliner cat like capability
by shmem (Chancellor) on Oct 29, 2019 at 17:51 UTC
    However, it prints out each lines twice, which is in the same order as in the file. What am I doing wrong?

    The -p switch implies print. See perlrun.

    Invoking perl with -p and an empty script body does the trick:

    perl -pe '' dataoutput.txt

    In your second example, you are just printing a newline character before the implicit print is done, hence the empty lines.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: oneliner cat like capability
by stevieb (Canon) on Oct 29, 2019 at 20:57 UTC
    "I am looking at doing and getting better with oneliners from the command line."

    If that's the case, I would highly recommend adding -wMstrict to your one-liners, right after the perl command, like so: perl -wMstrict -insert_other_stuff_here.

    It does appear through other responses that you're a beginner really trying to learn, so throwing the switches I've recommended into the mix while you're learning will provide you the benefit of learning both one liners, and the work involved in making full-blown Perl scripts and modules reliable and as trustworthy as possible. You want your code to operate flawlessly under use warnings (an expanded -w) and use strict (-Mstrict). Very few Perl coders trust code without these present.

    I know that using one-liners are typically for one-off type things, but for a relative newcomer to the language, a few extra keystrokes across one-liners will get you into the proper mindset so when you want to use them for more advanced things, you'll be able to do so on the fly quickly, and reliably.

      Thank you for the help and suggestions. Yes... I am new to Pearl and really trying to learn the language. I also appreciate the pointer for using 'strict' and 'warnings' to get into the proper mind set of using Perl.

        For all standard code strict and warnings or some non-core version of them like strictures is really necessary to sane code. For one-liners… it will help with learning but it will definitely get in the way of brevity and fun. I never use them on the command line unless vetting a weird issue and I use Perl on the command line every day.

Re: oneliner cat like capability
by Anonymous Monk on Oct 30, 2019 at 07:45 UTC
    Here is some really good homework:
    https://metacpan.org/source/BDFOY/PerlPowerTools-1.016/bin/cat
    
    When I type man which at osx it says:
    HISTORY
         The which command first appeared in FreeBSD 2.1.
    
    AUTHORS
         The which utility was originally written in Perl
    
    Reinventing wheels is great fun and practice. You never know "which" next great utility will be originally written in Perl! :) Keep on Truckin'
Re: oneliner cat like capability
by Random_Walk (Prior) on Oct 31, 2019 at 23:03 UTC

    Oneliner cat like capability ....

    Am I the only one that thought meouw ....?

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2021-05-12 20:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (132 votes). Check out past polls.

    Notices?