Further to Athanasius's reply: NB: It's not quite right to say that the  /g modifiers in the  m//g matches are doing nothing. In fact, they're actively screwing you over (even if you get beyond the improper scoping of the lexical in the if-block).

Because the  m//g matches are being called in scalar context in both cases in the OPed code, the  /g modifier acts to leave the match position string pointer where it is after the first (successful) match, and to start matching from that position in the second match.

The first thing you search for in the string is  'startTime' followed by some stuff. Later, you search the same string for  'endTime' and some stuff, but you'll never find it because  'endTime' appears before  'startTime' and the regex engine (under the influence of the  /g modifiers) has already passed by it in the string. This can be demonstrated by printing the pos match position of the string after the first match. (I've left out the chomp statements because I assume they really are useless.)

c:\@Work\Perl\monks>perl -wMstrict -le "my $timeLimit = 'xxx endTime=\"2016-12-28T23:59:59\" startTime=\"2016-09-30T00:00:0 +0\" yyy'; ;; $timeLimit =~ m/startTime=\"(.*?)\"/g; my $startTime = $1; print 'match position after 1st match: ', pos $timeLimit; ;; my $endTime; if ($timeLimit =~ m/endTime/) { $timeLimit =~ m/endTime=\"(.*?)\"/g; $endTime = $1; print 'match position after 2nd match: ', pos $timeLimit; } print \"[$startTime],[$endTime]\n\"; " match position after 1st match: 65 Use of uninitialized value in print at -e line 1. match position after 2nd match: Use of uninitialized value $endTime in concatenation (.) or string at +-e line 1. [2016-09-30T00:00:00],[]
Removing either — or better yet, both! — of the confounding and potentially very confusing  /g modifiers will get you what you want (if the lexical scoping problem is addressed too, of course).

Update: FWIW, my own preference in cases like this is to extract sub-strings from strings in list context and at the same time to generate an "extraction success" flag for possible later use:

c:\@Work\Perl\monks>perl -wMstrict -le "my $timeLimit = 'xxx endTime=\"2016-12-28T23:59:59\" startTime=\"2016-09-30T00:00:0 +0\" yyy'; ;; my $got_start = my ($start) = $timeLimit =~ m/startTime=\"(.*?)\"/; my $got_end = my ($end) = $timeLimit =~ m/endTime=\"(.*?)\"/; ;; print qq{start [$start], end [$end]} if $got_start and $got_end; " start [2016-09-30T00:00:00], end [2016-12-28T23:59:59]


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


In reply to Re: Pattern matching and deriving the data between the "(double quotes) in HTML tag by AnomalousMonk
in thread Pattern matching and deriving the data between the "(double quotes) in HTML tag by sp4rperl

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.