Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (0)

Powered by Vanilla. Made with Bootstrap.
Perl 101 - Part II.
  • rx-
    Posts: 169
    Basic Perl syntax and operations, simple arithmetic ( Scalar variables)

    Our example

    #!/usr/bin/perl
    # ^ We need to tell the compiler where the binary is located (only applies if you are running scripts with ./scriptname.pl, instead of perl scriptname.pl)

    my $variable1 = 1;
    my $variable2 = \"variable2\"; # Notice the \"
    my $variable3 = 'variable3'; # Notice the '
    my $variable4 = \"im not $variable3\"; # Notice the \"
    my $variable5 = 'im not $variable3'; # Notice the '

    print 'VARIABLE1: '.$variable1.\"\n\";
    print 'VARIABLE2: '.$variable2.\"\n\";
    print 'VARIABLE3: '.$variable3.\"\n\";
    print 'VARIABLE4: '.$variable4.\"\n\";
    print 'VARIABLE5: '.$variable5.\"\n\";
    # ^ \n instructs the compiler to print new line
    # ^ Notice how ' and \" is used in different situations through the code.


    Here you can see how a variable in Perl is assigned, how it is printed and how can it be concatenated. Now if you run the code, you will see this:

    VARIABLE1: 1
    VARIABLE2: variable2
    VARIABLE3: variable3
    VARIABLE4: im not variable3
    VARIABLE5: im not $variable3
    ^ IMPORTANT DIFFERENCE


    The explanation is really simple: When you want to print EXACTLY what is the value of a variable, you will use quote. However, if you want variables inside variable and special characters to apply, you will use pair of quotes.
    Now, to the arithmetics.

    Example code:

    #!/usr/bin/perl

    my $variable1 = 1 + 1;
    my $variable2 = 10 - 11;
    my $variable3 = 3 * 3;
    my $variable4 = 3 ** 4; # 3 to the power of 4 (3^4)
    my $variable5 = 5 / 2; # The outcome will be rounded
    my $variable6 = 5 % 2; # Remainder of 5 / 2

    my $variable7 = \"$variable1.' != '.$variable2\"; # Concatenation and punctuation once again ( Its the basics!)
    my $variable8 = $variable3 x $variable1; # Simple example: my $var = 3 x 9; this means it will print the number three 9 times

    print 'VARIABLE1: '.$variable1.\"\n\";
    print 'VARIABLE2: '.$variable2.\"\n\";
    print 'VARIABLE3: '.$variable3.\"\n\";
    print 'VARIABLE4: '.$variable4.\"\n\";
    print 'VARIABLE5: '.$variable5.\"\n\";
    print 'VARIABLE6: '.$variable6.\"\n\";
    print 'VARIABLE7: '.$variable7.\"\n\";
    print 'VARIABLE8: '.$variable8.\"\n\";


    Here are some basic arithmetic operations, here is the output:

    VARIABLE1: 2
    VARIABLE2: -1
    VARIABLE3: 9
    VARIABLE4: 81
    VARIABLE5: 2.5
    VARIABLE6: 1
    VARIABLE7: 2 != -1
    VARIABLE8: 99


    That sums up 2nd part of our Perl 101. I hope you enjoyed it, please post all comments in replys and all questions on IRC.
  • Xin
    Posts: 3,251
    Great tutorial! mind if i put these on the blog?
    Xin
  • rx-
    Posts: 169
    Yes, go on, its fine :)