Perl5 Undef Value and Basic Input-Output Commands #3

in blurtdevelopment •  2 years ago 

What Will I Learn?
Undef Value
Input - Output Commands
Display Data on the Screen

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

Difficulty
Intermediate

perl.png

Undef Value
When writing programs with Perl, you can use undefined variables in your assignment statements (although this is not true.) For example, the first statement of a Perl program

$row_count = $row_count + 1 ;

can. Although this is a very bad programming style, the command works correctly. In fact, Perl notions have a special undef value unless you assign them any value. If the variable is a numeric variable undef means zero. If the variable is a character string, undef is treated as a null string. Attention! The "empty array" and the "array with spaces" are completely different. So ""! = "".

$row_count = $row_count + 1;

If a value has not been previously assigned to the $row_count variable, the row_count = 1.

$name_lastname . = $lastname;

If a value has not been previously assigned to $name_lastname, the value of $name_lastname will be the same as the value of $lastname.

Examples

1. Program which lists numbers and cries from one to five:

#!/usr/bin/perl
# The location of the Perl interpreter on your computer may be different.
$count = 1;
while ( $count <= 5 )   {
    print $count . " - " . $count * $count . "\n";
    ++$count;
}

In this example I made use of an important conversion feature, and in the print statement I ended the numeric variable $counter with a character string "-" as if it were a character string. Since Perl can do automatic transformations according to the meaning of the place where the variables are used, it will convert the numeric value of $counter to a character string first and then concatenate it with "-" without any problems.

$eg1.pl
1 - 1
2 - 4
3 - 9
4 - 16
5 - 25
$



2. Examples of variable type conversions:

#!/usr/bin/perl
$num_1 = 2;                                                          # numerical variable
$num_2 = 4;
$num_1 = "Perl";                                               # character string
$num_2 = "10";
$double_line = "\n\n";                                   # character string

print $num_1, "\n";                                         # displayed on screen 2
print $num_1 + num_2, "\n";                      # displayed on screen 6
print "$num_1 + $num_2\n";                     # displayed on screen 2 + 4
print ("$num_1 + $num_2\n");                  # displayed on screen 2 + 3

print "$num_2\n";                                           # 4 is displayed on the screen for double quotes
print '$num_2\n';                                            # The number $num_2\n is displayed on the screen because it is a single track
print "\n";                                                            # Inline

In the codes we wrote above, we expect to perform the arithmetic operation first, then the print operation, that is, the display 11.

print $str_2 + 1, "\n";

# We expect the screen to display Perl-> 5.

print $str_1 . "->" . $str_2/2 . "\n";

# Hmm .. will the screen show Perl-> 5 or Perl-> 10/5?

print $str_1 . "->" . $str_2/2 . "\n";

print $str_1;
print $str_2;

# We have not printed \ n still. Let's print two lines at once lol

print $double_line;

Basic Input-Output Commands
It will be easier to understand the examples I will give later; I would like to touch on the basic input-output commands used to read something from the keyboard and display something on the screen, so that the reader can immediately start the experiment. I will be talking about later in my writings than the advanced input-output statements I'm sure you'll find very interesting.

On Screen Display Command: print

The most basic output command is print.

print "Hello";

Displays the Hello word on the screen.

print "Hello";
print "Blurt!";

expresses the "HelloBlurt" sequence on the screen. As you note, there is no space between the words because we did not give a command like that.

Display properly "Hello Blurt!" for viewing

print "Hello Blurt!";

or

print "Hello ";
print "Blurt";

or

print "Hello";
print " Blurt";

can be used.

Use "\n" for line control, ie carriage return or line feed.

E.G.:

print "Hello Blurt!\n==========\n\n";

the screen says,

Hello Blurt!
==========

will display.

When you want to display the value of a variable, for example;

$num = 32;
print $num;

32, the screen will be displayed.

print "Row count", $num;
# The command
Row Count = 32

It will display. In the above example, if there is no specific reason in print statements, it seems that there is a lack. Both do not make a carriage return after displaying what should be displayed on the screen. If this is the case with the program, there is no problem, but if you want each print statement to produce a line by itself, then these statements,

print $num, "\n";
print "Row count=", $num, "\n";

should be. I usually use the print command as follows:

print = "Row count=$num\n";

Since the statements between the double quotes (") are passed through a preliminary evaluation against possible placement, the value of this variable will be inserted instead of $num before the Row count = $num\n sequence is displayed, , I'm getting rid of the comma.

Thanks for reading, we will learn Perl Listings in the next post.

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!