How to Create Variables in PERL

As a portable, simple and versatile language, PERL has become popular for working on web-based scripts and programs. Originally developed for UNIX, PERL has a straightforward structure and a limber way of dealing with data. You can see this philosophy at work in how users create variables in PERL. Making different variables, such as strings and numbers, lacks the rigidity or complexity of some other languages.

Instructions

    • 1

      Identify the type of variable. For a single variable, PERL uses the same variable type for a numeric variable or a string. For arrays, you can either create an array of just values, or an array with both keys and values, so that each value has its own variable name.

    • 2

      Define your simple variable with an = statement. For these, the command in PERL will be similar to one in Visual Basic, without a "dim" command to dimension the variable. In PERL, you can create a numeric scalar with a command like this: $myfirstnumber=44

    • 3

      Build a string scalar. With string variable definitions, you can use either single or double quotations, so you get the same results with these two commands: $foo="hello" OR $foo="hello"

    • 4

      Construct an array. If you want more than one value in a variable, you'll be defining an array using a command with a set inside parentheses: for an array of the values 3, 5, and 7 in PERL, you would do something like this: @myarray= (3, 5, 7) or for an array of strings: @myarray = ("three", "five", "seven"). You can also do without the commas and just list your strings delimited by quotation marks. For sequential numeric arrays, you can save time by creating of defining them with a command like this, for example from 1 to 100: @myarray=(1..100)

    • 5

      Create hashes with complex identifiers. Since a hash has variables with both string and numeric indicators, you'll need to list both. For example, for a hash with elements Joe=33, Gary=29 and Sue=50, your hash definition would be: %myhash= ("Joe", 33, "Gary", 29, "Sue", 50)

Related Searches:

Comments

You May Also Like

Related Ads

Featured