...
(my $link1 = $linkheader) =~ s/ .*? (self)/$1/gx;
(my $link2 = $link1) =~ s/self/$1/g;
...
Repeatedly using $1 on the RHS of a substitution without any capturing parens on the LHS looks extremely suspicious ...

Further to Fletch's post:   Suspicious indeed. That code is almost certainly not doing what you expect.

Win8 Strawberry 5.8.9.5 (32) Fri 11/06/2020 18:48:09 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings use Data::Dump qw(pp); my $linkheader = 'axbyczSelfaxbycz'; printf "A: '$linkheader' (\$1 is %s) \n", pp $1; (my $link1 = $linkheader) =~ s/ .*? (Self)/$1/gx; printf "B: '$link1' (\$1 is %s) \n", pp $1; (my $link2 = $link1) =~ s/a/$1/g; printf "C: '$link2' (\$1 is %s) \n", pp $1; ^Z A: 'axbyczSelfaxbycz' ($1 is undef) B: 'Selfaxbycz' ($1 is "Self") Use of uninitialized value in substitution iterator at - line 9. C: 'Selfxbycz' ($1 is undef)
After the first substitution s/ .*? (Self)/$1/gx (point B in the demo code), $1 has the value of 'Self' as expected.

After the second substitution s/a/$1/g (point C), $1 is no longer defined because a successful match was made, but nothing was captured; there's no capture group. There are no captures in any of the following substitutions in the OPed code, so $1 never again has a defined value. (BTW: I'm surprised you didn't get a ton of "... uninitialized value ..." warnings. You do have warnings enabled, don't you? Along with strict?)

Update: If you need to use the value of a capture variable in a subsequent regex, it's usually best to capture the value of the capture variable in an ordinary variable (with a nice, self-documenting name) immediately after the regex in which the capture variable is populated. E.g.:

$string =~ m{ ... (pattern) ... }xms; my $you_can_trust_this = $1; ... ... m{ ... }xms; ... ... s{ ... (foo) ... }{$1...$you_can_trust_this}xms;
You can even do this without any direct reference to a particular capture variable:
my ($you_can_trust_this) = $string =~ m{ ... (pattern) ... }xms;
Note the parentheses around the lexical to impose list context on the match.

As Fletch mentioned, it looks like you might be trying to parse or transform some kind of ML with regexes. Don't. If this is your task, describe the task and ask for recommendations for appropriate modules; there are many well-tested and useful modules available.

As a general note, if you're trying to parse or transform non-ML text with regexes, this can be very successfully done for one-to-one | one-to-one replacement applications, even for quite large blocks of text. See haukex's article Building Regex Alternations Dynamically. As an example:

Win8 Strawberry 5.8.9.5 (32) Fri 11/06/2020 19:28:21 C:\@Work\Perl\monks >perl use strict; use warnings; my $string = 'foo + bar == baz'; my %xlate = ( 'foo' => 'wibble', 'bar' => 'wobble', 'baz' => 'boff', '+' => 'plus', '==' => 'equals', ); my ($rx_token) = map qr{ $_ }xms, join ' | ', map quotemeta, reverse sort keys %xlate ; print "\$rx_token $rx_token \n"; # for debug print "before: '$string' \n"; $string =~ s{ ($rx_token) }{$xlate{$1}}xmsg; print "after: '$string' \n"; ^Z $rx_token (?msx-i: foo | baz | bar | \=\= | \+ ) before: 'foo + bar == baz' after: 'wibble plus wobble equals boff'
The %xlate translation table, a hash, is easily maintained and automatically produces the corresponding regex.


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


In reply to Re: Not an ARRAY reference (updated) by AnomalousMonk
in thread Not an ARRAY reference by chandantul

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.