FW: Optional Variables
I don't know how or where to put how to use this $option. What if my Default value is a sql statement? Can I just include the name of the file handle between the brackets? -Original Message- From: Brett W. McCoy [mailto:[EMAIL PROTECTED]] Sent: Friday, June 29, 2001 6:40 PM To: M.W. Koskamp Cc: Evgeny Goldin (aka Genie); [EMAIL PROTECTED] Subject: Re: Optional Variables On Fri, 29 Jun 2001, M.W. Koskamp wrote: my $option = @ARGV ? shift : DEFAULT VALUE; Above option only works for 1 parameter tho (and commandline arguments). For function calls i like to use 'named parameters' by accepting a hash of options. Well, yeah, but the topic *was* command-line arguments, not function arguments. You can easily loop through @ARGV, unshifting as you go, until @ARGV is depleted and default values assigned. -- Brett http://www.chapelperilous.net/btfwk/ A free society is one where it is safe to be unpopular. -- Adlai Stevenson
Re: FW: Optional Variables
On Thu, 5 Jul 2001, Kim Green wrote: I don't know how or where to put how to use this $option. What if my Default value is a sql statement? Can I just include the name of the file handle between the brackets? You can do something like my $option = @ARGV ? shift : 'SELECT * FROM my_table'; or my $option = @ARGV ? shift : '/usr/local/db/queries.sql'; -- Brett http://www.chapelperilous.net/btfwk/ The future isn't what it used to be. (It never was.)
Re: FW: Optional Variables
Oops, sorry I got tied up here and forgot I was going to send you more info. Basicly what you need to do is this: my $query = select this, that from table where constant_where_clause_stuff = other constant_stuff ; $option = @ARGV ? and filename = $ARGV[0] : ; $query .= $option . group by whatever sort by whatever; $sth = $dbh-prepare($query); $sth-execute; . . . etc. On 05 Jul 2001 13:47:14 -0400, Kim Green wrote: I don't know how or where to put how to use this $option. What if my Default value is a sql statement? Can I just include the name of the file handle between the brackets? -Original Message- From: Brett W. McCoy [mailto:[EMAIL PROTECTED]] Sent: Friday, June 29, 2001 6:40 PM To: M.W. Koskamp Cc: Evgeny Goldin (aka Genie); [EMAIL PROTECTED] Subject: Re: Optional Variables On Fri, 29 Jun 2001, M.W. Koskamp wrote: my $option = @ARGV ? shift : DEFAULT VALUE; Above option only works for 1 parameter tho (and commandline arguments). For function calls i like to use 'named parameters' by accepting a hash of options. Well, yeah, but the topic *was* command-line arguments, not function arguments. You can easily loop through @ARGV, unshifting as you go, until @ARGV is depleted and default values assigned. -- Brett http://www.chapelperilous.net/btfwk/ A free society is one where it is safe to be unpopular. -- Adlai Stevenson -- Today is Sweetmorn, the 40th day of Confusion in the YOLD 3167 Pzat!
Re: FW: Optional Variables
On 29 Jun 2001 09:00:50 -0700, Randal L. Schwartz wrote: Chas == Chas Owens [EMAIL PROTECTED] writes: Chas my $query = Chas SELECT a.filename, a.size, a.date, b.owner, c.group Chas FROM filesystem a, outer owner b, outer group c Chas WHERE a.uid = b.uid Chas AND a.gid = b.gid; Chas my @query_options = ( Chas AND filename = , Chas AND size = , Chas AND date = Chas ); Chas my $i = 0; Chas #thanks go to Merlyn for making me think about @ARGV instead of using Chas #defined(ARGV[0]) Chas $query .= $query_options[$i++] . shift while (@ARGV); my @query_options = qw(filename size date); Yay, I finally get to stand up for myself: I thought about using just the column names, but if the option's operator should be OR or AND NOT instead of just AND? Of course, you could always require an operator, the field, an operator, and the data on the commandline like this: find_files AND filename = spec.txt AND size 1024 but this seems to be a utility script and that level of user interaction is undesirable since the point of such script is often to just make being a DBA easier. Of course, such a script would probably be much nicer to use if we were to use Getopt::Std, but then all of these tricks are pointless. $query .= sprintf AND %s = %s, shift @query_options, shift while @ARGV; Except I'd probably go the placeholder approach... my @query_options = qw(filename size date); my $query = join , 'END', map AND $_ = ?, @query_options[0..$#ARGV]; SELECT a.filename, a.size, a.date, b.owner, c.group FROM filesystem a, outer owner b, outer group c WHERE a.uid = b.uid AND a.gid = b.gid END my $sth = $dbh-prepare($query); $sth-execute($query, @ARGV); $sth-mumble_fetch_mumble... That way you don't have to worry about weird values in @ARGV. -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 [EMAIL PROTECTED] URL:http://www.stonehenge.com/merlyn/ Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! -- Today is Setting Orange, the 34th day of Confusion in the YOLD 3167 Hail Eris!