Strange behaviour in Perl

Strange behaviour in Perl

am 25.01.2006 12:47:17 von Arijit Das

--===============1556606198==
Content-Type: multipart/alternative; boundary="0-2084816457-1138189637=:39791"
Content-Transfer-Encoding: 8bit

--0-2084816457-1138189637=:39791
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

I am just wondering why is this giving a strange result. Any clues...?

bash-2.01$ echo 4.56 | perl -p -e 'my $var1 = ; $var2 = $var1 * 100; print $var2;'
04.56
bash-2.01$

I am expecting 456 in the ouput instead of 4.56

Am I missing anything...?

Thanks,
Arijit



---------------------------------
Do you Yahoo!?
With a free 1 GB, there's more in store with Yahoo! Mail.
--0-2084816457-1138189637=:39791
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

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



Do you Yahoo!?

With a free 1 GB, there's more in store with
--0-2084816457-1138189637=:39791--

--===============1556606198==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1556606198==--

RE: Strange behaviour in Perl

am 25.01.2006 14:04:22 von Joseph.Bautista

This is a multi-part message in MIME format.

--===============0624914272==
Content-Type: multipart/alternative;
boundary="----=_NextPart_000_0091_01C621C9.02F34BC0"

This is a multi-part message in MIME format.

------=_NextPart_000_0091_01C621C9.02F34BC0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

I think the -p is the one causing the problem



Tried



echo 4.56 | perl -e 'my $var1 = * 100; print $var1;'



and the result is 456







bash-2.01$ echo 4.56 | perl -p -e 'my $var1 = ; $var2 = $var1 * 100;
print $var2;'
04.56





I only remove the -p option


--
This message has been scanned for viruses and dangerous content by host-center.net and is believed to be clean.


------=_NextPart_000_0091_01C621C9.02F34BC0
Content-Type: text/html;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

osoft-com:office:office" xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns=3D"http://www.w3.org/TR/REC-html40">=0D

=0D
>=0D
=0D
=0D

=0D

=0D

=0D

=
style=3D'font-size:12.0pt;color:black'>I think the –p is the one caus=
ing=0D
the problem

=0D

=
style=3D'font-size:12.0pt;color:black'> 

=
=0D

=
style=3D'font-size:12.0pt;color:black'>Tried

=
=0D

=
style=3D'font-size:12.0pt;color:black'> 

=
=0D

=
style=3D'font-size:12.0pt;color:black'>echo 4.56 | perl -e 'my $var1 =
<STDIN> * 100; print $var1;'

=0D

=
style=3D'font-size:12.0pt;color:black'> 

=
=0D

=
style=3D'font-size:12.0pt;color:black'>and the result is 456 an>

=0D

=
style=3D'font-size:12.0pt;color:black'> 

=
=0D

=
style=3D'font-size:12.0pt;color:black'> 

=
=0D

'font-size:=0D
12.0pt'>  span>

=0D

4.0pt'>=0D

=0D

'font-size:=0D
12.0pt'>bash-2.01$ echo 4.56 | perl -p -e 'my $var1 =3D <STDIN>; $var=
2 =
$var1 * 100;  print $var2;'
=0D
04.56 nt>

=0D

=
style=3D'font-size:12.0pt;color:black'> 

=
=0D

=
style=3D'font-size:12.0pt;color:black'> 

=
=0D

=
style=3D'font-size:12.0pt;color:black'>I only remove the –p option p>

=0D

=0D

=0D

=0D

=0D


--=20

This message has been scanned for viruses and dangerous content by ho=
st-center.net and is believed to be clean.
=0D

------=_NextPart_000_0091_01C621C9.02F34BC0--



--===============0624914272==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0624914272==--

RE: Strange behaviour in Perl

am 25.01.2006 14:20:40 von Chris Newman

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

--===============1502795948==
Content-Type: multipart/alternative;
boundary="----_=_NextPart_001_01C621B2.2354419C"

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C621B2.2354419C
Content-Type: text/plain

The -p switch is causing Perl to iterate over the inputs as if they were
filename arguments.



Try this instead...



echo 4.56 | perl -e '$var1 =; $var2 = $var1 * 100; print "$var2\n"'



cn



_____

From: activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of Arijit Das
Sent: Wednesday, January 25, 2006 5:47 AM
To: activeperl@listserv.activestate.com;
perl-unix-users@listserv.activestate.com
Subject: Strange behaviour in Perl



I am just wondering why is this giving a strange result. Any clues...?



bash-2.01$ echo 4.56 | perl -p -e 'my $var1 = ; $var2 = $var1 * 100;
print $var2;'
04.56
bash-2.01$



I am expecting 456 in the ouput instead of 4.56



Am I missing anything...?



Thanks,
Arijit



_____

Do you Yahoo!?
With a free 1 GB, there's more in store with Yahoo!

Mail.


------_=_NextPart_001_01C621B2.2354419C
Content-Type: text/html
Content-Transfer-Encoding: quoted-printable

xmlns:o=3D"urn:schemas-microsoft-com:office:office" =
xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns=3D"http://www.w3.org/TR/REC-html40">


charset=3DUS-ASCII">












style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>The –p switch is causing =
Perl to
iterate over the inputs as if they were filename =
arguments.



style=3D'font-size:
10.0pt;font-family:Arial;color:navy'> 

=


style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>Try this =
instead…



style=3D'font-size:
10.0pt;font-family:Arial;color:navy'> 

=


style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>echo 4.56 | perl -e '$var1 =
=3D<STDIN>;
$var2 =3D $var1 * 100; print =
"$var2\n"'



style=3D'font-size:
10.0pt;font-family:Arial;color:navy'> 

=


style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>cn



style=3D'font-size:
10.0pt;font-family:Arial;color:navy'> 

=




size=3D3
face=3D"Times New Roman">






style=3D'font-size:10.0pt;
font-family:Tahoma;font-weight:bold'>From:
size=3D2
face=3DTahoma>
activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] style=3D'font-weight:
bold'>On Behalf Of
Arijit Das

Sent: Wednesday, January =
25, 2006
5:47 AM

To:
activeperl@listserv.activestate.com; =
perl-unix-users@listserv.activestate.com

Subject: Strange =
behaviour in Perl





style=3D'font-size:
12.0pt'> 





style=3D'font-size:
12.0pt'>I am just wondering why is this giving a strange result. Any =
clues...?







style=3D'font-size:
12.0pt'> 







style=3D'font-size:
12.0pt'>bash-2.01$ echo 4.56 | perl -p -e 'my $var1 =3D <STDIN>; =
$var2 =3D
$var1 * 100;  print $var2;'

04.56

bash-2.01$







style=3D'font-size:
12.0pt'> 







style=3D'font-size:
12.0pt'>I am expecting 456 in the ouput instead of =
4.56







style=3D'font-size:
12.0pt'> 







style=3D'font-size:
12.0pt'>Am I missing anything...?







style=3D'font-size:
12.0pt'> 







style=3D'font-size:
12.0pt'>Thanks,

Arijit







style=3D'font-size:
12.0pt'> 





size=3D3
face=3D"Times New Roman">






style=3D'font-size:
12.0pt'>Do you Yahoo!?

With a free 1 GB, there's more in store with href=3D"http://us.rd.yahoo.com/mail_us/taglines/mailstorage/ *http:/mail.=
yahoo.com/">Yahoo!
Mail.









------_=_NextPart_001_01C621B2.2354419C--

--===============1502795948==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1502795948==--

Re: Strange behaviour in Perl

am 25.01.2006 14:33:34 von DZ-Jay

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

Remove -p.

dZ.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Strange behaviour in Perl

am 25.01.2006 14:45:52 von intertwingled

Arijit Das wrote:

>I am just wondering why is this giving a strange result. Any clues...?
>
> bash-2.01$ echo 4.56 | perl -p -e 'my $var1 = ; $var2 = $var1 * 100; print $var2;'
>04.56
>bash-2.01$
>
> I am expecting 456 in the ouput instead of 4.56
>
> Am I missing anything...?
>
> Thanks,
>Arijit
>
>
>
>---------------------------------
>Do you Yahoo!?
> With a free 1 GB, there's more in store with Yahoo! Mail.
>
>
>----------------------------------------------------------- -------------
>
>_______________________________________________
>ActivePerl mailing list
>ActivePerl@listserv.ActiveState.com
>To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
Works fine without the -p.

Tony

--

I always have coffee when I watch radar!

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: [Perl-unix-users] Strange behaviour in Perl

am 25.01.2006 15:15:00 von DZ-Jay

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 = ;
$var2 = $var1 * 100;
print $var2;
} continue {
print;
}

It's already looping through the arguments with while(<>), so reading
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=; $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.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Strange behaviour in Perl

am 25.01.2006 15:25:40 von Brian Raven

From: activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of Arijit
Das
Sent: 25 January 2006 11:47
To: activeperl@listserv.activestate.com;
perl-unix-users@listserv.activestate.com
Subject: Strange behaviour in Perl


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

Possibly. Either remove the -p switch, or change your command to echo
4.56 | perl -p -e '$_ *= 100'.

Have you checked 'perldoc perlrun' for what -p does? That may give you a
clue as to what is happening.

HTH

--
Brian Raven



=================================
Atos Euronext Market Solutions Disclaimer
=================================
The information contained in this e-mail is confidential and solely for the intended addressee(s). Unauthorised reproduction, disclosure, modification, and/or distribution of this email may be unlawful.
If you have received this email in error, please notify the sender immediately and delete it from your system. The views expressed in this message do not necessarily reflect those of Atos Euronext Market Solutions.

L'information contenue dans cet e-mail est confidentielle et uniquement destinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. Toute copie, publication ou diffusion de cet email est interdite. Si cet e-mail vous parvient par erreur, nous vous prions de bien vouloir prevenir l'expediteur immediatement et d'effacer le e-mail et annexes jointes de votre systeme. Le contenu de ce message electronique ne represente pas necessairement la position ou le point de vue d'Atos Euronext Market Solutions.


_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Strange behaviour in Perl

am 25.01.2006 15:25:49 von Bowie Bailey

Arijit Das wrote:
> I am just wondering why is this giving a strange result. Any clues...?
>
> $ echo 4.56 | perl -p -e 'my $var1 = ; $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 = ;
$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
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: [Perl-unix-users] Strange behaviour in Perl

am 27.01.2006 10:02:53 von Arijit Das

--===============0873677828==
Content-Type: multipart/alternative; boundary="0-1263866535-1138352573=:22696"
Content-Transfer-Encoding: 8bit

--0-1263866535-1138352573=:22696
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

Thanks a lot folks.

My mistake...I should have looked at "-p"'s significance before posting it.

Thanks again,
Arijit

DZ-Jay wrote:
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 = ;
$var2 = $var1 * 100;
print $var2;
} continue {
print;
}

It's already looping through the arguments with while(<>), so reading
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=; $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.




---------------------------------

What are the most popular cars? Find out at Yahoo! Autos
--0-1263866535-1138352573=:22696
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

Thanks a lot folks.
 
My mistake...I should have looked at "-p"'s significance before posting it.
 
Thanks again,
Arijit

DZ-Jay <dz@caribe.net> wrote:
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.
------
R>So your
one-liner is actually:

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

It's already looping through the arguments with while(<>), so reading
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=; $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.





What are the most popular cars? Find out at Yahoo! Autos
--0-1263866535-1138352573=:22696--

--===============0873677828==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0873677828==--