Perl In The Command Line

1,291 views
1,248 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,291
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Perl In The Command Line

  1. 1. Perl in the Command Line
  2. 2. -c: Test Compiling <ul><li>-c : causes Perl to check the syntax of the program and then exit without executing it.
  3. 3. It will execute BEGIN , UNITCHECK , CHECK , and use blocks, because these are considered as occurring outside the execution of your program.
  4. 4. It will not execute INIT and END blocks. </li></ul>
  5. 5. -w, -W and -X: Warnings <ul><li>-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.
  6. 6. -W: Enables all warnings regardless of no warnings or $^W .
  7. 7. -X: Disables all warnings regardless of use warnings or $^W .
  8. 8. $> perldoc perllexwarn </li></ul>
  9. 9. -d: Debugging <ul><li>-d : This option puts you into the Perl debugger.
  10. 10. -d:DProf : executes the program using the Devel::DProf profiler, combined with dprofpp may help you to find where is your program slow.
  11. 11. $> perldoc perldebtut $> perldoc perldebug </li></ul>
  12. 12. -e: eval <ul><li>-e: allows you to define code to be executed by the compiler. </li><ul><li>$> perl -e 'print &quot;Hello World&quot;' &quot;Hello World&quot; program in the shell. </li></ul><li>-E : like -e, but enables all optional features </li></ul>
  13. 13. -M: module Import <ul><li>-M : This option imports a module. -Mmodule is the same has use module .
  14. 14. $> perl –Mdiagnostics script.pl Get better error message.
  15. 15. perl -MLWP::Simple -e 'print get($ARGV[0]), &quot; &quot;' http://perl.com Better as an alias. </li></ul>
  16. 16. -n: Looping <ul><li>-n : Perl assumes the following loop around your program: LINE: while (<>) { # Your code } </li></ul>
  17. 17. -n: Looping <ul><li>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. </li></ul>
  18. 18. -p: Looping with print <ul><li>-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. </li></ul>
  19. 19. -p: Looping with print <ul><li>The -n example $> perl -ne 's/PHP/Perl/g; print' in.txt
  20. 20. or the simpler $> perl -pe ' s/PHP/Perl/g ' in.txt
  21. 21. 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 </li></ul>
  22. 22. -i: edit in place <ul><li>$> perl -i -pe 's/PHP/Perl/g' in.txt Substitute every PHP occurence by Perl
  23. 23. -i[extension] : edit in place. Case extension exists the original file is renamed, otherwise the file is overwitten.
  24. 24. 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. </li></ul>
  25. 25. -l: Line-ending processing <ul><li>-l[octnum] : Automatic line-ending processing.
  26. 26. It assigns $ to have the value of octnum . Without octnum, $ will be assigned to $/ .
  27. 27. After </li><ul><li>chomp() everything on input
  28. 28. Adds $ (the output record separator) to each print </li></ul></ul>
  29. 29. -a and -F: Autosplitting <ul><li>-a: Splits the $_ whith a space while (<>) { @F = split(' '); # your code }
  30. 30. -F: choses a diferent separator
  31. 31. perl -F, -ane 'print join &quot; &quot;, @F' data.csv converts a csv into a tsv. Not really true but enough for example propose. </li></ul>
  32. 32. <ul><li>$> perldoc perlrun </li></ul>
  33. 33. Q&A

×