Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Code blocks with ternary operator or trailing conditionals

by Athanasius (Archbishop)
on Feb 09, 2014 at 02:49 UTC ( [id://1074086]=note: print w/replies, xml ) Need Help??


in reply to Code blocks with ternary operator or trailing conditionals

Just use do blocks:

#! perl use strict; use warnings; my $x = 42; do { print "Hello "; print "world!\n"; } if 7 < $x; 43 > $x ? do { print "Yes, "; print "it is!\n"; } : do { print "No, "; print "it isn't!\n"; };

Output:

12:46 >perl 865_SoPW.pl Hello world! Yes, it is! 12:46 >

See do.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Code blocks with ternary operator or trailing conditionals
by puterboy (Scribe) on Feb 09, 2014 at 02:55 UTC
    Thanks - I am aware of 'do' -- just was hoping there was a way to 'do' it without it...
      ... a way to 'do' it without it...

      I wouldn't say this is a better way (in fact, as kcott has pointed out below,  if { ... } else { ... } syntax is better for what you seem to want than this or a do block), but it's a way:

      c:\@Work\Perl\monks>perl -wMstrict -le "my $x = 42; ;; sub { print qq{Hello }; print qq{world!}; }->() if 7 < $x; ;; 43 > $x ? sub { print qq{Yes, }; print qq{it is!}; }->() : sub { print qq{No, }; print qq{it is not!}; }->() ; " Hello world! Yes, it is!
        You can factour out the ->() part:
        (43 > $x ? sub { print 'Yes, '; print 'it is!'; } : sub { print 'No, '; print 'it is not!'; } )->();
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        LOL - somehow I'm not sure that this will make my code read shorter and cleaner... though creative... :)

Log In?
Username:
Password:

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

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

    No recent polls found