Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Default values in functions arguments

by rodry (Beadle)
on Sep 27, 2000 at 21:56 UTC ( #34251=perlquestion: print w/replies, xml ) Need Help??
rodry has asked for the wisdom of the Perl Monks concerning the following question:

I wrote functions to perform the INSERT, UPDATE, and DELETE queries in the database. They take the column values as named parameters.

Sometimes, in UPDATE and INSERT queries, I do not have values for all the columns. I would like the function to simply use the default value I assign to them in the hash of values, like this:

sub function_sql { my %values = ( VALUE1 => 'one value', VALUE2 => 'two values', @_ );

You get the idea. For example, let's say I only pass it arguments for VALUE1. I would like VALUE2 to contain "two values". However, the @_ seems to replace it with UNDEF.

Thanks. PS: If you know of a better way to accomplish this task (have functions do the SQL for you), I would also like to hear them.

Replies are listed 'Best First'.
Re: Default values in functions arguments
by chromatic (Archbishop) on Sep 27, 2000 at 23:07 UTC
    I've seen something similar with constructors. A hash is set up with default values, and the constructor can take a hash or hash ref as an argument.
    sub new { my $class = shift; my $hash_ref = shift; my $self = { value => '100', color => 'charcoal', }; @$self{keys %$hash_ref} = (values %$hash_ref) if (defined %$hash_r +ef); bless($self, $class); }
    Passing in a list like you have won't let you do the nice list slice operation, and it's possible to pass in new values than what are defined.

    Still, it's a pretty nice technique, and there's no reason it has to be limited to constructors. In your case, it just might work.

Re: Default values in functions arguments
by mdillon (Priest) on Sep 27, 2000 at 22:12 UTC
RE: Default values in functions arguments
by Adam (Vicar) on Sep 28, 2000 at 00:18 UTC
    I use a slightly different, and probably slower, strategy for default arguments. I like it though, because it allows me to pass in multiple hashrefs. So it goes something like:
    sub Whatever { # Optional test of the arguments. Good for catching programmer err +ors. # Not that anyone here ever makes any. 8-) die 'Takes a hash ref' if @_ and grep { "HASH" ne ref } @_; # Set up the defaults here: my %args = ( KEY1 => 'default value', KEY2 => 'etc...' ); # And process the args. my $index = @_; # Intentionally reverse the order. # The first hash thus has precedence. while( --$index >= $[ ) { $args{$_} = $_[$index]->{$_} for keys %{$_[$index]} } # Do stuff with %args. }
    Also, instead of saying keys, you could use an array of legal key values (using uc or lc to handle case issues) and then overwrite $arg{$legalkey} if it exists in the passed in hash.

    Note that I keep saying "passed in hash", what I should say is "hash that the passed in hashref references", but I'm lazy. Then again I added this comment... sigh

Re: Default values in functions arguments
by ar0n (Priest) on Sep 27, 2000 at 22:11 UTC
    Do you mean this:
    $values{VALUE1} ||= "Default Value"; $values{VALUE2} ||= "Default Value";


Re: Default values in functions arguments
by jptxs (Curate) on Sep 27, 2000 at 22:44 UTC

    not sure what database you're using or if these 'default values' would change from time to time depending on context, but you could let the database take care of that for you. Most DBs will let you assign defaults for columns. I'm an Oracle guy and I do this in a few places and it works like a charm. obviously, this is only useful if these are true 'defaults' that would always be the same in the absence of real data to insert.

    -- I'm a solipsist, and so is everyone else. (think about it)

Re: Default values in functions arguments
by little (Curate) on Sep 27, 2000 at 22:45 UTC
    I suggest you write "NULL" to the questioned fields of DataBase if you don't want to insert values and if these aren't required. Though you can continue without big changes.
    I think this is, what the "NULL" was implemented for. :-)
    Have a nice day
    All decision is left to your taste
RE: Default values in functions arguments
by runrig (Abbot) on Sep 28, 2000 at 01:53 UTC
    What you have should work for what you describe, but it seems more like you are passing in a 'VALUE2' key with an undef value. If that's the case, then this may help:
    #!/usr/local/bin/perl -w use strict; my %hsh = (a=>1,c=>3,d=>undef); my %hsh1 = myfunc(%hsh); while (my ($key, $value) = each %hsh1) { print "$key=$value\n"; } sub myfunc { my %hsh = @_; my %dflt_values = (a=>0,b=>0,c=>0,d=>0); $hsh{$_} = $dflt_values{$_} for grep { not defined $hsh{$_} } keys %d +flt_values; %hsh; }
A better way to accomplish this task (was Re: Default values in functions arguments)
by Anonymous Monk on Sep 28, 2000 at 11:03 UTC

    In reply to your PS:

    If you have really sophisticated DB scheme and want to use OO-Perl abstraction of its structure instead of writing long SQL queries look at BingoX or Alzabo.

    Today restrictions: the former works only with RDBMS which support transactions (read - works with Postgres but does not work with MySQL), the latter - only with MySQL.

    -- brother ab

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://34251]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2017-02-24 10:59 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (354 votes). Check out past polls.