in reply to Writing to text file

The “immediate-IF” syntax can come in handy here:   ( expression ? value_if_true : value_if_false )

Another handy way to do it would be to define a little sub such as:

sub quoted { my $value = shift; if (defined($value)) { return "\"$value\""; } else { return ""; } }

Now you can print calls to this function.   It will return a quoted string if there’s anything inside, or an empty-string if not.   This kind of “shorthand” if applicable, can make code much easier to read.

Replies are listed 'Best First'.
Re^2: Writing to text file
by Your Mother (Archbishop) on Feb 09, 2018 at 06:58 UTC

    Code that compiles? When did this era end? For the benefit of posterity I must point out it's naïve and broken; handling quoting properly is not really a trivial problem–

    print quoted('"quoted"'), $/;

    And immediate if is a very un-Perly name for the ternary operator.