print 
Author Message
 print

===================================
$x=2;
open(FD,">/etc/security/passwd");
$x=print FD "kiran\n"|| die "hello\n";
if ($x eq undef )
{
 print "YES1\n";
Quote:
}

if($x == 0)
{
 print "YES\n";
Quote:
}

print "$x\n";
===================================

In the above code if open failes then FD will not be a valid one, so
print will fail to write, since print failed it will return 0. since
it is in OR with die it has to execute die "hello\n", but it is
printing YES1 and YES. If i change print as $x=print(FD
"kiran\n")||die "hello\n"; Now it is going to die. Why this diffrence
in behaviour? I am using perl-6.1 on AIX.

Please help,
kiran



Tue, 31 Aug 2004 15:08:21 GMT  
 print


Quote:
> ===================================
> $x=2;
> open(FD,">/etc/security/passwd");
> $x=print FD "kiran\n"|| die "hello\n";
> if ($x eq undef )
> {
>  print "YES1\n";
> }
> if($x == 0)
> {
>  print "YES\n";
> }
> print "$x\n";
> ===================================

> In the above code if open failes then FD will not be a valid one, so
> print will fail to write, since print failed it will return 0. since
> it is in OR with die it has to execute die "hello\n", but it is
> printing YES1 and YES. If i change print as $x=print(FD
> "kiran\n")||die "hello\n"; Now it is going to die. Why this diffrence
> in behaviour? I am using perl-6.1 on AIX.

> Please help,
> kiran

Okay ... this is merely conjecture .... I cant say this is definately the answer
;)

Without the parens, perl is checking whether the scalar "kiran\n" is true, not
the statement.
It is, so it doesn't die.
With the parens, it is checking that the exectution of print( FD "kiran\n") is
true ( you could achieve the same with ( print FD "kieran\n" ) || die;

A better way to write this (IMHO) would be.

$x=2;
open(FD,">/etc/security/passwd") or die "hello\n";
$x=print FD "kiran\n";

Since you are then checking the open worked before trying to print to it.
You could then ditch the undef check.

If you still want it ...

(($x eq undef) || ($x == 0) ) && { print "YES1\n";}
print "$x\n";

But, there's allways more than one way to do it ;)

HTH

CJ

----------------------------------------------------------------------------
Year 2000 never bothered me.
It's year 65536 that I'm worried about
----------------------------------------------------------------------------
H4x0R : I'm way cooler than you! I got 40 scrypts that can kill yer machine
sysop : Heh! Yeah right!
w33n3r: Yeah. I can nail you from here man ... gimme your ip and you're toast!
l4m3rz: Yeah .. we rock .. we're gonna fry your machine
sysop : Ok, I dare ya ... My ip is 127.0.0.1
H4x0R : ##Disconnected##
w33n3r: ##Disconnected##
l4m3rz: ##Disconnected##



Tue, 31 Aug 2004 22:11:10 GMT  
 print

Quote:
> ===================================
> $x=2;
> open(FD,">/etc/security/passwd");
> $x=print FD "kiran\n"|| die "hello\n";
> if ($x eq undef )
> {
>  print "YES1\n";
> }
> if($x == 0)
> {
>  print "YES\n";
> }
> print "$x\n";
> ===================================

> In the above code if open failes then FD will not be a valid one, so
> print will fail to write, since print failed it will return 0. since
> it is in OR with die it has to execute die "hello\n", but it is
> printing YES1 and YES. If i change print as $x=print(FD
> "kiran\n")||die "hello\n"; Now it is going to die. Why this diffrence
> in behaviour? I am using perl-6.1 on AIX.

Very simple. Precedence! If you omit the paranthesis as in
    $x=print FD "kiran\n"|| die "hello\n";
then the expression will be evaluated as
    $x=print FD ("kiran\n"|| die "hello\n");
because of the higher precedence of the "||" operator.

Obviously this is something totally different from
    $x=print (FD "kiran\n") || die "hello\n";

In situations like this you should use the low-precedence "or" instead of
"||".

And in any case, what you really want to do is checking the success of the
"open" itself instead of the success of the "print":
    open(FD,">/etc/security/passwd") or die "Can't open /etc/security/passwd
because $!\n";

jue



Wed, 01 Sep 2004 01:27:55 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. CGI module prints stuff I didn't print

2. Print before sleep prints after sleep

3. Print,send the printed, then do the action (CGI)

4. Print outputs despite no value to print out??

5. print in different codepage = overload print?

6. Printing output to a specific print driver

7. print $fh verses $fh->print

8. printing quotes with print command

9. status print not printing until output

10. FORMAT: printing non-printing characters

11. ?? How to print to 2 files with one print command

12. print Location and print "Location

 

 
Powered by phpBB® Forum Software