Perl5 Basic Concepts and Scalar Commands #1

in blurtdevelopment •  3 years ago 

What Will I Learn?

  • Basic Concepts
  • Perl Scalar
  • Numbers and Writing Styles

  • Requirements
  • Terminal or SSH
  • Linux/Unix Operating System or Linux Hosting

  • Difficulty
  • Intermediate
  • 1.webp

    Tutorial Contents
    Larry Wall "Perl is free!" compiled emphasizes that Perl is both "free" and "open-source." So if you do not have to give permission from anyone to install Perl on your computer or pay someone else's money. If you have Linux installed on your computer, Pel is probably already installed. If not, you can install it immediately with the following command.

    curl -L http://xrl.us/installperlnix | bash
    



    Basic Concepts
    As you know, UNIX shell programs are made up of UNIX commands sorted into a file. Running this shell program is equivalent to entering and running shell commands in the program files from the keyboard. For example; It's like BAT files in MS-DOS.
    The same concept applies to Perl; Perl programs are made up of a set of Perl commands, sorted into a file.
    Let's write a short Perl program immediately;

    $ cat > sdtyldz
    #!/usr/bin/perl
    print "Hello Blurt!\n";
    ^D
    
    $ chmod a+x sdtyldz
    $ ./sdtyldz
    Hello Blurt!
    $
    

    Let's take a look at these commands in order and explain what works.

    $ cat > sdtyldz
    

    With this command, you start to create a file with the name "sdtyldz" in your working directory and write the truth entered from the keyboard into the file.

    #!/usr/bin/perl
    

    This command reports that the following lines will be interpreted by the perl program in /usr/bin. Without this line; or rather, your Perl program will not work without correctly showing the directory where the Perl program is located. If you do not know which string your Perl program is in, you can learn $ with perl.

    print "Hello Blurt!\n";
    

    Yeah! Our first Perl command.
    The last "\ n", "Blurt!" after the word you want to make a carriage return says. The next ";" important. Perl specifies the end of the command line.

    ^D
    

    Control-D. The cat program tells you that the file you are typing from the keyboard is complete, meaning you will not enter more lines.

    chmod a+x sdtyldz
    

    Everyone ("a") is authorized to run ("x") for the sdtyldz file.

    ./sdtyldz
    

    The command that runs the program. You do not have to tell "./" at the beginning. Everyone who is enthusiastic about UNIX knows.

    Hello Blurt!
    

    The program worked successfully.

    Of course, not every Perl program will be that short; it is also not wise to attempt to write a program using the cat command. I would recommend using an editor such as vim or pico to write Perl code under UNIX. In order to have an idea of the Perl programming language, we start by looking at a Perl program listing usernames in the /etc/passwd file, where user accounts are stored on a UNIX system, with the codes of the system-defined users and their explicit names.

    #!/usr/bin/perl
    open(PWD,"etc/passwd") or die("/etc/passwd file could not be opened!\n");
    while (<PWD>) {
    ($code, $x1, $x2, $x3, $name) = split(":",$_);
    print $code, "-", $name, "\n";
    }
    close(PWD);
    

    For now I will not go into the details of this program, but I suggest you study carefully the lines for eye familiarity and try to understand what each line does. When examining what the program does, it is especially useful to look into the /etc/passwd file on line 4, where the split() function is used, with the UNIX command more /etc/passwd.

    Perl Scalar
    As you know, figures, numbers and character sequences are scalar values. Variables whose values they contain or can contain are called scalar functions.

    ggh.png

    Strings

    "a"
    'a'
    " "
    '123'
    

    A strings of characters with digits 1,2 and 3 in it.

    '123ab zyx'
    "Hello Blurt!\n"
    $a = "Hello Blurt!\n";
    

    We have assigned the character array "Hello Blurt!\n" to the variable $a scalar.

    The "\n" symbol is "newline heading"; that is, (Carriage Return, Line Feed). Perl, there are a few other codes with special meaning like "\n".

    bb.png

    The first lesson of our series ends here, in the next lesson we will see Single Quotation and Double Quotation writing styles and Arithmetic Operations. Thank you for reading, respectfully.

    Authors get paid when people like you upvote their post.
    If you enjoyed what you read here, create your account today and start earning FREE BLURT!
    Sort Order:  

    Hi, @sedatyildiz,

    Thank you for your contribution to the Blurt ecosystem.


    Please consider voting for the witness @symbionts.
    Or delegate to @ecosynthesizer to earn a portion of the curation rewards!