Re: Help with an operator precedence (?) puzzle

Discussion in 'Perl Misc' started by Rainer Weikusat, May 15, 2014.

  1. Henry Law <> writes:
    > Perl 5.14 running on Ubuntu. I'm fairly sure that the answer to this
    > is operator precedence but I've looked at it and consulted perldoc
    > until my brain hurts and I can't work it out. Could someone explain?


    [...]

    > #!/usr/bin/perl
    > use strict;
    > use warnings;
    > use 5.010;
    >
    > sub tryit {
    > my ( $user, $conf, $parms ) = @_;
    > $@ = "reset_user: invalid parameters\n" and return if $parms==1;
    > $@ = "reset_user: '$user' doesn't exist\n" and return if $parms==2;
    > }
    >
    > If I compile it Perl warns me: "Found = in conditional, should be ==
    > at tryout.pl line 8."


    $parms == 1 and $@ = "reset toastbrot" and return;

    The second and-junctor is technically nonsense because the value
    of its left-hand operand will always be true. The perl compiler was so
    kind to assume that you meant to write something sensible and
    accidentally forgot to put in the == which would have turned this into a
    (still rather bizarre) test.

    The easy way to avoid being told than one isn't making sense by a
    computer is to stop confusing it (and the hypothetical maintencance
    programmer would be very grateful for that), ie, use

    sub tryit {
    my ( $user, $conf, $parms ) = @_;
    $@ = "reset_user: invalid parameters\n", return if $parms==1;
    $@ = "reset_user: '$user' doesn't exist\n", return if $parms==2;
    }

    instead.

    NB: 'Mr Law' is unlikely to see this but I hope it may be useful for
    someone else. Usually, I wouldn't write it in this style ...
    Rainer Weikusat, May 15, 2014
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Frauzel

    Operator precedence

    David Frauzel, May 13, 2004, in forum: Perl
    Replies:
    2
    Views:
    537
    Luc Van Hove
    May 17, 2004
  2. kbd

    Operator Precedence

    kbd, Jul 22, 2004, in forum: Java
    Replies:
    10
    Views:
    742
    Roedy Green
    Jul 27, 2004
  3. Master of C++
    Replies:
    4
    Views:
    712
    Karl Heinz Buchegger
    Feb 7, 2005
  4. Richard Bos

    && and || Operator precedence enforcement

    Richard Bos, Jan 19, 2004, in forum: C Programming
    Replies:
    18
    Views:
    604
    Dan Pop
    Jan 20, 2004
  5. Tim McDaniel
    Replies:
    62
    Views:
    217
    Eric Pozharski
    Jun 3, 2014
Loading...

Share This Page