Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
The stupid question is the question not asked
 
PerlMonks  

Re: Omitted subroutine arguments

by techcode (Hermit)
on Mar 20, 2011 at 20:51 UTC ( #894372=note: print w/ replies, xml ) Need Help??


in reply to Omitted subroutine arguments

If you really want to get funky you can do that by analyzing @_ array and possibly even it's contents and type/content of stuff in it.

Before people say that it's bad idea - I agree that it usually is a bad idea - but sometimes it just makes sense. Like one module I'm working on - in which I'm trying to avoid as much of additional code that you have when working with DB - but allowing you a lot of customization - and not taking you away from SQL in the usual ORM way.

sub get { my $self = shift; my $columns = ref $_[0] eq 'ARRAY' ? join(', ', @{ shift() }) : ' +*'; my $db_table = shift || croak "I really need to know DB/Table name +"; my $field = @_ == 2 ? shift : 'id'; my $value = shift || croak "I really need key value"; my ($db, $table) = ($db_table =~ /^.+\..+$/) ? split(/\./, $db_tab +le) : ($self->default_db(), $db_table); my $dbh = $self->dbh($db); my $sth = $dbh->prepare_cached("SELECT $columns FROM $table WHERE +$field = ?", undef, 2); $sth->execute($value); my $resp = $sth->rows() ? $sth->fetchrow_hashref() : undef; return $resp; }
This allows you to call this method using any of these ways:
$object->get('tablename', 10); $object->get('dbname.tablename', 10); $object->get([qw/id foo bar/], 'tablename', 10); $object->get('tablename', 'key_field', 10); $object->get([qw/id foo bar/], 'dbname.tablename', 'key_field', 10);

Yes I could have used hash(ref) based params - but I wanted to avoid them because that would add what I found as unnececary cruft - that I am trying to avoid in the first place - looking something like:

$object->get( fields => [qw/id foo bar/], database => 'dbname', table => 'tablename', key_field => 'key_field', key_value => 10, );

And to repeat, in general it's a bad idea, but IMHO in this case it does make sense as it replaces a familiar structure of SELECT _fields_ FROM _dbname_._tablename_ WHERE _id_field_ = _id_value_; (added bonus that DB can be a different connection/DBH) so it's relatively easy to remeber ordering and uses the most usual defaults if you ommit them.

In your case it could be something like:

sub test1 { my $arg1 = shift; # but if you wanna print $arg2 then use '' instead of undef ot +herwise you get warning # and you do have use strict; use warnings; at the top right? +:) my $arg2 = @_ >= 2 || ref $_[0] ne 'ARRAY' ? shift : undef; my $argrest = shift; print "arg1=$arg1, arg2=$arg2, argrest=@$argrest\n\n"; } test1('arg1', [qw/foo bar/]); test1('arg1', \@otherargs); test1('arg1', 'arg2'); test1('arg1', 'arg2', [qw/foo bar/]);


Have you tried freelancing/outsourcing? Check out Scriptlance - I work there since 2003. For more info about Scriptlance and freelancing in general check out my home node.


Comment on Re: Omitted subroutine arguments
Select or Download Code
Re^2: Omitted subroutine arguments
by tel2 (Scribe) on May 10, 2011 at 01:44 UTC
    Thanks for taking the time to share that, techcode.
    Just noticed it now.
    Tel2

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://894372]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-04-18 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (464 votes), past polls