as always a couple of threads make sense when you join them together

In chat the subject of length of a defined scalar popped up, and discussions of if else conditional syntax have occured, this article merges parts of those ideas together

from the command line, this was being typed

shell$ perl -e 'my $input = <>; chomp($input);if (length($input)) { pr +int q(length) }; if (defined($input)) { print q(defined) };

with questions about why zero length scalars remain defined.

I quickly suggested that a true conditional be placed as a further argument, to suggest the reason. I went back to this after, and evolved a useful learning trick.

Briefly the code suggested in the title is obviously the famous shakespearian quote to be or not to be? Well more rightly it is the answer if Romeo makes the decision, the question itself is an OR conditionial, ok IM getting existential now...

if($decision eq "to be"){print "Romeo is"} else{print "Romeo is not"}

translates to

$dec eq "to be" ? print "Romeo is" : print "Romeo is not";

Best to start with the truth I think. In the original command line the input scalar will always be defined, as it is a scalar passed to the code via the command line. What it will or won't be is true. Lined out for readability.

shell$ perl -e 'my $input=<>; chomp $input; print q(Romeo ); $input ? print q(is ) : print q(is not ); print qq(\ntaking ),length($input),qq( hours to decide!\n);

So lets try this with an undefined scalar. No input from command line and hence no chomp also. We will aslo extended the conditional argument so that the false or 'else' side becomes a further conditional or 'elsif' side. Either way something very interesting happens.

shell$ perl -e 'my $input; # not yet defined print q(Romeo ); #test for the scalar to be not defined to be true !defined $input ? print q(undefined ) : $input ? print q(is ) : print q(is not ); print qq(\n taking ),length($input),qq( hours to decide!\n);

So the crux of the play is surely whether the love was ever defined in the first place?!

Coyote


In reply to defined ? to be : not by Don Coyote

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.