Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

RE: RE: mybooks.pl (or or )

by BlueLines (Hermit)
on Aug 09, 2000 at 06:25 UTC ( #26932=note: print w/replies, xml ) Need Help??


in reply to RE: mybooks.pl (or or | | )
in thread mybooks.pl

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.

Replies are listed 'Best First'.
RE: RE: RE: mybooks.pl (or or )
by tye (Sage) on Aug 09, 2000 at 08:29 UTC

    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")
RE: RE: RE: mybooks.pl (or or )
by BlueLines (Hermit) on Aug 11, 2000 at 04:06 UTC
    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://26932]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2022-05-24 22:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (84 votes). Check out past polls.

    Notices?