Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Confusion with context

by thinker (Parson)
on Mar 11, 2005 at 10:39 UTC ( [id://438593]=perlquestion: print w/replies, xml ) Need Help??

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
    You may or may not consider this clearer:
    my $ref = $sth->fetchrow_arrayref(); $ref->[1] =~ /enum\(/g or return (); return $ref->[1] =~ /\G'(.*?)',?/g;
    "Find the start of the enum, then extract whatever is between quotes and followed optionally by a comma."

    Caution: Contents may have been coded under pressure.
Re: Confusion with context
by davis (Vicar) on Mar 11, 2005 at 10:50 UTC
    (sidestepping your actual question...)
    what about:
    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $array_ref = [qw/one two three four/]; $array_ref->[1] =~ s/w//g; print Dumper($array_ref);
    This avoids the temporary variable, and looks pretty readable to me.

    Ignore me — I didn't see the capturing parentheses. {sigh}. /me goes to find coffee.

    Update: I'd probably write it like this:

    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; sub foo { my $array_ref = ["foo" , "enum('bar,baz')"]; if($array_ref->[1] =~ /enum\('(.*?)'\)/) { return split ",", $1; } } print Dumper(foo());
    The temporary variable you're talking about is automatically created (called "$1") containing the contents of the capturing parentheses. If you had more than one capturing set, they'd be returned as ($1, $2) (ie the match operation returns an arraylist containing the contents of each of the capturing parentheses, hence your needing the [0] in the second version).

    davis
    It wasn't easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://438593]
Approved by davis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-25 08:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found