Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

RE: mybooks.pl (or or | | )

by ybiC (Prior)
on Aug 09, 2000 at 06:15 UTC ( [id://26930]=note: print w/replies, xml ) Need Help??


in reply to mybooks.pl

I have an honest question reqarding zdog's <code>code</code>.

Exactly 31 days ago, this thread led me to the conclusion that or is generally better than || when used with die.   Does this conclusion apply here as well?
    cheers,
    ybiC

Update: escaping all of <code>code</code> was meant as a wry joke on zdog, who is known to have keen sense of humor.
ybiC scans horizon for personal drummer...

Replies are listed 'Best First'.
RE: RE: mybooks.pl (or or )
by BlueLines (Hermit) on Aug 09, 2000 at 06:25 UTC
    Well, yes and no. If you use open as a list operator like this:
    open FOO || die "Argh: $!\n";

    then you will not get what you want. However, this:
    open (FOO) || die "Argh: $!\n";

    will do what you want it to (which is what zdog used).

    So in general, or is a safer bet, but || is not necessarily wrong. And perl will let you know that there is a precedence problem if you use -w.

    BlueLines

    Disclaimer: This post may contain inaccurate information, be habit forming, cause atomic warfare between peaceful countries, speed up male pattern baldness, interfere with your cable reception, exile you from certain third world countries, ruin your marriage, and generally spoil your day. No batteries included, no strings attached, your mileage may vary.

      And in the case we are discussing here, || is very much wrong. Consider the first example of || die in the posted code:

      mkdir "$dir", 0777 || die "Could not mkdir \"$dir\": $!\n";

      Here is a demonstration of why this is wrong:

      my $dir= "source"; # A directory that I know exists. warn "Using or...\n"; mkdir "$dir", 0777 or die "Could not mkdir \"$dir\": $!\n"; warn "Using ||...\n"; mkdir "$dir", 0777 || die "Could not mkdir \"$dir\": $!\n"; warn "Done.\n";

      This produces the following output:

      E:\etm\Work>perl -w mkdir.pl Using or... Could not mkdir "source": File exists Using ||... Done.

      Note that -w was silent as well, so I'm not sure when it will warn me of a precedence problem (I'm using Perl 5.6.0).

              - tye (but my friends call me "Tye")
      i'm using 5.005_03, and this is what happens when i try to use || in a naughty way:
      #!/usr/bin/perl $FOO="/etc/passwd"; open FOO || die "Argh: $!\n";

      results in:
      nooky:~$ ./foo.pl Precedence problem: open FOO should be open(FOO) at ./foo.pl line 3.
      Running with -w like a good boy produces even more useful output:
      nooky:~$ ./foo.pl Precedence problem: open FOO should be open(FOO) at ./foo.pl line 3. Probable precedence problem on logical or at ./foo.pl line 3.

      I have not played with 5.6 yet, but I'd be a little upset if this feature was removed....

      BlueLines

      Disclaimer: This post may contain inaccurate information, be habit forming, cause atomic warfare between peaceful countries, speed up male pattern baldness, interfere with your cable reception, exile you from certain third world countries, ruin your marriage, and generally spoil your day. No batteries included, no strings attached, your mileage may vary.

        Ah, that only works for open. Thanks for clearing that up.

                - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://26930]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-03-28 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found