Wprowadzenie do poleceń Perla

Perl to język programowania. Wcześniej zaprojektowany tylko do edycji tekstu, jest teraz używany do wielu celów, takich jak administracja systemem w Linuksie, tworzenie stron internetowych, programowanie sieciowe itp. Głównym architektem i twórcą Perla jest Larry Wall. Został stworzony w 1987 roku i nadal jest używany jako główny język programowania. Perl jest językiem wysokiego poziomu. Jest to również interpretowany i dynamiczny język programowania. Teraz nauczymy się szczegółowo poleceń Perla.

Podstawowe polecenia Perla

1. Podstawowe polecenie Perla do drukowania w Perlu

#!/usr/bin/perl
# This will print "Hello, World"
print "Hello, world\n";

2. Komentarz jednowierszowy w Perlu

#!/usr/bin/perl
# This is a single line comment
print "Hello Perl\n";

3. Komentarz do wielu wierszy w Perlu

#!/usr/bin/perl
=begin comment
This is a multiline comment.
Line 1
Line 2
Line 3
We can insert
as much lines
as comments
until we code =cut
to end multiline comments
=cut
print "Hello Perl\n";

4. Przypisanie zmiennych w Perlu (interpolacja zmiennych podwójnie cytowanych)

#!/usr/bin/perl
$a = 10;
print "Variable a = $a\n";

5. Ucieczka postaci w Perlu

#!/usr/bin/perl
$a = "This is \"Perl\"";
print "$a\n";
print "\$a\n";

6. W Perlu łańcuchy mają różne zachowanie z podwójnymi i pojedynczymi cudzysłowami. Podczas gdy podwójne cudzysłowy umożliwiają interpolację, pojedyncze cudzysłowy nie.

#!/usr/bin/perl
# Interpolation example.
$str = "Hello \nPerl";
print "$str\n";
# Non-interpolation example.
$str = 'Hello \nPerl';
print "$str\n";

7. Wielkie litery w poleceniu Perla

#!/usr/bin/perl
# Only u will become upper case.
$str = "\uhello perl";
print "$str\n";
# All the letters will become Uppercase.
$str = "\Uhello perl";
print "$str\n";
# A portion of string will become Uppercase.
$str = "hello \Uperl\E";
print "$str\n";

8. Przypisanie zmiennej skalarnej w Perlu

#!/usr/bin/perl
$age = 35; # Assigning an integer
$name = "Tony Stark"; # Assigning a string
$pi = 3.14; # Assigning a floating point
print "Age = $age\n";
print "Name = $name\n";
print "Pi = $pi\n";

9. Proste operacje skalarne w Perlu

#!/usr/bin/perl
$constr = "hi" . "perl";# Concatenates two or more strings.
$add = 40 + 10; # addition of two numbers.
$prod = 4 * 51;# multiplication of two numbers.
$connumstr = $constr . $add;# concatenation of string and number.
print "str = $constr\n";
print "num = $add\n";
print "mul = $prod\n";
print "mix = $connumstr\n";

10. Literały specjalne w Perlu

#!/usr/bin/perl
print "Current file name ". __FILENAME__ . "\n";
print "Current Line Number " . __LINENO__ ."\n";
print "Current Package " . __PACKAGENAME__ ."\n";
# here they cannot be interpolated
print "__FILENAME__ __LINENO__ __PACKAGENAME__\n";

Pośrednie polecenia Perla

1. Tablice w Perlu

Indeks tablicy zaczyna się od 0. Indeks ujemny wskazuje elementy z ostatniej pozycji. Przykład poniżej.

#!/usr/bin/perl

@weekday = qw/Mon Tue Wed Thu Fri Sat Sun/;

print "$weekday(0)\n";
print "$weekday(1)\n";
print "$weekday(2)\n";
print "$weekday(6)\n";
print "$weekday(-1)\n";
print "$weekday(-6)\n";

2. Tablice elementów w sekwencji

#!/usr/bin/perl
@oneToTen = (1..10);
@fiftyToSeventyfive = (50..75);
@aToZ = (a..z);
print "@oneToTen\n"; # Prints one to ten
print "@fiftyToSeventyfive\n"; # Prints fifty to seventy five
print "@aToZ\n"; # Prints from a to z

3. Dodawanie i usuwanie elementów tablicy

#!/usr/bin/perl
# creating an array
@expression = ("happy", "sad", "angry");
print "1. \@expression = @expression\n";
# add element to the end of the arraypush(@expression, "jolly");
print "2. \@expression = @expression\n";
# add element to the beginning of the arrayunshift(@expression, "excited");
print "3. \@expression = @expression\n";
# remove element to the last of the array.pop(@expression);
print "4. \@expression = @expression\n";
# remove element from the beginning of the array.shift(@expression);
print "5. \@expression = @expression\n";

4. Hashe w Perlu

Hash jest koncepcją pary kluczowych wartości. Poniżej znajduje się przykład tworzenia skrótu.

#!/usr/bin/perl
%data = ('Mohan Singh' => 55, 'Ram Gupta' => 25, 'Bhuvan Kumar' => 31);
@age = values %data;
print "$age(0)\n";
print "$age(1)\n";
print "$age(2)\n";

5. Dodawanie i usuwanie elementu mieszającego

#!/usr/bin/perl
%data = ('Mohan Singh' => 55, 'Ram Gupta' => 25, 'Bhuvan Kumar' => 31);
@keys = keys %data;
$size = @keys;
print "a - Hash size: $size\n";
# add an element to the hash;
$data('Imran Khan') = 44;
@keys = keys %data;
$size = @keys;
print "b - Hash size: $size\n";
# delete an element from the hash;
delete $data('Imran Khan');
@keys = keys %data;
$size = @keys;
print "c - Hash size: $size\n";

6. Warunkowe stwierdzenie w Perlu: jeśli… elsif… else

#!/usr/local/bin/perl
$num = 50;
# check condition using if statement
if( $num == 40 ) (
# print the following if true
printf "num has a value which is 20\n";
) elsif( $num == 60 ) (
# else print if the next condition is true
printf "num has a value which is 30\n";
) else (
# if none is true print following
printf "num has a value which is $num\n";
)

7. Oświadczenie warunkowe w Perlu: chyba, że… elsif… wyrażenie inne

#!/usr/local/bin/perl,
$num = 50;
# check condition using unless statement
unless( $num == 25) (
# if condition is false then print the following
printf "num has a value which is not 25\n";
) elsif( $num == 55) (
# if condition is true then print the following
printf "num has a value which is 55";
) else (
# if both the condition is dissatisfied, print the
original value
printf "num has a value which is $num\n";
)

8. Pętle w Perlu: pętla while

#!/usr/local/bin/perl
$i = 1;
# while loop
while( $i < 5 ) (
printf "Value of i: $i\n";
$i = $i + 1;
)

9. Pętle w Perlu: Until Loop i For Loop

#!/usr/local/bin/perl
$i = 1;
# until loop
until( $i > 5 ) (
printf "Value of i: $i\n";
$i = $i + 1;
)
# for loop
for ($j = 0; $j < 3; $j++) (
printf "Value of j: $j\n";
)

10. Pętle w Perlu: do… while Loop

#!/usr/local/bin/perl
$i = 10;
# do…while loop
do(
printf "Value of i: $i\n";
$i = $i + 1;
)
while( $i < 20 );

Zaawansowane polecenia Perla

1. Programowanie gniazd w Perlu: serwer

#!/usr/bin/perl -w
# Filename : server.pl
use strict;
use Socket;
# use port 8081 as default
my $port = shift || 8081;
my $protocol = getprotobyname('tcp');
my $server = "localhost"; # Host IP running the
server
# create a socket, make it reusable
socket(SOCKET, PF_INET, SOCK_STREAM,
$protocol)
or die "Can't open socket $!\n";
setsockopt(SOCKET, SOL_SOCKET,
SO_REUSEADDR, 1)
or die "Can't set socket option to SO_REUSEADDR
$!\n";
# bind to a port, then listen
bind( SOCKET, pack_sockaddr_in($port,
inet_aton($server)))
or die "Can't bind to port $port! \n";
listen(SOCKET, 5) or die "listen: $!";
print "SERVER started on port $port\n";
# accepting a connection
my $client_addr;
while ($client_addr = accept(NEW_SOCKET,
SOCKET)) (
# send them a message, close connection
my $name = gethostbyaddr($client_addr,
AF_INET );
print NEW_SOCKET "Smile from the server";
print "Connection recieved from $name\n";
close NEW_SOCKET;
)

2. Programowanie gniazd w Perlu: klient

!/usr/bin/perl -w
# Filename : client.pl
use strict;
use Socket;
# initialize host and port
my $host = shift || 'localhost';
my $port = shift || 8081;
my $server = "localhost"; # Host IP running the
server
# create the socket, connect to the port
socket(SOCKET, PF_INET, SOCK_STREAM, (getproto
byname('tcp'))(2))
or die "Can't create a socket $!\n";
connect( SOCKET, pack_sockaddr_in($port,
inet_aton($server)))
or die "Can't connect to port $port! \n";
my $line;
while ($line = ) (
print "$line\n";
)close SOCKET or die "close: $!";

3. Łączność z bazą danych za pomocą Perla

#!/usr/bin/per
use DBI
use strict;
my $driver = "mysql";
my $database = "DBTEST";
my $dsn = "DBI:$driver:database=$database";
my $userid = "user123";
my $password = "pass123";
my $dbh = DBI->connect($dsn, $userid, $password
) or die $DBI::errstr;

4. Programowanie CGI przy użyciu Perla

#!/usr/bin/perl
print "Content-type:text/html\r\n\r\n";
print '';
print '';
print 'Hello Perl - CGI Example';
print '';
print '';
print '

Cześć Perl! To jest przykład programu CGI

„;
drukuj '';
drukuj '';
1;

Wskazówki i porady dotyczące używania poleceń Perla

Mówi się, że Perl jest mieszanką wszystkich języków, tzn. Jest wyposażony w najlepsze funkcje głównych języków programowania. Najważniejszym aspektem jest opanowanie podstaw i kontynuowanie praktyki tego języka. Ulepszenie i samodoskonalenie jest kluczem do sukcesu.

Wniosek

Powyższe programy są próbkami, które pomogą jednostce zrozumieć podstawy i przejść do samodoskonalenia. Mówi się, że jest to brzydki język programowania, ale w rzeczywistości zawiera wiele różnych funkcji. Zaleca się postępowanie zgodnie z tym dokumentem, aby skompilować kody i zrozumieć, co dzieje się w samym programie.

Polecane artykuły

To był przewodnik po poleceniach Perla. Omówiliśmy tutaj podstawowe, natychmiastowe i zaawansowane polecenia Perla. Możesz także spojrzeć na następujący artykuł, aby dowiedzieć się więcej-

  1. Zastosowania poleceń Tableau
  2. Jak korzystać z poleceń HBase
  3. Polecenia MongoDB
  4. Znaczenie poleceń świni
  5. Programowanie gniazd w Pythonie