thinker has asked for the wisdom of the Perl Monks concerning the following question:
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Confusion with context
by Roy Johnson (Monsignor) on Mar 11, 2005 at 14:30 UTC | |
Re: Confusion with context
by davis (Vicar) on Mar 11, 2005 at 10:50 UTC |