Arijit Das wrote:
I am just wondering why is this giving a strange result. Any clues...?

This is from the Perl In A Nutshell book:
------
-p: Causes Perl to assume the following loop around your script, which makes it iterate over filename arguments:

    LINE:
    while (<>) {
        ...     # your script goes here
    } continue {
        print;
    }

The lines are printed automatically. To suppress printing, use the -n switch. If both are specified, the -p switch overrides -n. BEGIN and END blocks may be used to capture control before or after the implicit loop.
------

So your one-liner is actually:

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

It's already looping through the arguments with while(<>), so reading <STDIN> will yield nothing. Also, -p implies printing each argument read, so that's the output that you are getting, not $var2 as you may think.

You have 2 choices, either remove -p to read STDIN as you are doing, or change -p to -n to supress automatic printing of the input strings and remove the assignment of STDIN and read $_, which would be the next argument read from the command line:

echo 4.56 | perl -e 'my $var1=<STDIN>; $var2=$var1*100;print $var2;'

or

echo 4.56 | perl -n -e '$var2=$_*100;print $var2;'

If you keep -p, it'll print the input string right after printing $var2, like this:

4564.56

Which you do not want.

        dZ.
_______________________________________________
Perl-Unix-Users mailing list
Perl-Unix-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to