Open In App

Perl | print operator

Last Updated : 04 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

print operator in Perl is used to print the values of the expressions in a List passed to it as an argument. Print operator prints whatever is passed to it as an argument whether it be a string, a number, a variable or anything. Double-quotes(“”) are used as a delimiter to this operator. 
 

Syntax: print “”; or print(); or print(” “);
Returns: 
0 on failure and 1 on success.

Example 1: 
 

Perl




#!/usr/bin/perl -w
 
# Defining a string
$string = "Geeks For Geeks";
 
# Defining an array of Integers
@array = (10, 20, 30, 40, 50, 60);
 
# Searching a pattern in the string
# using index() function
$index = index ($string, 'or');
 
# Printing the position of matched pattern
print "Position of 'or' in the string $index\n";
 
# Printing the defined array
print "Array of Integers is @array\n";


Output: 

Position of 'or' in the string 7
Array of Integers is 10 20 30 40 50 60

 

Example 2: 
 

Perl




#!/usr/bin/perl -w
 
# Defining a string
$string = "Welcome to GFG";
 
# Defining an array of integers
@array = (-10, 20, 15, -40, 45, -60);
 
# Searching a pattern in the string
# using index() function
$index = index($string, 'o G');
 
# Printing the position of matched pattern
print "Position of 'o G' in the string $index\n";
 
# Printing the defined array
print "Array of Integers @array\n";


Output: 

Position of 'o G' in the string 9
Array of Integers -10 20 15 -40 45 -60

 

We can also use print with braces like print() to print something, it will act as same way as above, this is for those developers who are coming from other languages which has print() and not print “”.

Perl




#!/usr/bin/perl
# your code here
#!/usr/bin/perl -w
 
# Defining a string
$string = "Welcome to GFG";
 
# Defining an array of integers
@array = (-10, 20, 15, -40, 45, -60);
 
# Searching a pattern in the string
# using index() function
$index = index($string, 'o G');
 
# Printing the position of matched pattern
print("Position of 'o G' in the string $index\n");
 
# Printing the defined array
print("Array of Integers @array\n");


Output – 

 

Time Complexity - O(1).
Space Complexity - O(1).


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads