[25465] in Perl-Users-Digest
Perl-Users Digest, Issue: 7710 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jan 29 18:10:20 2005
Date: Sat, 29 Jan 2005 15:10:12 -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 Sat, 29 Jan 2005 Volume: 10 Number: 7710
Today's topics:
REQ: Help with hash reference to a module <joeblow@joes.com>
Re: REQ: Help with hash reference to a module ioneabu@yahoo.com
Re: REQ: Help with hash reference to a module <spamtrap@dot-app.org>
Re: REQ: Help with hash reference to a module <matternc@comcast.net>
Re: REQ: Help with hash reference to a module <spamtrap@dot-app.org>
Re: REQ: Help with hash reference to a module <joeblow@joes.com>
Re: Search Through List <wksmith@optonline.net>
Re: Search Through List <news@chaos-net.de>
Tom Christiansen's books review <xah@xahlee.org>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 29 Jan 2005 21:12:48 GMT
From: Joe <joeblow@joes.com>
Subject: REQ: Help with hash reference to a module
Message-Id: <Xns95EDA4F29F714joeblowjoescom@216.221.81.119>
Hello all,
Old C programmer here. Trying to pass a reference to a hash to a sub
routine in a module.
Have seen many posts about references, nothing seems to work in the case
of modules.
The following example currently returns the error:
Type of arg 3 to MYSTUFF::ParseAssociative must be hash (not reference
construct
or) at main.pl line 9, near "%myListeners )"
Execution of main.pl aborted due to compilation errors.
I would like it to say:
L= adam, hello0
L= Nothing, hello1
L= Something, hello2
Here are the two files:
test.pl
------------------------------------------------
use MYSTUFF;
my %myListeners;
my $test1 = "Nothing";
my $test2 = "Something";
$myListeners{"adam"} = "Hello0";
MYSTUFF::myWorst( $test1, $test2, \%myListeners );
foreach $listener (sort(keys %myListeners)) {
print "L= $listener, $myListeners{$listener}\n";
}
exit;
------------------------------------------------
MYSTUFF.pm
------------------------------------------------
package MYSTUFF;
sub myWorst ($$\%) {
my ($first, $second, %refArray) = @_;
$refArray{$first} = "hello1";
$refArray{$second} = "hello2";
}
1;
--------------------------------------------------
help, and example are greatly appreciated,thanks.
btw perl5.8
------------------------------
Date: 29 Jan 2005 13:37:39 -0800
From: ioneabu@yahoo.com
Subject: Re: REQ: Help with hash reference to a module
Message-Id: <1107034659.703754.164570@z14g2000cwz.googlegroups.com>
Joe wrote:
> Hello all,
>
> Old C programmer here. Trying to pass a reference to a hash to a sub
> routine in a module.
> Have seen many posts about references, nothing seems to work in the
case
> of modules.
>
> The following example currently returns the error:
> Type of arg 3 to MYSTUFF::ParseAssociative must be hash (not
reference
> construct
> or) at main.pl line 9, near "%myListeners )"
> Execution of main.pl aborted due to compilation errors.
>
>
> I would like it to say:
> L= adam, hello0
> L= Nothing, hello1
> L= Something, hello2
>
> Here are the two files:
>
> test.pl
> ------------------------------------------------
> use MYSTUFF;
>
> my %myListeners;
> my $test1 = "Nothing";
> my $test2 = "Something";
>
> $myListeners{"adam"} = "Hello0";
>
> MYSTUFF::myWorst( $test1, $test2, \%myListeners );
>
> foreach $listener (sort(keys %myListeners)) {
> print "L= $listener, $myListeners{$listener}\n";
> }
>
> exit;
> ------------------------------------------------
>
>
> MYSTUFF.pm
> ------------------------------------------------
> package MYSTUFF;
>
> sub myWorst ($$\%) {
>
> my ($first, $second, %refArray) = @_;
>
> $refArray{$first} = "hello1";
> $refArray{$second} = "hello2";
>
> }
>
> 1;
> --------------------------------------------------
>
> help, and example are greatly appreciated,thanks.
> btw perl5.8
try this:
#!/usr/bin/perl
use strict;
use warnings;
package MYSTUFF;
#the forced parameters thing here
#is something I need to read up on too.
sub myWorst ($$\%) {
my ($first, $second, $refArray) = @_;
${$refArray}{$first} = "hello1";
${$refArray}{$second} = "hello2";
}
package main;
my %myListeners;
my $test1 = "Nothing";
my $test2 = "Something";
$myListeners{"adam"} = "Hello0";
#looks like you use hash and it is passed by reference
#due to the parameter list thing you did above
MYSTUFF::myWorst( $test1, $test2, %myListeners );
foreach my $listener (sort(keys %myListeners)) {
print "L= $listener, $myListeners{$listener}\n";
}
------------------------------
Date: Sat, 29 Jan 2005 16:45:26 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: REQ: Help with hash reference to a module
Message-Id: <L5ydneiCdeRlnmHcRVn-uA@adelphia.com>
Joe wrote:
> Old C programmer here. Trying to pass a reference to a hash to a sub
> routine in a module.
The problem is how you've declared your sub, not the fact that it's in a
module. You've declared it as taking a hash, but passing it a hash
reference.
> sub myWorst ($$\%) {
The subroutine declared here takes a hash as its third argument, and passes
it by reference. You'd call it like this:
myWorst($foo, $bar, %baz);
Inside myWorst(), you'd fetch the arguments like this:
my ($foo, $bar, $baz) = @_;
Note that in the above, $baz is a reference to %baz - that happens
automagically, as a result of the backslash in the prototype.
If you want to explicitly pass a reference, as opposed to having one created
for you automagically, you need to declare the argument as a scalar:
sub myWorst ($$$) {
Then you can call your sub the way you want to:
myWorst($foo, $bar, $bazRef);
You can find more about prototypes in "perldoc perlsub".
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: Sat, 29 Jan 2005 16:48:49 -0500
From: Chris Mattern <matternc@comcast.net>
Subject: Re: REQ: Help with hash reference to a module
Message-Id: <ueydnfRs3I9fmWHcRVn-pA@comcast.com>
Joe wrote:
> Hello all,
>
> Old C programmer here. Trying to pass a reference to a hash to a sub
> routine in a module.
> Have seen many posts about references, nothing seems to work in the case
> of modules.
>
> The following example currently returns the error:
> Type of arg 3 to MYSTUFF::ParseAssociative must be hash (not reference
> construct
> or) at main.pl line 9, near "%myListeners )"
> Execution of main.pl aborted due to compilation errors.
>
>
> I would like it to say:
> L= adam, hello0
> L= Nothing, hello1
> L= Something, hello2
>
> Here are the two files:
>
> test.pl
> ------------------------------------------------
> use MYSTUFF;
>
> my %myListeners;
> my $test1 = "Nothing";
> my $test2 = "Something";
>
> $myListeners{"adam"} = "Hello0";
>
> MYSTUFF::myWorst( $test1, $test2, \%myListeners );
>
> foreach $listener (sort(keys %myListeners)) {
> print "L= $listener, $myListeners{$listener}\n";
> }
>
> exit;
> ------------------------------------------------
>
>
> MYSTUFF.pm
> ------------------------------------------------
> package MYSTUFF;
>
> sub myWorst ($$\%) {
>
> my ($first, $second, %refArray) = @_;
>
> $refArray{$first} = "hello1";
> $refArray{$second} = "hello2";
>
> }
>
> 1;
> --------------------------------------------------
>
> help, and example are greatly appreciated,thanks.
> btw perl5.8
You're passing a reference just fine. What you're NOT doing
is *dereferencing* it--your subroutine treats it as if it
were a hash instead of a reference to one. Your assignment
statement should be:
my ($first, $second, $refArray) = @_;
since a reference is always itself a scalar, regardless of what
it may point to.
Then, you have to dereference the reference when you want
to use the hash:
$$refArray{$first} = "hello1";
$$refArray{$second} = "hello2";
--
Christopher Mattern
"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
------------------------------
Date: Sat, 29 Jan 2005 17:11:32 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: REQ: Help with hash reference to a module
Message-Id: <N5SdnSYVZJyLl2HcRVn-oQ@adelphia.com>
Chris Mattern wrote:
> Joe wrote:
>
>> sub myWorst ($$\%) {
>>
>
> You're passing a reference just fine.
... to a subroutine which has been explicitly prototyped as taking a hash as
its third argument. Hence the error Joe mentioned:
> The following example currently returns the error:
> Type of arg 3 to MYSTUFF::ParseAssociative must be hash
You're right about dereferencing the passed-in reference though. That would
be the next issue to address, after fixing the prototype so that the sub
will take a reference in the first place.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: Sat, 29 Jan 2005 22:58:43 GMT
From: Joe <joeblow@joes.com>
Subject: Re: REQ: Help with hash reference to a module
Message-Id: <Xns95EDB6E7D64A7joeblowjoescom@216.221.81.119>
thank you, that worked the way I was hoping.
thanks everyone else too.
ioneabu@yahoo.com wrote in news:1107034659.703754.164570
@z14g2000cwz.googlegroups.com:
> Joe wrote:
>> Hello all,
>>
>> Old C programmer here. Trying to pass a reference to a hash to a sub
>> routine in a module.
>> Have seen many posts about references, nothing seems to work in the
> case
>> of modules.
>>
>> The following example currently returns the error:
>> Type of arg 3 to MYSTUFF::ParseAssociative must be hash (not
> reference
>> construct
>> or) at main.pl line 9, near "%myListeners )"
>> Execution of main.pl aborted due to compilation errors.
>>
>>
>> I would like it to say:
>> L= adam, hello0
>> L= Nothing, hello1
>> L= Something, hello2
>>
>> Here are the two files:
>>
>> test.pl
>> ------------------------------------------------
>> use MYSTUFF;
>>
>> my %myListeners;
>> my $test1 = "Nothing";
>> my $test2 = "Something";
>>
>> $myListeners{"adam"} = "Hello0";
>>
>> MYSTUFF::myWorst( $test1, $test2, \%myListeners );
>>
>> foreach $listener (sort(keys %myListeners)) {
>> print "L= $listener, $myListeners{$listener}\n";
>> }
>>
>> exit;
>> ------------------------------------------------
>>
>>
>> MYSTUFF.pm
>> ------------------------------------------------
>> package MYSTUFF;
>>
>> sub myWorst ($$\%) {
>>
>> my ($first, $second, %refArray) = @_;
>>
>> $refArray{$first} = "hello1";
>> $refArray{$second} = "hello2";
>>
>> }
>>
>> 1;
>> --------------------------------------------------
>>
>> help, and example are greatly appreciated,thanks.
>> btw perl5.8
>
> try this:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> package MYSTUFF;
>
> #the forced parameters thing here
> #is something I need to read up on too.
> sub myWorst ($$\%) {
> my ($first, $second, $refArray) = @_;
> ${$refArray}{$first} = "hello1";
> ${$refArray}{$second} = "hello2";
> }
>
> package main;
>
> my %myListeners;
> my $test1 = "Nothing";
> my $test2 = "Something";
>
> $myListeners{"adam"} = "Hello0";
>
> #looks like you use hash and it is passed by reference
> #due to the parameter list thing you did above
> MYSTUFF::myWorst( $test1, $test2, %myListeners );
>
> foreach my $listener (sort(keys %myListeners)) {
> print "L= $listener, $myListeners{$listener}\n";
> }
>
------------------------------
Date: Sat, 29 Jan 2005 12:12:27 -0500
From: "Bill Smith" <wksmith@optonline.net>
Subject: Re: Search Through List
Message-Id: <YtPKd.1404$s63.727@fe12.lga>
"Gomer" <chastaib@hotmail.com> wrote in message
news:1106953951.344600.84260@z14g2000cwz.googlegroups.com...
> How can I search through a list in PERL? For example, I want to
> do something like the following:
>
> @namelist = ('JONES', 'SMITH', 'JOHNSON');
>
> print "Enter name of teammate whose password you need to reset, then
> press the ENTER key: \n";
>########### $teammate = <STDIN>;
chomp($teammate = <STDIN>);
>
> ##########if (grep ($teammate, @namelist))
unless (grep /^$teammate$/i, @namelist)
> { print "I can\'t let you do that...";}
> else
> { print "You are going to reset the password for $teammate";}
>
> ----------------------------------------------------------------------
--
Use a matching expression in grep. Note that in scalar context, grep
returns the number of matches. In this case it will be either
1 (true), or 0 (false). I changed the sense of the match
because your messages suggest that you do want to change teammate
passwords, but no others.
Removing the return with chomp simplifies the match. It also removes
the
return from the second print statement. I recommend appending a "\n" to
both print messages.
Bill
------------------------------
Date: Sat, 29 Jan 2005 21:48:28 +0100
From: Martin Kissner <news@chaos-net.de>
Subject: Re: Search Through List
Message-Id: <slrncvntks.8vo.news@maki.homeunix.net>
Tad McClellan wrote :
> Martin Kissner <news@chaos-net.de> wrote:
>> Tad McClellan wrote :
>>> Martin Kissner <news@chaos-net.de> wrote:
>>>> if ($teammate =~ /\b$_\b/i ) # Case does not matter
>
>
> I forgot to mention it earlier, but your pattern was not correct,
> it should be using ^ and $ anchors instead of \b anchors.
Oh, that's right (of course).
Else I would get a match if I tried to reset the password of Mr. Jones
> [...]
> The primary reason it is better IMO is because it *looks like*
> an equality test.
>
> $teammate =~ /^$_$/i vs. lc $teammate eq lc $_
>
> You have to think about the first one for a second to realize
> that it is an equality test in disguise.
>
> I eschew disguising production code. :-)
Thank you for pointing that out.
If one would look at it really strictly it should be
$teammate eq $_
since Jones ne JONES
To me this is mainly a question of understanding Perl.
To the OP it might be a question of convenience, fault liability and
maybe even security.
Thanks again for your help.
Martin
--
perl -e 'print 7.74.117.115.116.11.32.13.97.110.111.116.104.101.114.11
.32.13.112.101.114.108.11.32.13.104.97.99.107.101.114.10.7'
------------------------------
Date: 29 Jan 2005 11:44:01 -0800
From: "Xah Lee" <xah@xahlee.org>
Subject: Tom Christiansen's books review
Message-Id: <1107027841.456771.167890@f14g2000cwb.googlegroups.com>
Tom Christiansen used to have a Perl books review here
http://language.perl.com/critiques/
does anyone know where to find it now?
thanks.
Xah
xah@xahlee.org
http://xahlee.org/PageTwo_dir/more.html
------------------------------
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 7710
***************************************