For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > October 2006 > use of variables









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author use of variables
news2003@wanadoo.es

2006-10-30, 7:09 pm

#!/usr/bin/perl

# assign variables and values
$a = 10;
$b = 5;

print "\n";
print "trail ". $a + $b . "\n"; # why this???

This produces :

$perl sample3.pl

5

If I change the last statement to

print "trail ". ($a + $b) . "\n"; # why this???

now the result is

$perl sample3.pl

trail 15

Why is that?

Thanks in advance for your help
John

Paul Lalli

2006-10-30, 7:09 pm

news2...@wanadoo.es wrote:
> #!/usr/bin/perl
>
> # assign variables and values
> $a = 10;
> $b = 5;
>
> print "\n";
> print "trail ". $a + $b . "\n"; # why this???
>
> This produces :
>
> $perl sample3.pl
>
> 5


You really shouldn't be asking us for help until you ask Perl for help.
Ask it to tell you when you do something wrong. Add the lines
use strict;
use warnings;
right under your shebang. Then run your program again. Now do you
see what's going wrong?

If you see it, but don't understand why, take a look at the precedence
of operators, found in the operator documentation:
perldoc perlop

Paul Lalli

Brian Raven

2006-10-30, 7:09 pm

news2003@wanadoo.es writes:

> #!/usr/bin/perl
>
> # assign variables and values
> $a = 10;
> $b = 5;
>
> print "\n";
> print "trail ". $a + $b . "\n"; # why this???
>
> This produces :
>
> $perl sample3.pl
>
> 5
>
> If I change the last statement to
>
> print "trail ". ($a + $b) . "\n"; # why this???
>
> now the result is
>
> $perl sample3.pl
>
> trail 15
>
> Why is that?


If you had included "use warnings;" (and "use strict;" as well of
course), then perl would have given you a hint as to the problem.

Additional hint: check out the precedence and associativity of the
operators that you are using in 'perldoc perlop'.

HTH

--
Brian Raven
Michael Perle

2006-10-30, 7:09 pm

news2003@wanadoo.es wrote:
> $a = 10;
> $b = 5;
>
> print "trail ". $a + $b . "\n"; # why this???
> $perl sample3.pl
> 5


The precedence of the plus might be lower than
the one of the string concatenation? I don't know,
but if so then the first statement would be like

print ("trail " . $a) + ($b . "\n");

where ($b + "\n") results in "5\n".

> If I change the last statement to
> print "trail ". ($a + $b) . "\n"; # why this???


Yes, that seems to confirm that I am right with the above.

MP
Brian Raven

2006-10-30, 7:09 pm

Michael Perle <michael_perle@yahoo.com> writes:

> news2003@wanadoo.es wrote:
>
> The precedence of the plus might be lower than
> the one of the string concatenation? I don't know,
> but if so then the first statement would be like
>
> print ("trail " . $a) + ($b . "\n");
>
> where ($b + "\n") results in "5\n".
>
>
> Yes, that seems to confirm that I am right with the above.


If you had checked 'perldoc perlop' you would have seen that those
operators have the same precedence.

HTH

--
Brian Raven
Joe Smith

2006-10-30, 7:09 pm

Michael Perle wrote:
> news2003@wanadoo.es wrote:
>
> print ("trail " . $a) + ($b . "\n");


Not quite. The "(" after "print" causes the "if it looks like a
function, it is a function" rule to be invoked. What you've
written is parsed as:

(print("trail " . $a)) + ($b . "\n");
=
(print "trail 10") + ("$b\n");
=
1 + "5\n";
=
Useless use of addition (+) in void context at /tmp/test.pl line 4.

-Joe
Paul Lalli

2006-10-30, 7:09 pm

Michael Perle wrote:
> news2003@wanadoo.es wrote:
>
> The precedence of the plus might be lower than
> the one of the string concatenation? I don't know,


Why are you guessing? Why aren't you just reading the documentation?
perldoc perlop
shows that they have the same precedence, and are left-associative.
Therefore the . operates first, then the +, then the second .

> but if so then the first statement would be like
>
> print ("trail " . $a) + ($b . "\n");


No, it would be:
print ( ("trail " . $a) + ($b . "\n") );
which is very different. Please enable warnings when you are testing
code. Your code there is the equivalent of:
(print ("trail " . $a) ) + ($b . "\n");

> where ($b + "\n") results in "5\n".


I have no idea what that means, because ($b + "\n") can *never* result
in "5\n". + returns numbers only.

>
> Yes, that seems to confirm that I am right with the above.


Uh. No. It really doesn't. Not sure why you think it does.

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com