Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

exists function used with attributes/operators?

by bArriaM (Novice)
on Jul 19, 2015 at 01:35 UTC ( [id://1135337]=perlquestion: print w/replies, xml ) Need Help??

bArriaM has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking at a script that accepts arguments and stores them in %opts.

There is this line of code at some point:

my $C = exists $opts{'c'} ? '!' : '';

Can this be read as:

my $C = exists $opts{'c'} - if TRUE, $C gets !, if FALSE, $C gets empty string

If I'm right, can you explain a little more about those operators?

Thank you!

Replies are listed 'Best First'.
Re: exists function used with attributes/operators?
by toolic (Bishop) on Jul 19, 2015 at 02:26 UTC
    Yes, your understanding is correct. You should create some test code to prove it to yourself. Perl has excellent documentation:

      Thank you!

Re: exists function used with attributes/operators?
by AnomalousMonk (Archbishop) on Jul 19, 2015 at 04:48 UTC

    One way to gain experimental insight into the sub-expressions of a Perl statement and their interrelationship is to use the O and B::Deparse modules with full parenthesization (the  -p switch) enabled:

    c:\@Work\Perl>perl -wMstrict -MO=Deparse,-p -le "my %opts = (c => 1); my $C = exists $opts{'c'} ? '!' : ''; " BEGIN { $^W = 1; } BEGIN { $/ = "\n"; $\ = "\n"; } use strict 'refs'; (my(%opts) = ('c', 1)); (my $C = (exists($opts{'c'}) ? '!' : '')); -e syntax OK


    Give a man a fish:  <%-(-(-(-<

      Thanks

Re: exists function used with attributes/operators?
by 1nickt (Canon) on Jul 19, 2015 at 11:23 UTC

    Yes, you are right, that's Perl's "ternary operator."

    Fewer keystrokes and often more readable than an if ... then ... else block.

    One of the most useful features is that you can nest them! Here's an exaggerated example (I would use a lookup hash if there were so many choices), with the logic reversed from your OP so we can test a little more deeply:

    my $C = ! exists $opts{'c'} ? 'missing' : $opts{'c'} eq '' ? '' : $opt +s{'c'} > 100 ? 'a lot' : $opts{'c'} > 10 ? 'some' : $opts{'c'} > 1 ? +'a few' : $opts{'c'} < 1 ? 'none' : 'one';

    Usually I enclose the condition to be evaluated in parentheses (you will have to if you are testing for more than one condition), and write the statement on multiple lines for clarity:

    my $C = (! exists $opts{'c'}) ? 'missing' : ($opts{'c'} eq '') ? '' : ($opts{'c'} > 100) ? 'a lot' : ($opts{'c'} > 10) ? 'some' : ($opts{'c'} > 1) ? 'a few' : ($opts{'c'} < 1) ? 'none' : 'one';

    HTH

    Update: added error handling to code.

    The way forward always starts with a minimal test.

      Thank you!!

Re: exists function used with attributes/operators?
by Laurent_R (Canon) on Jul 19, 2015 at 10:18 UTC
    Your interpretation is correct, but to put it in actual Perl code, rather than pseudo-code, you could replace:
    my $C = exists $opts{'c'} ? '!' : '';
    with, for example, this:
    my $C; if (exists $opts{'c'}) { $C = '!'; } else { $C = ''; }
    The semantic of the so-called ?: ternary operator (see: http://perldoc.perl.org/perlop.html#Conditional-Operator is essentially the following:
    $c = condition ? value of $c if condition is true : value of $c otherw +ise;

      Thank you very much!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 14:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found