And how is '' => '}' replacing the line ending? I know \z matches the end, but those empty 'quotes' puzzle me.

That's easy enough to explain. In essence, it's a bit of regex affectation. The idea was to stick a }-curly on the end of a string. That can be done well enough with a
    $string .= '}';
statement, but I thought it would be neat to it with the same global substitution that was handling all the other transformations. The empty pattern matches the empty string, and the empty string is present n+1 times in a string of n length.

c:\@Work\Perl\monks\tel2>perl -wMstrict -le "my $s = '123'; $s =~ s{ }{X}xmsg; print qq{A: '$s'}; ;; $s = ''; $s =~ s{ }{X}xmsg; print qq{B: '$s'}; ;; $s = '123'; $s =~ s{ \z }{X}xmsg; print qq{C: '$s'}; " A: 'X1X2X3X' B: 'X' C: '123X'
And there's always an empty string at the  \z (absolute end) of any string, even the empty string! (And the empty string is a perfectly good hash key.)

As you have seen, this approach breaks down when you try to replace an empty string at different locations with different replacement strings: there can only be one ''-keyed value in a hash. To do what you want with a regex, you'd have to resort to some screaming hack like (untested)

my %globize = ('P' => '{P', ':' => '}:{', '' => '}'); ... $globule =~ s{ (\A P | : | \z) }{$globize{$1}}xmsg;
(in the unlikely case your input string always starts with a fixed sequence like 'P'), but if you just want to add different stuff at the start and end of a string, it would be better IMHO to just do something like
    $globule = "{$globule}";
instead (update: and forget about all the  \z alternation and  '' => '}' stuff).


Give a man a fish:  <%-{-{-{-<


In reply to Re^4: Combinations of lists, etc by AnomalousMonk
in thread Combinations of lists to a hash by tel2

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.