The "correct" answer to your question would be to use a module to run the external command, and as far as I understand ioreg has an -a switch to give its output in the more parseable XML format, and then use a module such as XML::LibXML to parse that.

But this might be one of those rare cases where a "quick and dirty" solution is enough. (Update: To clarify: using backticks is probably "ok" here if the command doesn't output anything on STDERR, you don't care about the command's exit value, and because the command below doesn't contain any shell metacharacters or interpolated variables. I explain this more in the link above.)

my ($UUID) = `ioreg -d2 -c IOPlatformExpertDevice` =~ /^.*\bIOPlatformUUID\b.*"([^"]+)"\s*$/m or die "failed to parse ioreg";

Note I can't fully test this because I don't have a Mac handy. Also, this will match the first of any lines with the string IOPlatformUUID, so you have to be sure of the command's output, and the above code is correspondingly brittle.

It is complaining with sh: -c: line 0: unexpected EOF while looking for matching `"'. Why?

The -F\" is being interpreted by Perl instead of the shell, and the shell is only seeing -F", which is an unmatched quote - this is one of the many pitfalls of backticks. Also, one shouldn't call sed or awk from Perl because native Perl code can replace these two commands and plenty others.


In reply to Re: Read UUID on macOS from script by haukex
in thread Read UUID on macOS from script by Anonymous Monk

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.