Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Using scalar values as conditionals

by amarceluk (Beadle)
on May 28, 2002 at 16:29 UTC ( #169819=perlquestion: print w/ replies, xml ) Need Help??
amarceluk has asked for the wisdom of the Perl Monks concerning the following question:

This isn't a question about how something works, but how/why it's used. In "Learning Perl" (my new favorite book), this appears in the section about the "if" control structure:
You may actually use any scalar value as the conditional of the if control structure. That's handy if you want to store a true or false value into a variable, like this:
$is_bigger=$name gt 'fred'; if ($is_bigger) {...}
I understand how that works, but I'm curious as to how people use it, and why it would be used instead of
if ($name gt 'fred') {...}
I want to train myself to use good coding practices, but also to understand them, so I'd appreciate any thoughts on using this particular one.

__________
"Abby-somebody. Abby-normal."
Young Frankenstein

Comment on Using scalar values as conditionals
Select or Download Code
Re: Using scalar values as conditionals
by Abigail-II (Bishop) on May 28, 2002 at 16:50 UTC
    You are focussing to much on the details of the example. Don't forget this example comes from a book. Books are small. Code fragments in books need to be even smaller.

    In a real program, the $name gt 'fred' could be a long, expensive expression, whose value is needed more than once. Or the comparison between $name and 'fred' and if statement has some code between it, code that may, or may not modify $name.

    Abigail

      Yes, very good points. In particular, I hadn't thought about there being code that might modify $name. Thank you!

      I guess I do focus too much on the details because I'm still at an early stage of learning and I try very hard to understand exactly how everything works, but don't have enough knowledge/imagination to see how the example being demonstrated might be used in real code. Hopefully as I write more real code it'll be easier to understand these things!

      __________
      "Abby-somebody. Abby-normal."
      Young Frankenstein
Re: Using scalar values as conditionals
by samtregar (Abbot) on May 28, 2002 at 18:43 UTC
    It's unusual to save the value of a comparison, but it is very common to use a variable in an if statement. Consider this code:

    # get office_id for this office my $office_id = get_office_id(); if ($office_id) { # found an office } else { # no office found }

    The hypothetical get_office_id() function could be written in such a way that the office id returned will either be a positive integer or undef if the office id cannot be found. Then the $office_id can be used directly in an if to test to result.

    Of course, this will be wrong if 0 is a vaild office id. Then you need to do:

    # get office_id for this office my $office_id = get_office_id(); if (defined $office_id) { # found an office } else { # no office found }

    Although this is a common pattern it is actually not the best way to write functions in Perl. A better way is to separate the status code from the return value:

    # get office_id for this office my ($ok, $office_id) = get_office_id(); if ($ok) { # found an office } else { # no office found }

    That way there can be no confusion between the office id itself and the success of the call.

    -sam

Re: Using scalar values as conditionals
by Biker (Priest) on May 29, 2002 at 07:39 UTC

    "...and why it would be used..."

    Consider the situation where it's expensive to get to the value of $is_bigger. Like for instance getting the information from a database, from an external file or prompting the user. If you then need to test it a number of times, it makes sense to 'remember' the value of $is_bigger.


    Everything went worng, just as foreseen.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://169819]
Approved by boo_radley
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-09-21 10:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls