Programowanie w systemie UNIX/Perl

Z Wikibooks, biblioteki wolnych podręczników.

Perl

Pierwsze kroki[edytuj]

Wersja interpretera[edytuj]

W konsoli wpisujemy:

 perl -v

otrzymujemy:

This is perl 5, version 14, subversion 2 (v5.14.2) built for x86_64-linux-gnu-thread-multi
(with 80 registered patches, see perl -V for more detail)

Copyright 1987-2011, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

Pomoc[edytuj]

Sprawdzamy czy mamy interpreter perla. W konsoli wpisujemy:

perl -help

Otrzymujemy:

Usage: perl [switches] [--] [programfile] [arguments]
  -0[octal]         specify record separator (\0, if no argument)
  -a                autosplit mode with -n or -p (splits $_ into @F)
  -C[number/list]   enables the listed Unicode features
  -c                check syntax only (runs BEGIN and CHECK blocks)
  -d[:debugger]     run program under debugger
  -D[number/list]   set debugging flags (argument is a bit mask or alphabets)
  -e program        one line of program (several -e's allowed, omit programfile)
  -E program        like -e, but enables all optional features
  -f                don't do $sitelib/sitecustomize.pl at startup
  -F/pattern/       split() pattern for -a switch (//'s are optional)
  -i[extension]     edit <> files in place (makes backup if extension supplied)
  -Idirectory       specify @INC/#include directory (several -I's allowed)
  -l[octal]         enable line ending processing, specifies line terminator
  -[mM][-]module    execute "use/no module..." before executing program
  -n                assume "while (<>) { ... }" loop around program
  -p                assume loop like -n but print line also, like sed
  -s                enable rudimentary parsing for switches after programfile
  -S                look for programfile using PATH environment variable
  -t                enable tainting warnings
  -T                enable tainting checks
  -u                dump core after parsing program
  -U                allow unsafe operations
  -v                print version, patchlevel and license
  -V[:variable]     print configuration summary (or a single Config.pm variable)
  -w                enable many useful warnings
  -W                enable all warnings
  -x[directory]     ignore text before #!perl line (optionally cd to directory)
  -X                disable all warnings
  
Run 'perldoc perl' for more help with Perl.

Tryby pracy[edytuj]

interaktywny[edytuj]

Perl nie ma interaktywnej konsoli jak Python [1], ale można użyć:

  • debuggera: perl -de1 [2]
  • dodatkowego programu: perlconsole[3]

skryptowy[edytuj]

Tworzymy plik tekstowy, którego pierwszą linijką jest:

#!/usr/bin/perl

a następne zawierają polecenia perla, np.:

 print "Hello world!\n";

Cały skrypt wygląda tak:

#!/usr/bin/perl
 
 use warnings;
 
 print "Hello world!\n";

Uruchamianie skryptu[edytuj]

Są 2 sposoby:

  • bezpośrednio, jako plik wykonywalny, z niejawnym użyciem interpretera
  • pośrednio, z jawnym użyciem interpretera [4]

bezpośrednio[edytuj]

Plik zawierający skrypt perla zapisujemy z rozszerzeniem pl. Nadajemy mu prawa wykonywalności:

chmod +x m.pl


I uruchamiamy:

./m.pl

pośrednio[edytuj]

Plik uruchamiamy poprzez jawne wywołanie interpretera: [5]

perl m.pl

Przykłady[edytuj]

Kopiowanie plików[edytuj]

Jak połączyć wiele plików vcf w jeden (rozdzielone pustą linią):

#!/usr/bin/perl 
# Append all *.vcf files into 1 single vcf file
# https://productforums.google.com/forum/#!topic/gmail/NVFm9HGt1sc
# amit
opendir(DIR, ".");
@files = grep(/\.vcf$/,readdir(DIR));
closedir(DIR);
open (WRITEFILE, '>>all_in_one.vcf'); 
foreach $thisfile (@files)
{
   open (THISFILE, $thisfile); 
   while (<THISFILE>)
   {
   	print WRITEFILE "$_";
   }
   print WRITEFILE "\n";   
   close(THISFILE);
}
close(WRITEFILE);


Odnośniki[edytuj]

  1. How can I start an interactive console for Perl?
  2. Perl interacive
  3. perlconsole
  4. Polski FAQ
  5. MiniFaq - Ubuntu forum