Perl doesn't have a "true-once" variable, however there are many ways to get the behaviour you want --- and several have been mentioned, including the most pertinent one: namely that an algorithm with join is probably your best option.

That said, there are a few ways to get the 'once-only' behaviour you are looking in your case without creating new synthetic variables. First you can use the flip-flop operator:

#!/usr/bin/perl -w use strict; while(<DATA>){ chomp; print ",\n" unless 1..1; print; } __DATA__ foo bar blah

In scalar context the range .. op is the flip-flop, and when using constant expressions on either side, they are automatically compared to the builtin $. variable. So the above flip-flop is only true for the first record read. This of course leads to the more explicit and only slightly longer direct comparison with $. itself:

#!/usr/bin/perl -w use strict; while(<DATA>){ chomp; print ",\n" unless $. == 1; print; } __DATA__ foo bar blah

But the idea of "once-only" can also be expressed by using the special ?? delimiters for a regex: with such delimiters, the regex will match only once --- so, using an expression that will succeed will also get you what you want:

#!/usr/bin/perl -w use strict; while(<DATA>){ chomp; print ",\n" unless ?^?; print; } __DATA__ foo bar blah

In reply to Re: A TRUE-once variable by danger
in thread A TRUE-once variable by gmax

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.