[24604] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 6780 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jul 8 21:05:46 2004

Date: Thu, 8 Jul 2004 18:05:06 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 8 Jul 2004     Volume: 10 Number: 6780

Today's topics:
    Re: Active State <matthew.garrish@sympatico.ca>
    Re: Active State <invalid-email@rochester.rr.com>
    Re: hash as argument <spamtrap@dot-app.org>
    Re: hash as argument <aaron@deloachcorp.com>
    Re: hash as argument <aaron@deloachcorp.com>
    Re: hash as argument <spamtrap@dot-app.org>
    Re: hash as argument <spamtrap@dot-app.org>
    Re: Negation of RegEx (Dan)
    Re: Negation of RegEx <kkeller-usenet@wombat.san-francisco.ca.us>
    Re: Newbie: How do I  filter output to the screen and w (Mav)
    Re: Newbie: How do I  filter output to the screen and w <noreply@gunnar.cc>
    Re: perl cgi problem <emschwar@pobox.com>
    Re: perl cgi problem <pfancy@bscn.com>
    Re: Regular expression to match surrounding parenthesis <sholden@flexal.cs.usyd.edu.au>
    Re: Returns / list context <noreply@gunnar.cc>
    Re: Returns / list context <perl@my-header.org>
    Re: Returns / list context <spamtrap@dot-app.org>
    Re: Returns / list context <chrisnewton@no.junk.please.btinternet.com>
    Re: Search using a variable in a Perl Script (Philip Scobie)
        waitpid interruped by "Alarm clock" (M Weaver)
    Re: What does this do? <pinyaj@rpi.edu>
    Re: What does this do? <sholden@flexal.cs.usyd.edu.au>
    Re: What does this do? <gnari@simnet.is>
    Re: What does this do? <notvalid@email.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Thu, 8 Jul 2004 19:33:19 -0400
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: Active State
Message-Id: <1RkHc.34829$WM5.1703546@news20.bellglobal.com>


"timgerr" <tim@-123nospan-mtgallagher.com> wrote in message
news:ccjjkj$cre$1@newshost.mot.com...
> I am new to Perl and I need to get some mods on my W32 box.
> I am using Active State Perl with the command PPM.  I can
> search the repositories but I need mods from CPAN.  I know
> how to add repositories but I do not know the location to the
> CPAN Mods that needs to be entered into PPM.

Please post plain-text messages to usenet, not html.

In response to your question, there's little way anyone here can help given
the information you provided. You never said what modules you're trying to
install or even what additional repositories you've added. Have you added
the University of Winnipeg server? (I forget the url off-hand, but it is in
the documentation) Have you tried googling for a ppd file? Have the modules
been tested (and passed) on Windows?

Matt




------------------------------

Date: Fri, 09 Jul 2004 00:16:51 GMT
From: Bob Walton <invalid-email@rochester.rr.com>
Subject: Re: Active State
Message-Id: <40EDE3F2.3050506@rochester.rr.com>

timgerr wrote:

> I am new to Perl and I need to get some mods on my W32 box.  I am using 
> Active State Perl with the command PPM.  I can search the repositories 
> but I need mods from CPAN.  I know how to add repositories but I do not 
> know the location to the CPAN Mods that needs to be entered into PPM.


Well, if the module isn't available though the default PPM repository, 
it's because ActiveState hasn't ported it.  Maybe that's because they 
didn't get to that module yet, or maybe the module didn't pass its tests 
or otherwise work properly when ported.

    http://ppm.activestate.com/BuildStatus/5.8-A.html

might be illuminating.  If you really want a particular package on CPAN, 
you can follow the directions there to directly build the package and 
see how you make out.  For some packages a C compiler is required (and, 
generally, it should be the same C compiler and libraries as was used to 
compile Perl).

You can also try alternative PPM repositories.  Googling for them finds 
lots.  The one at the University of Winnipeg might be worth a look.


 ...
> gerr

-- 
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl



------------------------------

Date: Thu, 08 Jul 2004 18:21:42 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: hash as argument
Message-Id: <SfqdnSSSf5ZlVXDd4p2dnA@adelphia.com>

Aaron DeLoach wrote:

> I am trying to call a sub with two arguments. One being a hash generated
> from a sub, the other being a string. I've tried other variations but can
> only get the receiving sub to recognize either the hash or the string, but
> never both.
> 
> e.g, &do_something(&create_hash, "This is the string.");

You are using Perl 5.x, right? Not Perl 4? Unless you're using Perl 4, or
you know precisely what the & does and why you want to do that, don't use
it. And don't use double-quotes on strings unless it's necessary.

do_something(create_hashref(), 'This is a string');

> sub do_something
> {
>     my (%hash, $string) = @_;
> 
>     process...
> }

sub do_something {
    my ($hashref, $string) = @_;

    # Use a hash reference
    my @keys = keys(%$hashref);
}

> sub create_hash
> {
>     my %h;
>     # construct the hash ...
>     return %h;
> }

sub create_hashref {
    my $h = {};

    # Store something via the hashref
    $h->{'foo'} = 'bar';
    $h->{'baz'} = 'buzz';

    return $h;
}

> Can anyone get me in the right direction?

See also:

perldoc perldsc
perldoc perllol

sherm--

-- 
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


------------------------------

Date: Thu, 8 Jul 2004 17:23:07 -0500
From: "Aaron DeLoach" <aaron@deloachcorp.com>
Subject: Re: hash as argument
Message-Id: <rtGdnZNSx9vRVHDdRVn-hQ@eatel.net>

That's one method I didn't try... but it worked.

Thanks

"Matija Papec" <perl@my-header.org> wrote in message
news:fkgre0hj8hnmkin37di5ilqlfckvbrf43p@4ax.com...
> X-Ftn-To: Aaron DeLoach
>
> "Aaron DeLoach" <aaron@deloachcorp.com> wrote:
> >I am trying to call a sub with two arguments. One being a hash generated
> >from a sub, the other being a string. I've tried other variations but can
> >only get the receiving sub to recognize either the hash or the string,
but
> >never both.
> >
> >e.g, &do_something(&create_hash, "This is the string.");
> >
> >sub do_something
> >{
> >    my (%hash, $string) = @_;
> >
> >    process...
> >}
> >
> >sub create_hash
> >{
> >    my %h;
> >    # construct the hash ...
> >    return %h;
> >}
> >
> >Can anyone get me in the right direction?
>
> Try,
>   my ($string, %hash) = @_;
>
> this is for the same reason you can't
>  my (@arr, $string) = @_;
>
> as @arr gets all arguments from @_
>
>
>
> -- 
> Matija




------------------------------

Date: Thu, 8 Jul 2004 17:27:32 -0500
From: "Aaron DeLoach" <aaron@deloachcorp.com>
Subject: Re: hash as argument
Message-Id: <fK6dndb51eTIV3DdRVn-vA@eatel.net>


"Sherm Pendley" <spamtrap@dot-app.org> wrote in message
news:SfqdnSSSf5ZlVXDd4p2dnA@adelphia.com...
> Aaron DeLoach wrote:
>
> > I am trying to call a sub with two arguments. One being a hash generated
> > from a sub, the other being a string. I've tried other variations but
can
> > only get the receiving sub to recognize either the hash or the string,
but
> > never both.
> >
> > e.g, &do_something(&create_hash, "This is the string.");
>
> You are using Perl 5.x, right? Not Perl 4? Unless you're using Perl 4, or
> you know precisely what the & does and why you want to do that, don't use
> it. And don't use double-quotes on strings unless it's necessary.

Perl 5.x

I use the '&' mainly to distinguish sub calls.  Why not?

I used the double-quotes in the example.

Thanks for your help!

>
> do_something(create_hashref(), 'This is a string');
>
> > sub do_something
> > {
> >     my (%hash, $string) = @_;
> >
> >     process...
> > }
>
> sub do_something {
>     my ($hashref, $string) = @_;
>
>     # Use a hash reference
>     my @keys = keys(%$hashref);
> }
>
> > sub create_hash
> > {
> >     my %h;
> >     # construct the hash ...
> >     return %h;
> > }
>
> sub create_hashref {
>     my $h = {};
>
>     # Store something via the hashref
>     $h->{'foo'} = 'bar';
>     $h->{'baz'} = 'buzz';
>
>     return $h;
> }
>
> > Can anyone get me in the right direction?
>
> See also:
>
> perldoc perldsc
> perldoc perllol
>
> sherm--
>
> -- 
> Cocoa programming in Perl: http://camelbones.sourceforge.net
> Hire me! My resume: http://www.dot-app.org




------------------------------

Date: Thu, 08 Jul 2004 18:37:50 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: hash as argument
Message-Id: <3uidnVF3X49dUXDd4p2dnA@adelphia.com>

Aaron DeLoach wrote:

> I use the '&' mainly to distinguish sub calls.  Why not?

It's deprecated for normal use in Perl 5. It disables prototype checking,
and passes the current @_ to the called sub instead of creating a lexically
scoped @_ within the sub.

So unless you're using a Very Old Perl, or you have a specific reason for
needing one of the above effects, don't use & to call subs.

A better way to make sub calls more visually distinctive is to always use
the ()s, even when you're not passing any arguments. That helps them stand
out visually, but doesn't have the unwanted side-effects of using &.

sherm--

-- 
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


------------------------------

Date: Thu, 08 Jul 2004 18:46:56 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: hash as argument
Message-Id: <quSdnU54ksR8U3DdRVn-ig@adelphia.com>

Aaron DeLoach wrote:

> I used the double-quotes in the example.

The most popular reason for not doing that is that it's misleading to the
eye. A veteran Perl programmer sees double-quoting as a sign that the
string contains variables to be interpolated into it, and knows that
single-quoted strings are constants that aren't interpolated.

Some folks also claim it's inefficient, because Perl has to handle
interpolation for double-quoted strings, but the performance hit is really
pretty trivial.

But these are minor issues...

The reason I personally advise against it is that some folks here will yell
at you and refuse to look at your script until you "fix" the double-quoting
"problem". I agree with them to a point, which is why I mention it at all,
but some folks here make *way* too big an issue of it. So unless you enjoy
being spanked for no good reason, you should avoid double-quotes unless
they're really necessary.

sherm--

-- 
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


------------------------------

Date: 8 Jul 2004 15:51:02 -0700
From: dan_yuan@trendmicro.com (Dan)
Subject: Re: Negation of RegEx
Message-Id: <4c135583.0407081451.5a26b92a@posting.google.com>

Randal's answer /^(?!FOO)/can just deals with a special case in which
the unwanted word is at the beginning of a sentence.

What if the unwanted word could appear at any position of the
sentence?
The look-ahead negtive assertion doesn't work. Just give it a try!

So any other solutions?

I have a detour solution here

$sentence= "I have a problem";
$sentence=~/(?(?=.*\bhave\b)  <something never match>(?{print "True
Branch\n";})| <something always match> (?{print "False Branch\n";}))/;

This is a if () THEN {} ELSE {} statement.  
The condition of if is a look-ahead positive assertion, which works
correctly.
But the problem is when it goes to True branch and has an unmatch
there, a wierd thing happens: it goes to FALSE BRANCH.

if the logic in THEN is ture, it should go out of the statement and
not go into
ELSE branch. This is what I can't understand

Hope someone would solve this problem.

Dan 


>>Andreas> is it possible to construct a single regular expression
which matches
>>Andreas> everything except strings which start with a certain
string?

>>The regex

>>  /^(?!FOO)/

>>matches everything except lines that start with FOO.

>>print "Just another Perl hacker,"; # and the first one, at that






"Andre Wisniewski" <andre.wisniewski@gmx.de> wrote in message news:<opsat1fwpdudlqzq@ares>...
> > I have a special problem as the following:
> >
> > $sentence="a hard problem";
> >
> > I want to build a regular expression for searching there is
> > no word "hard" in the above sentence.
> > Usually, we could try $sentence !~/RegEx/ for this problem.
> > But, I am not allowed to use "!~", I can only use "=~" for this
> > problem, which means that /RegEx/ is a pattern for searching no
> > "hard" in a sentence.
> >
> > Is there anyone who konws the solution of this problem?
> 
> Two weeks ago this problem was discussed already.
>     Subject: REGEX negation
> First read this.


------------------------------

Date: Thu, 8 Jul 2004 16:24:47 -0700
From: Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us>
Subject: Re: Negation of RegEx
Message-Id: <v3lkcc.us3.ln@goaway.wombat.san-francisco.ca.us>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

On 2004-07-08, Dan <dan_yuan@trendmicro.com> wrote:
> But, I am not allowed to use "!~", I can only use "=~" for this
> problem,

Why are you not allowed to use !~?  Sounds like homework to me.

- --keith

- -- 
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom

-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQFA7de+hVcNCxZ5ID8RAmYuAJ0SHm1aUirkhpKPU+WdMHoaZnxopwCggvpE
4krTCU8/idIytSlF109dDes=
=NaGl
-----END PGP SIGNATURE-----


------------------------------

Date: 8 Jul 2004 16:56:19 -0700
From: mluvw47@yahoo.com (Mav)
Subject: Re: Newbie: How do I  filter output to the screen and writing the   orginal output to a file?
Message-Id: <dfaafecd.0407081556.68b03fb7@posting.google.com>

Thanks again. 
I probably don't use die to since I need to get the Failed 
 ...
 close BUILDLOG;
 close $proc;
   #Check Build Result
   if ($? == 0) {
      $buildResult = "Passed";
   }
   else {
      $buildResult = "Failed";
   }

Do you think that is a better way?


Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message news:<2l39pbF7odgcU1@uni-berlin.de>...
> Mav wrote:
> > Thanks Gunnar. That is exactly the answer I am looking for.
> 
> Good, but don't forget to thank Anno who provided the actual solution...
> 
> > If I use system(@args) I can get a $buildResult from it. 
> > $buildResult = system(@args); (That will give me return 0 if build
> > successful).
> > 
> > Now, if I am using open:
> > open my $proc, "@args |" or die $!;
> > Is that anyway I can get the $buildResult?
> 
> You can check the return value from close():
> 
>      close $proc or die "Build failed $! $?";


------------------------------

Date: Fri, 09 Jul 2004 02:28:40 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Newbie: How do I  filter output to the screen and writing the orginal output to a file?
Message-Id: <2l672aF9519nU1@uni-berlin.de>

Mav wrote:
> Gunnar Hjalmarsson wrote:
>> You can check the return value from close():
>> 
>>     close $proc or die "Build failed $! $?";
> 
> I probably don't use die to since I need to get the Failed
> ...
>  close BUILDLOG;
>  close $proc;
>    #Check Build Result
>    if ($? == 0) {
>       $buildResult = "Passed";
>    }
>    else {
>       $buildResult = "Failed";
>    }
> 
> Do you think that is a better way?

If you think it is, I have no reason to object. :)

A closer equivalent to

     $buildResult = system(@args);

would be to just do:

     close $proc;
     $buildResult = $?;

What makes most sense probably depends on how you want to use 
$buildResult.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: Thu, 08 Jul 2004 17:02:34 -0600
From: Eric Schwartz <emschwar@pobox.com>
Subject: Re: perl cgi problem
Message-Id: <etor7rmhzlh.fsf@fc.hp.com>

"pfancy" <pfancy@bscn.com> writes:
> I'm sorr for such a long code. but i did not know what else to do.
> but it gives me the 404 error.

What gives you which 404 error?  

>  I'm beginning to think that i have
> some code typed wrong because it looks for the guestbook.pl file and
> says it can't find it,

You have such a confusion of pronouns here, I can't even begin to
figure out what you're talking about.  'it' seems to mean at least
three things in that one sentence alone.

> it told me to set the chmod for 755 and i did. but it claims it
> can't find the file but i have placed the guestbook.pl into the cgi
> bin.

Fundamentally, you have three problems:

1) You're using Matt Wright's scripts, instead of the superior
   replacements at http://nms-cgi.sourceforge.net/.

2) You're not good at partitioning problems-- that's not a crime, and
   I'm not insulting you by saying it, but it's a skill you'll need to
   develop.  Partitioning a problem is where you figure out if the
   problem is caused by the web server's set up, the script's logic,
   or the script's configuration.  In your case, I imagine it's a
   mixture of the three, but because you don't seem to understand the
   distinctions yourself, it's hard to say for sure.

3) You are either unfamiliar with your web host's configuration, or
   your script's setup.  If it's the first, then you need to talk to
   your web hosting provider.  If the second, you need to talk to
   whoever supports your scripts.

   This is another advantage of using the NMS scripts-- Matt no longer
   supports his code, if he ever did, and there is a mailing list to
   help you with problems running the NMS code.

-=Eric
-- 
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
		-- Blair Houghton.


------------------------------

Date: Thu, 8 Jul 2004 18:14:28 -0500
From: "pfancy" <pfancy@bscn.com>
Subject: Re: perl cgi problem
Message-Id: <40edeff7@news.greennet.net>


"Eric Schwartz" <emschwar@pobox.com> wrote in message
news:etor7rmhzlh.fsf@fc.hp.com...
> "pfancy" <pfancy@bscn.com> writes:
> > I'm sorr for such a long code. but i did not know what else to do.
> > but it gives me the 404 error.
>
> What gives you which 404 error?
>
> >  I'm beginning to think that i have
> > some code typed wrong because it looks for the guestbook.pl file and
> > says it can't find it,
>
> You have such a confusion of pronouns here, I can't even begin to
> figure out what you're talking about.  'it' seems to mean at least
> three things in that one sentence alone.
>
> > it told me to set the chmod for 755 and i did. but it claims it
> > can't find the file but i have placed the guestbook.pl into the cgi
> > bin.
>
> Fundamentally, you have three problems:
>
> 1) You're using Matt Wright's scripts, instead of the superior
>    replacements at http://nms-cgi.sourceforge.net/.
>
> 2) You're not good at partitioning problems-- that's not a crime, and
>    I'm not insulting you by saying it, but it's a skill you'll need to
>    develop.  Partitioning a problem is where you figure out if the
>    problem is caused by the web server's set up, the script's logic,
>    or the script's configuration.  In your case, I imagine it's a
>    mixture of the three, but because you don't seem to understand the
>    distinctions yourself, it's hard to say for sure.
>
> 3) You are either unfamiliar with your web host's configuration, or
>    your script's setup.  If it's the first, then you need to talk to
>    your web hosting provider.  If the second, you need to talk to
>    whoever supports your scripts.
>
>    This is another advantage of using the NMS scripts-- Matt no longer
>    supports his code, if he ever did, and there is a mailing list to
>    help you with problems running the NMS code.
>
> -=Eric
> -- 
> Come to think of it, there are already a million monkeys on a million
> typewriters, and Usenet is NOTHING like Shakespeare.
> -- Blair Houghton.

Thats cool.  I agree with you.  I know there is a lot that i need to learn.
I have downloaded the script from the nms site.  The guest book is the same
as the one i got but better right.




------------------------------

Date: 8 Jul 2004 22:32:09 GMT
From: Sam Holden <sholden@flexal.cs.usyd.edu.au>
Subject: Re: Regular expression to match surrounding parenthesis
Message-Id: <slrncerir9.hdk.sholden@flexal.cs.usyd.edu.au>

On 8 Jul 2004 10:13:02 GMT,
	Anno Siegel <anno4001@lublin.zrz.tu-berlin.de> wrote:
> Abigail  <abigail@abigail.nl> wrote in comp.lang.perl.misc:
>> Josef Moellers (josef.moellers@fujitsu-siemens.com) wrote on MMMCMLXIV
>> September MCMXCIII in <URL:news:ccipqq$2gp$1@nntp.fujitsu-siemens.com>:
>> --  Bob wrote:
>
>> -- > I am trying to create a regular expression to verify that user entered
>> -- > data is surrounded by the same number of open and closed parenthesis.
>
> [...]
>
>> --  In a nutshell: no, this is impossible. (<- that is a definite period!)
[snip FSA explanation]
>> --  This is from the theory of automata. Practical implementations may allow 
>> --  this but the question was whether regular expressions can do this.
>> 
>> 
>> It all depends on your definition of 'regular expressions'. In the context
>> of Perl, a 'regular expression' is something that can be grokked by Perls
>> regular expression engine. And with those regular expressions, you *can*
>> matched strings with balanced parenthesis.
>> 
>> It would be very inpractical if discussion on the forum would refer to
>> "expressions that can be grokked by Perl's regular expression engine"
>> instead of what's understood by everyone but a few pedants, "regular
>> expressions".
>
> The mathematicians who work with regular expressions are just a club of
> pedants?

I'm sure those mathematicians who happen to read clpm know the
difference between the two regular expression definitions and never
think for even a moment that a post on clpm that mentions "regular
expression" means anything but "expressions that can be grokked by
Perl's regular expression engine", hence they aren't in the "few
pedants" set above.

>
> "Regular expression" has two significantly different definitions in
> mathematics and in CS.  Of course, the CS meaning is the primary one
> on clpm, but pointing out the difference is not merely pedantry.

But answering "No" to can a regular expression match X, where X can be
done by perl's non-regular regular expressions but not by regular
regular expressions seems pedantic to me.

Sure, it's fun to show off that you know all about FSAs and PDAs and TMs
but since this is clpm they are all pretty much irrelevant to the
question. Hence the showing off should be accompanied by an answer in
perl, after all someone else will know an answer and can do the showing
themselves.

The OP clearly didn't mean "can a mathematical regular expression do
this?", they meant "can a perl regular expression do this?", hence the
answer of "No" is deceptive. I seriously doubt anyone would ask in clpm
about the properties of a real honest to goodness regular expressions.

In fact I have seen people criticised for asking about less powerful
regular expressions here, asking for a regular expression to match X and
then saying that the answer won't work because grep doesn't understand
all that. Which seems to be the other side of the coin.

-- 
Sam Holden


------------------------------

Date: Fri, 09 Jul 2004 00:11:30 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Returns / list context
Message-Id: <2l5v0qF8vsbfU1@uni-berlin.de>

Chris Newton wrote:
> I want to pass back a sequence of values from a function, to put
> into a hash mapping from strings onto string arrays. I tried this
> (obviously simplified here) approach:

Does your approach include "use warnings;"? It should.

> my %hash;
> my $key = "key";
> 
> @hash{$key} = values();

That line causes a compilation error, since values() is a built-in
Perl function. I assume that you actually are using some other name;
I'm using mysub() below.

It should be noted that @hash{$key} results in a warning, since it is
a slice with only one key. If you had had warnings enabled, that might
have given you enough of a hint to realize what's wrong.

> sub values
> {
>   return ("value1", "value2");
> }
> 
> After this, @hash{"key"} seems to be an array containing just the
> one string "value1". Why wouldn't this call be working in list
> context, and stuffing both values into the hash entry?

Even if you are calling the function in list context, since the slice
only contains one key, the hash cannot assume more than one value.

You may want to either store an array reference:

     $hash{ $key } = [ mysub() ];

or add a key to the hash slice:

     my $key = "key";
     my $key2 = "key2";

     @hash{ $key, $key2 } = mysub();

HTH

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: Fri, 09 Jul 2004 00:09:48 +0200
From: Matija Papec <perl@my-header.org>
Subject: Re: Returns / list context
Message-Id: <orgre0ltr1jnujpj9rcafedh1r3p1f0chl@4ax.com>

X-Ftn-To: Chris Newton 

Chris Newton <chrisnewton@no.junk.please.btinternet.com> wrote:
>I think I've been looking at this too long, now, and I'm just missing 
>the blindingly obvious...
>
>I want to pass back a sequence of values from a function, to put into a 
>hash mapping from strings onto string arrays. I tried this (obviously 
>simplified here) approach:
>
>my %hash;
>my $key = "key";
>
>@hash{$key} = values();
>
>sub values
>{
>   return ("value1", "value2");
>}

First, you'll be probably better with some other function name; "values" is
perl builtin function for returning hash values.

If you want to store an array in hash{$key}, you'll need reference (check
perldoc perlref),

$hash{$key} = [ values() ];

use Data::Dumper; print Dumper \%hash;

>After this, @hash{"key"} seems to be an array containing just the one 
>string "value1". Why wouldn't this call be working in list context, and 
>stuffing both values into the hash entry?

@hash{"key"} is called hash slice and it is used for reading/populating
multiple hash values at once,

@hash{'one', 'two'} = 1..2;
is pretty same as
($hash{'one'}, $hash{'two'}) = 1..2;


-- 
Matija


------------------------------

Date: Thu, 08 Jul 2004 18:14:24 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Returns / list context
Message-Id: <XdSdnZMm45TfWnDdRVn-sw@adelphia.com>

Chris Newton wrote:

> I want to pass back a sequence of values from a function, to put into a
> hash mapping from strings onto string arrays. I tried this (obviously
> simplified here) approach:
> 
> my %hash;
> my $key = "key";

Strict, warnings, useless double-quotes, etc.

> @hash{$key} = values();
> 
> sub values
> {
>    return ("value1", "value2");
> }
> 
> After this, @hash{"key"} seems to be an array containing just the one
> string "value1". Why wouldn't this call be working in list context, and
> stuffing both values into the hash entry?

It's not clear what you were expecting. Did you want to assign the array
elements to separate hash elements? If so, you need to use multiple hash
keys in the assignment, like this:

@hash{'key1','key2'} = getvalues();

If you want to associate an array with a single key, you need to write your
sub so that it returns a reference, and then store the reference:

$hash{'key'} = getvalues();
sub getvalues {
    return ['value1','value2'];
}

Note that in both cases, I named the sub "getvalues" instead of "values" -
the latter is the name of a built-in sub in Perl.

For more on complex data structures - a *lot* more - see:

perldoc perldsc
perldoc perllol

sherm--

-- 
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


------------------------------

Date: Fri, 9 Jul 2004 00:37:54 +0000 (UTC)
From: Chris Newton <chrisnewton@no.junk.please.btinternet.com>
Subject: Re: Returns / list context
Message-Id: <cckpd1$bvb$1@sparta.btinternet.com>

Matija Papec wrote...
> First, you'll be probably better with some other function name;
> "values" is perl builtin function for returning hash values.

Sorry, that was just brain-fade while typing in the simplified version. 
The real code uses a different name.

> If you want to store an array in hash{$key}, you'll need reference
> (check perldoc perlref),
> 
> $hash{$key} = [ values() ];

Thanks; I'd forgotten it needed to become a reference when stored in a 
hash. Told you it would be obvious! :-)

Thanks to all for the replies,
Chris
(who's been programming in too many different languages this week!)



------------------------------

Date: 8 Jul 2004 16:33:17 -0700
From: philipscobie@hotmail.com (Philip Scobie)
Subject: Re: Search using a variable in a Perl Script
Message-Id: <d2c030d8.0407081533.75e29fed@posting.google.com>

Many thanks.

The variable had a space after it.

Somewhat of a red face on this end... Thanks


------------------------------

Date: 8 Jul 2004 15:33:05 -0700
From: matthew.weaver@gmail.com (M Weaver)
Subject: waitpid interruped by "Alarm clock"
Message-Id: <139c56c9.0407081433.737f8e5@posting.google.com>

I open a system call in a pipe, do stuff, then call waitpid($pid,0) on
the process opened in the pipe.  It dies before the system call exits
with an "Alarm clock" message.  Why?

Thanks.

-Matthew



#Here's some code:


my $slowCommandPID = undef;
eval {
    local $SIG{ALRM} = sub { die "MY-TIMEOUT"; };
    alarm (int (60 * $timeoutMins));
    print "DEBUG: Alarm set; opening pipe for '$cmd'...$/" if $debug;
    ($pid = open(CMD_IN, "$cmd |"))  or
        warn("Unable to execute $cmd: $!$/");
    print "DEBUG: Pipe is opened (pid $pid), reading...$/" if $debug;
    while (<CMD_IN>) {
        print $line unless ($noLogging);
    }
    print "DEBUG: Closing pipe for '$cmd'...$/" if $debug;
    close CMD_IN or warn "Couldn't close pipe: $!$/";
    print "DEBUG: Just checking, \$? = $? $/$/" if $debug;
    alarm 0;
};
if ($@) {                       # caught a signal
    print "DEBUG: We got a signal while executing '$cmd'...$/$/" if
$debug;
    unless ($@ =~ /MY-TIMEOUT/) {
        fail($@);
    }                           # it's not our signal
    alarm 0;
    my $timeoutMsg = "[ERROR] System call timed out after $timeoutMins
minutes!";
    print $timeoutMsg;
    if ($killOnTimeout) {
        print "Killing pid $pid...";
        kill ('TERM', $pid) or warn "Could not send SIGTERM to pid
$pid: $!$/";
    } else {
        $slowCommandPID = $pid;
    }
}


 . . .


if ($slowCommandPID) {
    pstatus "Waiting for slow command to complete, pid
$slowCommandPID...";
    my $childPid = waitpid ($slowCommandPID,0); #blocking
    pstatus "Slow command has completed: \$childPid = $childPid,
status = $?";
}


------------------------------

Date: Thu, 8 Jul 2004 18:16:15 -0400
From: Jeff 'japhy' Pinyan <pinyaj@rpi.edu>
Subject: Re: What does this do?
Message-Id: <Pine.SGI.3.96.1040708181456.48121A-100000@vcmr-64.server.rpi.edu>

On 8 Jul 2004, Peter J. Acklam wrote:

>Abigail <abigail@abigail.nl> wrote:
>
>> There's no advantage of writing '$| ++' instead of '$| = 1', but
>> the latter is far less obscure.
>
>And the latter turns on buffering also in the extremely unlikely
>event that $| should ever be -1, in which case $|++ would turn it
>off.

Except that $| can't ever be -1 (unless you've done *| = \-1, in which
case $|++ will throw a "Modification of read-only" error).

$|, as defined by the internal magic supporting it, is set to 0 when given
a false value, and 1 when given ANY true value.

--
Jeff "japhy" Pinyan         %  How can we ever be the sold short or
RPI Acacia Brother #734     %  the cheated, we who for every service
RPI Corporation Secretary   %  have long ago been overpaid?
http://japhy.perlmonk.org/  %  
http://www.perlmonks.org/   %    -- Meister Eckhart




------------------------------

Date: 8 Jul 2004 22:34:30 GMT
From: Sam Holden <sholden@flexal.cs.usyd.edu.au>
Subject: Re: What does this do?
Message-Id: <slrncerivm.hdk.sholden@flexal.cs.usyd.edu.au>

On 08 Jul 2004 23:11:24 +0200, Peter J. Acklam <pjacklam@online.no> wrote:
> Abigail <abigail@abigail.nl> wrote:
>
>> There's no advantage of writing '$| ++' instead of '$| = 1', but
>> the latter is far less obscure.
>
> And the latter turns on buffering also in the extremely unlikely
> event that $| should ever be -1, in which case $|++ would turn it
> off.

$| is a special variable it can never have the value of -1 and hence that
event is not possible at all.

It is either 0 or 1.

perl -le "$|=-1;print $|"

-- 
Sam Holden


------------------------------

Date: Thu, 8 Jul 2004 23:39:52 -0000
From: "gnari" <gnari@simnet.is>
Subject: Re: What does this do?
Message-Id: <ccklsq$v2f$1@news.simnet.is>

"Jeff 'japhy' Pinyan" <pinyaj@rpi.edu> wrote in message
news:Pine.SGI.3.96.1040708181456.48121A-100000@vcmr-64.server.rpi.edu...
>
> $|, as defined by the internal magic supporting it, is set to 0 when given
> a false value, and 1 when given ANY true value.

in that case the $|++ does not even have the advantage
of allowing us to 'localize' the value as in:

    # unknown $| state
    $|++;
    ... do stuff ...
    $|--;
this will alway end by resetting $| to 0, even if it
was originally 1

on the other hand $|-- will always reverse the value.
now I will have to find a use for that ...

gnari







------------------------------

Date: Fri, 09 Jul 2004 00:43:18 GMT
From: Ala Qumsieh <notvalid@email.com>
Subject: Re: What does this do?
Message-Id: <GSlHc.8179$Ch6.1764@newssvr25.news.prodigy.com>

gnari wrote:

> on the other hand $|-- will always reverse the value.
> now I will have to find a use for that ...

Some refer to this as the flip-flop behaviour. It is an invaluable tool 
for perlgolf. You can use it to alternate between two array elements in 
a loop without the need for an explicit variable. Here are a couple of 
perlgolf "holes" with solutions using this behaviour:

http://perlgolf.sourceforge.net/TPR/0/3/
http://perlgolf.sourceforge.net/TPR/0/5a/

--Ala



------------------------------

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 6780
***************************************


home help back first fref pref prev next nref lref last post