I'm having trouble getting a script to read a custom sort statement from a text file and use it. The text file contains the sort statement:

{ ($a =~ /B(\d+)/)[0] <=> ($b =~ /B(\d+)/)[0] or ($a =~ /U(\d+)/)[0] <=> ($b =~ /U(\d+)/)[0] }

The code I have so far is below:
*gSort = ''; my $sortname = 'gSort'; chomp(my $sort = `cat globalSort.txt` // ''); if ($sort) { *gSort = sub {$sort}; } foreach my $t (sort $sort @data) { print "$t\n"; }
When I run the script, I get the error:

Argument "{ ($a =~ /B(\\d+)/)[0] <=> ($b =~ /B(\\d+)/)[0] or ($a =..." isn't numeric

I can see that it is escaping the backslash for the \d+ part of the regex. If I manually write the same sort statement in to the code, it works as expected. What am I doing wrong? I should add that this is the first time I have used a typeglob (*) as it was the suggested solution that I found when looking on Google.

Any help would be very much appreciated, thank you!


In reply to Create sort function from a text file by Doozer

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.