Here's how I would have posted the code sample for this question to make it easier to answer:

my $output = q/..../; my $matched = $output =~ m/"access_token":"(.*)", "ex/g; print "Match: $matched\n"; say '$1 is <', (defined($1) ? $1 : 'undef'), ">\n";

Except in place of .... I would paste in exactly the output from the curl call, but paired down to enough to demonstrate the portion you're trying to match without showing us 64kb of extra cruft.

When you do this, you will probably be able to answer the question yourself without even posting it, because you will see exactly why the match is failing.

But it's also probable that when you solve this problem, by retaining a regex approach you'll remain quite fragile. JSON isn't really intended to be processed as plain old text. And your regular expression is expecting the next key after "access_token" to start with "ex.

According to the JSON spec, "An object is an unordered set of name/value pairs." There's no guarantee of key order when objects are serialized and deserialized. That being the case, the match could fail while the data remains perfectly valid if you somehow got the keys back in a different order. There is no inherent order to hash keys, even in JSON. If you rely on an order, you're relying in an implementation detail that may work in practice but only works because nothing has changed to cause keys to come in some other order. Order is not guaranteed by the spec.

A far better approach would be to process the JSON as JSON using JSON to decode it. Then you could just dive into the datastructure precisely, and without any regex.


Dave


In reply to Re: why is this an uninitialized value? by davido
in thread why is this an uninitialized value? by jdorwin

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.