[25680] in Perl-Users-Digest
Perl-Users Digest, Issue: 7921 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Mar 29 13:27:33 2005
Date: Tue, 29 Mar 2005 10:27:22 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 29 Mar 2005 Volume: 10 Number: 7921
Today's topics:
Subroutine paramater question <nospam@nospam.com>
Re: Subroutine paramater question <Im.so.HuckinFappy@spamgourmet.com>
Re: Subroutine paramater question <bkimelman@JUNK.sympatico.ca>
Re: Subroutine paramater question <sbryce@scottbryce.com>
Re: Subroutine paramater question <spamtrap@dot-app.org>
Re: Subroutine paramater question <jurgenex@hotmail.com>
Re: Subroutine paramater question <matternc@comcast.net>
Re: Subroutine paramater question <nospam@nospam.com>
Re: Subroutine paramater question <sbryce@scottbryce.com>
Re: Subroutine paramater question <robic0@yahoo.com>
Re: Subroutine paramater question <robic0@yahoo.com>
Re: Subroutine paramater question <robic0@yahoo.com>
Re: Subroutine paramater question <robic0@yahoo.com>
Re: Subroutine paramater question <1usa@llenroc.ude.invalid>
Re: Subroutine paramater question <robic0@yahoo.com>
Re: Subroutine paramater question <1usa@llenroc.ude.invalid>
Re: Subroutine paramater question <nobull@mail.com>
Re: Subroutine paramater question <nobull@mail.com>
Re: Subroutine paramater question robic0@yahoo.com
Re: Subroutine paramater question <nobull@mail.com>
SYstem wait display <joe.cipale@radisys.com>
Re: The error "Use of uninitialized value " <1usa@llenroc.ude.invalid>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 26 Mar 2005 13:54:44 -0500
From: "daniel kaplan" <nospam@nospam.com>
Subject: Subroutine paramater question
Message-Id: <1111863599.347584@nntp.acecape.com>
I know that in my subroutines I get my parameters from @_.
But can I get a count of @_ first, so I know how many to extract?
or
Can I just extract the maximum without fear of some type of "error" occuring
when the maximum isn't there?
In other words:
If I make
sub MyText
{
my ($message, $size, $color) = @_
but in some intances I only call it as MyText("Hello World", 3) or even
MyText("Goodbye Cruel World")
can I be assured that an error won't occur?
Obviosuly in these cases where I do not include a size or color I would use
a default. Am just trying to write a one subroutine fits many purposes
function.
Hope I worded this properly.
Thanks ahead as usual,
Daniel
------------------------------
Date: Sat, 26 Mar 2005 12:06:43 -0700
From: Jeff <Im.so.HuckinFappy@spamgourmet.com>
Subject: Re: Subroutine paramater question
Message-Id: <d24bs4$h6l7@xco-news.xilinx.com>
daniel kaplan wrote:
> I know that in my subroutines I get my parameters from @_.
>
> But can I get a count of @_ first, so I know how many to extract?
>
> or
>
> Can I just extract the maximum without fear of some type of "error" occuring
> when the maximum isn't there?
>
> In other words:
>
> If I make
>
> sub MyText
> {
> my ($message, $size, $color) = @_
>
>
> but in some intances I only call it as MyText("Hello World", 3) or even
> MyText("Goodbye Cruel World")
>
> can I be assured that an error won't occur?
>
> Obviosuly in these cases where I do not include a size or color I would use
> a default. Am just trying to write a one subroutine fits many purposes
> function.
Then be more explicit about your parameters. What you're doing is
called positional parameters, and is a fine option for simpler code. But
when you start having multiple optional parameters, you run into
problems, as you have found. What you may wish to consider is named
parameters, which uses a hash:
sub MyText
{
my %args = @_;
my $message = $args{ message } || 'No Message given';
my $size = $args{ size } || 'No size Given';
my $color = $args{ color } || 'Red...No, Blue!'
}
MyText( message => 'Goodbye Cruel World',
size => 3,
color => 'Green' );
MyText( message => 'Whatever' );
HTH,
~Jeff
------------------------------
Date: Sat, 26 Mar 2005 14:08:07 -0500
From: surfking <bkimelman@JUNK.sympatico.ca>
Subject: Re: Subroutine paramater question
Message-Id: <MPG.1caf7d1f6f3a5042989877@news1.on.sympatico.ca>
While wandering through cyberspace on Sat, 26 Mar 2005 13:54:44 -0500
daniel kaplan said :
> I know that in my subroutines I get my parameters from @_.
>
> But can I get a count of @_ first, so I know how many to extract?
>
> or
>
> Can I just extract the maximum without fear of some type of "error" occuring
> when the maximum isn't there?
>
> In other words:
>
> If I make
>
> sub MyText
> {
> my ($message, $size, $color) = @_
>
>
> but in some intances I only call it as MyText("Hello World", 3) or even
> MyText("Goodbye Cruel World")
>
> can I be assured that an error won't occur?
>
> Obviosuly in these cases where I do not include a size or color I would use
> a default. Am just trying to write a one subroutine fits many purposes
> function.
>
> Hope I worded this properly.
>
> Thanks ahead as usual,
>
> Daniel
my $num_parms;
$num_parms = scalar @_;
------------------------------
Date: Sat, 26 Mar 2005 12:46:42 -0700
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: Subroutine paramater question
Message-Id: <B9KdnbZ-nKi_IdjfRVn-oA@comcast.com>
Jeff wrote:
> sub MyText
> {
> my %args = @_;
> my $message = $args{ message } || 'No Message given';
> my $size = $args{ size } || 'No size Given';
> my $color = $args{ color } || 'Red...No, Blue!'
> }
>
> MyText( message => 'Goodbye Cruel World',
> size => 3,
> color => 'Green' );
>
> MyText( message => 'Whatever' );
This is simpler, but less flexible. The disadvantage of this method is
that you cannot specify a color unless you also specify a size. This
maintains the positional parameters:
use strict;
use warnings;
MyText ('Passed in');
sub MyText
{
my ($message, $size, $color) = @_;
$message = $message || 'Default Message';
$size = $size || '12';
$color = $color || 'black';
print "Message = $message\nSize = $size\nColor = $color";
}
------------------------------
Date: Sat, 26 Mar 2005 14:54:26 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Subroutine paramater question
Message-Id: <H8WdnZrjaN1uINjfRVn-qQ@adelphia.com>
Scott Bryce wrote:
> Jeff wrote:
>
>> sub MyText
>> {
>> my %args = @_;
>> my $message = $args{ message } || 'No Message given';
>> my $size = $args{ size } || 'No size Given';
>> my $color = $args{ color } || 'Red...No, Blue!'
>> }
>>
>> MyText( message => 'Goodbye Cruel World',
>> size => 3,
>> color => 'Green' );
>>
>> MyText( message => 'Whatever' );
>
> This is simpler, but less flexible. The disadvantage of this method is
> that you cannot specify a color unless you also specify a size.
You've got it backwards. What you're describing is the disadvantage of using
positional params - if you want to specify color, it has to be in the third
position, meaning you have to also specify a size.
With named params, you can specify any or all of them, in any order you
like.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: Sat, 26 Mar 2005 20:04:46 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Subroutine paramater question
Message-Id: <yfj1e.25444$b_6.5673@trnddc01>
daniel kaplan wrote:
> I know that in my subroutines I get my parameters from @_.
>
> But can I get a count of @_ first, so I know how many to extract?
Why don't you just use @_ in scalar context? After all it is just an array
and in no way special.
use strict; use warnings;
sub t{
print "Number of parameters: " . @_ . "\n";
}
t(1,2);
t(1, 3, 45,65);
jue
------------------------------
Date: Sat, 26 Mar 2005 15:54:21 -0500
From: Chris Mattern <matternc@comcast.net>
Subject: Re: Subroutine paramater question
Message-Id: <Bdadnb6Y_pFjVtjfRVn-iQ@comcast.com>
daniel kaplan wrote:
> I know that in my subroutines I get my parameters from @_.
>
> But can I get a count of @_ first, so I know how many to extract?
Sure. @_ in a scalar context gives the number of items in the array,
just like any other array.
>
> or
>
> Can I just extract the maximum without fear of some type of "error"
> occuring when the maximum isn't there?
Yah, you can do that.
>
> In other words:
>
> If I make
>
> sub MyText
> {
> my ($message, $size, $color) = @_
>
>
> but in some intances I only call it as MyText("Hello World", 3) or even
> MyText("Goodbye Cruel World")
>
> can I be assured that an error won't occur?
No error will occur. However, if the array isn't large enough, the
variables that can't be provided for will be undefined. If you have
enabled warnings (which you really should), you will be warned if
you try to use the value of an undefined variable.
>
> Obviosuly in these cases where I do not include a size or color I would
> use
> a default.
And you can do that very easily after the above assignment by saying:
$color = "chartreuse" unless defined $color;
> Am just trying to write a one subroutine fits many purposes
> function.
>
> Hope I worded this properly.
>
> Thanks ahead as usual,
>
> Daniel
--
Christopher Mattern
"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
------------------------------
Date: Sat, 26 Mar 2005 17:29:22 -0500
From: "daniel kaplan" <nospam@nospam.com>
Subject: Re: Subroutine paramater question
Message-Id: <1111876294.3316@nntp.acecape.com>
"Chris Mattern" <matternc@comcast.net> wrote in message
news:Bdadnb6Y_pFjVtjfRVn-iQ@comcast.com...
> daniel kaplan wrote:
> No error will occur. However, if the array isn't large enough, the
> variables that can't be provided for will be undefined. If you have
> enabled warnings (which you really should), you will be warned if
> you try to use the value of an undefined variable.
thanks to all that answered. i think after sifting through it all i am will
stick
to my current method, especially if I am comfortable with:
> No error will occur.
These modules are for my own use so I'll just be real careful. Thanks again
all.
daniel
------------------------------
Date: Sat, 26 Mar 2005 16:18:38 -0700
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: Subroutine paramater question
Message-Id: <FKCdnSklp_JTcNjfRVn-2w@comcast.com>
Sherm Pendley wrote:
> You've got it backwards. What you're describing is the disadvantage of using
> positional params - if you want to specify color, it has to be in the third
> position, meaning you have to also specify a size.
>
> With named params, you can specify any or all of them, in any order you
> like.
Actually, that is what I meant. My "this" must have been ambiguous. By
"this" I meant my example.
So to clarify, my example is simpler, Jeff's example is more flexible.
------------------------------
Date: 26 Mar 2005 15:36:22 -0800
From: "robic0@yahoo.com" <robic0@yahoo.com>
Subject: Re: Subroutine paramater question
Message-Id: <1111880182.121448.80620@f14g2000cwb.googlegroups.com>
Daniel,
Use the hash method, unless like
you say you would know for sure whats passed and in
what order.
aka:
$failure = yourcall ('what' => 1, 'the' => 2, 'heck' => 3);
die "Can't do it! Failure code = $failure" if ($failure);
sub yourcall
{
return 1 if (!@_);
for (@_) {
if (lc($_) eq 'what') {
#u got what
}
elsif (lc($_) eq 'the') {
#u got the
}
elsif (lc($_) eq 'heck') {
#u got heck
} else {
return 2; # invalid parm
}
}
# func body
}
my $.02 = .01; #euro
------------------------------
Date: 26 Mar 2005 15:38:58 -0800
From: "robic0@yahoo.com" <robic0@yahoo.com>
Subject: Re: Subroutine paramater question
Message-Id: <1111880338.383700.253570@z14g2000cwz.googlegroups.com>
Doh!, take off that last else...
------------------------------
Date: 26 Mar 2005 15:39:23 -0800
From: "robic0@yahoo.com" <robic0@yahoo.com>
Subject: Re: Subroutine paramater question
Message-Id: <1111880363.395648.308560@g14g2000cwa.googlegroups.com>
Doh!, take off that last else...
------------------------------
Date: 26 Mar 2005 15:50:39 -0800
From: "robic0@yahoo.com" <robic0@yahoo.com>
Subject: Re: Subroutine paramater question
Message-Id: <1111881039.463627.200290@l41g2000cwc.googlegroups.com>
Its all wrong, disregard...
------------------------------
Date: Sat, 26 Mar 2005 23:55:03 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Subroutine paramater question
Message-Id: <Xns9625C071780D5asu1cornelledu@127.0.0.1>
"robic0@yahoo.com" <robic0@yahoo.com> wrote in
news:1111881039.463627.200290@l41g2000cwc.googlegroups.com:
> Its all wrong, disregard...
Now, this is what I call an astute observation.
Sinan.
------------------------------
Date: 26 Mar 2005 16:13:28 -0800
From: "robic0@yahoo.com" <robic0@yahoo.com>
Subject: Re: Subroutine paramater question
Message-Id: <1111882408.179277.280710@l41g2000cwc.googlegroups.com>
Hows this astute
$failure = yourcall ('what' => 1, 'the' => 2, 'heck' => 3);
die "Can't do it! Failure code = $failure" if ($failure);
sub yourcall
{
return 1 if (!@_);
for (my $i=0; i<@_; i+=2) {
my $key = $_[i];
if (lc($key) eq 'what') {
#u got what
}
elsif (lc($key) eq 'the') {
#u got the
}
elsif (lc($key) eq 'heck') {
#u got heck
} else {
return 2; # invalid parm
}
}
# func body
}
my $.02 = .01; #euro
------------------------------
Date: Sun, 27 Mar 2005 00:26:41 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Subroutine paramater question
Message-Id: <Xns9625C5CEA381Basu1cornelledu@127.0.0.1>
"robic0@yahoo.com" <robic0@yahoo.com> wrote in
news:1111882408.179277.280710@l41g2000cwc.googlegroups.com:
> Hows this astute
Huh?
> for (my $i=0; i<@_; i+=2) {
What happened when you tested this code before posting it?
Sinan
------------------------------
Date: Sun, 27 Mar 2005 09:36:56 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: Subroutine paramater question
Message-Id: <d25ugq$hsg$2@sun3.bham.ac.uk>
Scott Bryce wrote:
> $message = $message || 'Default Message';
There is a ||= operator.
$message ||= 'Default Message';
------------------------------
Date: Sun, 27 Mar 2005 09:46:28 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: Subroutine paramater question
Message-Id: <d25v2n$iab$1@sun3.bham.ac.uk>
robic0@yahoo.com wrote:
> Daniel,
>
> Use the hash method, unless like
> you say you would know for sure whats passed and in
> what order.
Er, yes. If you don't use positional parameters then use a hash.
> $failure = yourcall ('what' => 1, 'the' => 2, 'heck' => 3);
> die "Can't do it! Failure code = $failure" if ($failure);
>
>
> sub yourcall
> {
> return 1 if (!@_);
> for (@_) {
> if (lc($_) eq 'what') {
> #u got what
> }
> elsif (lc($_) eq 'the') {
> #u got the
> }
> elsif (lc($_) eq 'heck') {
> #u got heck
> } else {
That is wrong at so many levels.
It does not use a hash.
It would be confused by 'what' => 'the'.
In the block 'u got what' there's no simple way to pick up the 1.
Case insensativity in indetifiers in Perl is not the norm so unless you
have a special reason it is a retrograde step to make your named
paramters case insensative.
------------------------------
Date: Mon, 28 Mar 2005 23:20:25 -0800
From: robic0@yahoo.com
Subject: Re: Subroutine paramater question
Message-Id: <e10i41tdu52br6t19et911vrg9se3ihem2@4ax.com>
On Sun, 27 Mar 2005 00:26:41 GMT, "A. Sinan Unur"
<1usa@llenroc.ude.invalid> wrote:
>"robic0@yahoo.com" <robic0@yahoo.com> wrote in
>news:1111882408.179277.280710@l41g2000cwc.googlegroups.com:
>
>> Hows this astute
>
>Huh?
>
>> for (my $i=0; i<@_; i+=2) {
>
>What happened when you tested this code before posting it?
>
>Sinan
Is there a prblem with this?
sub SetSomeParams
{
my (@args) = @_;
my $val;
if (@args > 0)
{
while (($_, $val) = splice @args, 0, 2) {
if (/^red$/i) {
#u got red, so something with $val;
}
elsif (/^green$/i) {
#u got green, so something with $val;
}
elsif (/^blue$/i) {
#u got blue, so something with $val;
}
}
}
}
------------------------------
Date: Tue, 29 Mar 2005 09:02:25 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: Subroutine paramater question
Message-Id: <d2b586$4jk$1@sun3.bham.ac.uk>
robic0@yahoo.com wrote:
>
> Is there a prblem with this?
Yes, as I said before it's wrong at many levels. You've now fixed the
code so it will actually work (well almost) but the other things that
are wrong with it persist.
> sub SetSomeParams
> {
> my (@args) = @_;
> my $val;
You are suffering that nasty affliction known as premature declaration.
> if (@args > 0)
@args cannont be < 0 so simply if(@args).
But if @args is emply the the loop will iterate zero times anyhow. It
is a bad (although supprisingly common) programming habit to enclose
every loop inside and if() that skips over the loop if it would iterate
zero times.
> {
> while (($_, $val) = splice @args, 0, 2) {
You forgot to localize the $_. You are stomping all over the caller's
$_. This will cause weird and hard to find bugs soemwhere down the road.
For reasons I've explained numerous times before you should actually
local(*_), never local($_).
Better still, in this case, don't use $_ or use for().
> if (/^red$/i) {
> #u got red, so something with $val;
> }
> elsif (/^green$/i) {
> #u got green, so something with $val;
> }
> elsif (/^blue$/i) {
> #u got blue, so something with $val;
> }
> }
> }
> }
Making identifiers case insenvative is unperlish.
Of course the main thing wrong with you code is that it's a lot more
complex and less readable than using a hash. Unless you acutually need
each argument to be able to be specified more than once just use a hash.
sub SetSomeParams
{
my %args = @_;
#something with $args{red}, $args{green} and $args{blue}
}
------------------------------
Date: 28 Mar 2005 09:51:24 -0800
From: "joe.cipale@radisys.com" <joe.cipale@radisys.com>
Subject: SYstem wait display
Message-Id: <1112032284.420285.223580@f14g2000cwb.googlegroups.com>
I am in need of some advice. My application needs to prompt the user to
be patient for approx. 3 minutes while an enterprise server blade
reboots in a system chassis. I would like to use an
hourglass icon that will turn over once the 'sand' runs out. I there a
Tk lib function/method that will accomplish this? None of the books on
Tk I have referenced show such a device.
TIA,
Joe
------------------------------
Date: Wed, 23 Mar 2005 12:33:38 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: The error "Use of uninitialized value "
Message-Id: <Xns96224CED75143asu1cornelledu@127.0.0.1>
Sherm Pendley <spamtrap@dot-app.org> wrote in
news:7fSdnR6svMwhAt3fRVn-2w@adelphia.com:
> A. Sinan *could* have chosen to be helpful and point out that the text
> in the subject was a warning.
I do recommend that you read the full post before making such claims.
Just for the record:
+> "A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote in
+> news:Xns96217BE1FE637asu1cornelledu@127.0.0.1:
+
+>> "geek" <metri.jain@gmail.com> wrote in
+>> news:1111509770.079356.323180@g14g2000cwa.googlegroups.com:
+>>
+>>> I am getting this error
+>>
+>> which error?
+
+...
+
+>> It is now up to you to fix the rest of the jumble. In the process,
+>> you might also discover what "this" error is. "Use of uninitialized
+>> value" is a warning, not an error.
Sinan.
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 7921
***************************************