Hello wise monks! I come to ask for your kind advice and enlightenment on the following matter:

I've been trying to print a character a certain number of times, if a certain condition is met. For the printing, I use an “until” loop. For the condition, I would like to use the conditional (ternary) operator. However, this apparently results in a syntax error. Below is example code which produces the error. My intention was to print the equals sign ten times instead.

#!/usr/bin/perl use strict; use warnings; my $count = 0; ( defined $count ) ? ( print "=" until ( $count ++ == 10 ) ) : ( print "Undefined count.\n" );

This results in the error: syntax error at ./example.pl line 8, near ""=" until". I have tried using a different form for the loop:

( defined $count ) ? ( until ( $count ++ == 10 ) { print "=" } ) : ( print "Undefined count.\n" );

However, that results in a similar error: syntax error at ./example.pl line 8, near "( until". When I try to achieve this using “if” and “else”, it works very well, using this code:

if ( defined $count ) { print "=" until ( $count ++ == 10 ); } else { print "Undefined count.\n"; }
Is it possible to use such constructions within the conditional (ternary) operator, as I've been trying to do? And if yes, how may I do so correctly? Thank you in advance!

In reply to Compound statements within the conditional (ternary) operator by Fendo

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.