Perl In The Command Line

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    Perl In The Command Line - Presentation Transcript

    1. Perl in the Command Line
    2. -c: Test Compiling
      • -c : causes Perl to check the syntax of the program and then exit without executing it.
      • It will execute BEGIN , UNITCHECK , CHECK , and use blocks, because these are considered as occurring outside the execution of your program.
      • It will not execute INIT and END blocks.
    3. -w, -W and -X: Warnings
      • -w : This turns on warnings that Perl will then give you if it finds any of a number of problems in your code. The -w option has been replaced by the use warnings pragma.
      • -W: Enables all warnings regardless of no warnings or $^W .
      • -X: Disables all warnings regardless of use warnings or $^W .
      • $> perldoc perllexwarn
    4. -d: Debugging
      • -d : This option puts you into the Perl debugger.
      • -d:DProf : executes the program using the Devel::DProf profiler, combined with dprofpp may help you to find where is your program slow.
      • $> perldoc perldebtut $> perldoc perldebug
    5. -e: eval
      • -e: allows you to define code to be executed by the compiler.
        • $> perl -e 'print "Hello World"' "Hello World" program in the shell.
      • -E : like -e, but enables all optional features
    6. -M: module Import
      • -M : This option imports a module. -Mmodule is the same has use module .
      • $> perl –Mdiagnostics script.pl Get better error message.
      • perl -MLWP::Simple -e 'print get($ARGV[0]), " "' http://perl.com Better as an alias.
    7. -n: Looping
      • -n : Perl assumes the following loop around your program: LINE: while (<>) { # Your code }
    8. -n: Looping
      • The command $> perl -ne 'print &quot;$. - $_&quot;' in.txt Would become LINE: while (<>) { print &quot;$. - $_&quot; } This code prints each line of the file together with the current line number.
    9. -p: Looping with print
      • -p : Perl assumes the following loop around your program: LINE: while (<>) { # your code goes here } continue { print or die &quot;-p destination: $! &quot;; } This uses a continue block on a while loop to ensure that the print statement is always called.
    10. -p: Looping with print
      • The -n example $> perl -ne 's/PHP/Perl/g; print' in.txt
      • or the simpler $> perl -pe ' s/PHP/Perl/g ' in.txt
      • It's very common to see command line programs that use Unix I/O redirection like this: $> perl -pe 's/PHP/Perl/g' in.txt > out.txt
    11. -i: edit in place
      • $> perl -i -pe 's/PHP/Perl/g' in.txt Substitute every PHP occurence by Perl
      • -i[extension] : edit in place. Case extension exists the original file is renamed, otherwise the file is overwitten.
      • If the extension doesn't contain a * , then it is appended to the end of the current filename. If the extension does contain one or more * characters, then each * is replaced with the current filename.
    12. -l: Line-ending processing
      • -l[octnum] : Automatic line-ending processing.
      • It assigns $ to have the value of octnum . Without octnum, $ will be assigned to $/ .
      • After
        • chomp() everything on input
        • Adds $ (the output record separator) to each print
    13. -a and -F: Autosplitting
      • -a: Splits the $_ whith a space while (<>) { @F = split(' '); # your code }
      • -F: choses a diferent separator
      • perl -F, -ane 'print join &quot; &quot;, @F' data.csv converts a csv into a tsv. Not really true but enough for example propose.
      • $> perldoc perlrun
    14. Q&A
    SlideShare Zeitgeist 2009

    + Marcos RebeloMarcos Rebelo Nominate

    custom

    161 views, 0 favs, 0 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 161
      • 161 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 1
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories