Perl | Automatic String to Number Conversion or Casting Last Updated : 12 Feb, 2019 Comments Improve Suggest changes Like Article Like Report Perl has a different way to deal with the operators because here operator defines how the operands will behave but in other programming languages, operands define how an operator behaves. Casting refers to the conversion of a particular variable's data type to another data type. For example, if there is a string "1234" and after converting it to int data type the output will be an integer 1234. Conversion of string to integer in Perl has many ways. One is to use Typecasting, while the other one involves use of 'sprintf' function. Sometimes people do not use the word casting in Perl because the whole thing of conversion is automatic. Typecasting Type conversion happens when we assign the value of one data type to another. If the data types are compatible, then Perl does Automatic Type Conversion. If not compatible, then they need to be converted explicitly which is known as Explicit Type conversion. There are two types of typecasting: Implicit Typecasting: Implicit type conversion is done by the compiler itself. There is no need for the user to mention a specific type conversion using any method. The compiler on its own determines the data type of the variable if required and fixes it. In Perl when we declare a new variable and assign a value to it, it automatically converts it into a required data type. Example 1: Perl # Perl code to demonstrate implicit # type casting # variable x is of int type $x = 57; # variable y is of int type $y = 13; # z is an integer data type which # will contain the sum of x and y # implicit or automatic conversion $z = $x + $y; print "z is ${z}\n"; # type conversion of x and y integer to # string due to concatenate function $w = $x.$y; # w is a string which has the value: # concatenation of string x and string y print "w is ${w}\n"; Output: z is 70 w is 5713 Explicit Typecasting: In this conversion the user can cast a variable to a particular data type according to requirement. Explicit type conversion is required if the programmer wants a particular variable to be of a particular data type. It is important for keeping the code consistent so that no variable causes an error due to type conversion. Example: The following perform explicit typecasting where a string(or any data type) is converted to specified type(say int). Perl # Perl code to demonstrate Explicit # type casting # String type $string1 = "27"; # conversion of string to int # using typecasting int() $num1 = int($string1); $string2 = "13"; # conversion of string to int # using typecasting int() $num2 = int($string2); print "Numbers are $num1 and $num2\n"; # applying arithmetic operators # on int variables $sum = $num1 + $num2; print"Sum of the numbers = $sum\n"; Output: Numbers are 27 and 13 Sum of the numbers = 40 sprintf function This sprintf function returns a scalar value, a formatted text string, which gets typecasted according to the code. The command sprintf is a formatter and doesn't print anything at all. Perl # Perl code to demonstrate the use # of sprintf function # string type $string1 = "25"; # using sprintf to convert # the string to integer $num1 = sprintf("%d", $string1); $string2 = "13"; # using sprintf to convert # the string to integer $num2 = sprintf("%d", $string2); # applying arithmetic operators # on int variables print "Numbers are $num1 and $num2\n"; $sum = $num1 + $num2; print"Sum of the numbers = $sum\n"; Output: Numbers are 25 and 13 Sum of the numbers = 38 Comment More infoAdvertise with us Next Article Spring Boot Interview Questions and Answers S SrijaPandey Follow Improve Article Tags : Perl Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i 6 min read Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc 15+ min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi 6 min read What is an Operating System? An Operating System is a System software that manages all the resources of the computing device. Acts as an interface between the software and different parts of the computer or the computer hardware. Manages the overall resources and operations of the computer. Controls and monitors the execution o 9 min read Like