Hello everyone

I was writing a function to extract enum values from a mysql table, when I encountered something which confused me. My first attempt was

sub get_enums { my ($field, $table) = @_; my $dbh = get_ka_dbh(); my $sql = qq/SHOW COLUMNS FROM $table LIKE "$field"/; my $sth = $dbh->prepare($sql) or die $!; $sth->execute(); my $ref = $sth->fetchrow_arrayref(); my ($f) = $ref -> [1] =~ /enum\('(.*)'\)/ ; return split "','", $f; }

This works fine, but I felt there should be no need for the temporary $f variable

My first attempts at removing it brought failure, as the $ref -> [1] =~ /enum\('(.*)'\)/ was returning in the wrong context (I think), and returning "1".

After some experimentation I discovered that what I wanted could be achieved by  return split  "','",   ( $ref->[1] =~ /enum\('(.*)'\)/ )[0];

I have decided that my original version, with temp variable, is clearest, and will be what i use, but I wonder if anyone could kindly explain what is happening here, and why I need the [0] at the end

Thank You

thinker


In reply to Confusion with context by thinker

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.