Arijit Das wrote:
> I am just wondering why is this giving a strange result. Any clues...?
> 
> $ echo 4.56 | perl -p -e 'my $var1 = <STDIN>; $var2 = $var1 * 100;  print
$var2;' 
> 04.56
> $
> 
> I am expecting 456 in the ouput instead of 4.56
> 
> Am I missing anything...?

The '-p' option is causing the problem.  It is adding a loop into the
program.  It looks something like this:

    while (<>) {
    
        my $var1 = <STDIN>;
        $var2 = $var1 * 100;
        print $var2;
    
        print $_;
    }

So the while loop picks up your input into $_ leaving nothing for you to
read into $var1.  Since $var1 is empty, $var2 becomes 0.  So you
effectively wind up with this:

    print 0;
    print 4.56;

which outputs:

    04.56

Remove the '-p' and your program will work as expected.

Or use the '-p' and do it like this:

    echo 4.56 | perl -p -e '$_ *= 100';

-- 
Bowie
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to