Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I'm currently working on a script to performs auto-categorisation of data elements based on rules generated from their characteristics.

These categorisation rules change periodically, and hence I've created a rules file to perform the categorisation and return a suitable category and reason to the calling sub.

My problem: I'm struggling to have eval return anything. According to the docs:

In both forms, the value returned is the value of the last expression evaluated inside the mini-program; a return statement may be also used, just as with subroutines.

.. but that doesn't seem to work for me.

A typical rule might look like the following, where $abs_diff and $source are defined within the sub performing the eval:

return( "A", "Cat A" ) if $abs_diff < 75_000 and $source eq "ICE";

I've tried the following:

# process analysis rules foreach( @$analysis_rules ) { eval $_; }
# process analysis rules foreach( @$analysis_rules ) { my( $cat, $comment ) = eval $_; return( $cat, $comment ) if $cat and $comment; }
among a bunch of variations without luck.

Can anyone see where I'm going wrong?

Thanks in advance ...

Update: Fixed a typo in the rule - thanks to holli for pointing that out :)

In reply to Returning data from an eval by Tanalis

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (3)
    As of 2020-12-01 05:59 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found