[21695] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3899 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Oct 2 06:10:48 2002

Date: Wed, 2 Oct 2002 03:10:15 -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           Wed, 2 Oct 2002     Volume: 10 Number: 3899

Today's topics:
        Set many slots in a hash to the same defined value. (tî'pô)
    Re: Set many slots in a hash to the same defined value. (tî'pô)
    Re: Set many slots in a hash to the same defined value. <ubl@schaffhausen.de>
    Re: Set many slots in a hash to the same defined value. <Tassilo.Parseval@post.rwth-aachen.de>
    Re: Set many slots in a hash to the same defined value. <bigj@kamelfreund.de>
    Re: Sticky forms without CGI.pm? <pete@NOSPAMpopcornwebdesign.co.uk>
    Re: Sticky forms without CGI.pm? <ubl@schaffhausen.de>
    Re: Store as BLOB vs as file <nospam@nospam.com>
        Unable to trap error from child <tom.beer@btfinancialgroup.spamfilter.com>
    Re: Unable to trap error from child news@roaima.freeserve.co.uk
    Re: Use Java! Was: becoming a better programmer <lieven.marchand@just.fgov.be>
    Re: Which one is more efficient regarding constant scal <Tassilo.Parseval@post.rwth-aachen.de>
        why does where matter? <gm@magpage.com>
    Re: why does where matter? <nospam@nospam.com>
    Re: why does where matter? <ubl@schaffhausen.de>
        windows registry <stuff@nowhere.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 02 Oct 2002 11:13:56 +0300
From: "Teh (tî'pô)" <teh@mindless.com>
Subject: Set many slots in a hash to the same defined value.
Message-Id: <pp9lpuobru9dss9ks4teuetenqhqncoaud@4ax.com>

Say I want to set several key in a hash, if I just want them to exist
I can say:
my %hash;
@hash{qw/a b c/} = ();

but what if I want to set them to a specific value (1 for example),
the simplest way I can come up with is:
my %hash;
my @keys = qw/a b c/;
@hash{@keys} = split(/ /, "1 "x scalar @keys);

Is there a better way in terms of simplicity and/or efficiency?



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

Date: Wed, 02 Oct 2002 11:44:44 +0300
From: "Teh (tî'pô)" <teh@mindless.com>
Subject: Re: Set many slots in a hash to the same defined value.
Message-Id: <jiblpu4npnpgo3na7j30naj0c5n1j06rtu@4ax.com>

I (eye) bravely attempted to attach 13 electrodes of knowledge to the
nipples of comp.lang.perl.misc by saying:
>Say I want to set several key in a hash, if I just want them to exist
>I can say:
>my %hash;
>@hash{qw/a b c/} = ();
>
>but what if I want to set them to a specific value (1 for example),
>the simplest way I can come up with is:
>my %hash;
>my @keys = qw/a b c/;
>@hash{@keys} = split(/ /, "1 "x scalar @keys);
>
>Is there a better way in terms of simplicity and/or efficiency?

Actualy this is an over simplification. I want to the value to set to
be a reference to an object and the method I've listed doesn't work
for that. Here's a better example.

#!/bin/env perl -w
use strict;

package A; 
sub new { 
        my $self = bless {}, shift;
        $self->{arr} = [@_];
        return $self;
}

sub set {
        my ($self, $hash) = @_;
# This doesn't work with objects, they turn into strings
#        @$hash{@{$self->{arr}}} =  
#           split(/ /, "$self "x scalar @{$self->{arr}});

	# This does:
	my @vals;
	push(@vals, $self) for (0..$#{$self->{arr}});
	@$hash{@{$self->{arr}}} = @vals;

	# but its just as much work as doing
	# $$hash{$_} = $self foreach(@{$self->{arr}});
}
sub print {
        my $self = shift;
        print "A (",join(",",@{$self->{arr}}),")\n";
}

my $a = new A(qw/a b c/);
my $b = new A(qw/x y z/);
print "a = $a\nb = $b\n";

my %hash;
$a->set(\%hash);
$b->set(\%hash);

$hash{b}->print(); # print A (a,b,c)


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

Date: Wed, 02 Oct 2002 10:53:51 +0200
From: Malte Ubl <ubl@schaffhausen.de>
Subject: Re: Set many slots in a hash to the same defined value.
Message-Id: <anefi8$4o3$1@news.dtag.de>

Teh (tî'pô) wrote:
> I (eye) bravely attempted to attach 13 electrodes of knowledge to the
> nipples of comp.lang.perl.misc by saying:
> 
>>Say I want to set several key in a hash, if I just want them to exist
>>I can say:
>>my %hash;
>>@hash{qw/a b c/} = ();
>>
>>but what if I want to set them to a specific value (1 for example),
>>the simplest way I can come up with is:
>>my %hash;
>>my @keys = qw/a b c/;
>>@hash{@keys} = split(/ /, "1 "x scalar @keys);
>>
>>Is there a better way in terms of simplicity and/or efficiency?

Efficient? I don't know. Simple? maybe:

my %hash;
@hash{qw/a b c/} = map { "x" } 0..2;

print join ",", values %hash;


->malte



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

Date: 2 Oct 2002 09:13:10 GMT
From: "Tassilo v. Parseval" <Tassilo.Parseval@post.rwth-aachen.de>
Subject: Re: Set many slots in a hash to the same defined value.
Message-Id: <anedb6$10r$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Teh (tî'pô):

> Actualy this is an over simplification. I want to the value to set to
> be a reference to an object and the method I've listed doesn't work
> for that. Here's a better example.
> 
> #!/bin/env perl -w
> use strict;
> 
> package A; 
> sub new { 
>         my $self = bless {}, shift;
>         $self->{arr} = [@_];
>         return $self;
> }
> 
> sub set {
>         my ($self, $hash) = @_;
> # This doesn't work with objects, they turn into strings
> #        @$hash{@{$self->{arr}}} =  
> #           split(/ /, "$self "x scalar @{$self->{arr}});
> 
> 	# This does:
> 	my @vals;
> 	push(@vals, $self) for (0..$#{$self->{arr}});
> 	@$hash{@{$self->{arr}}} = @vals;
> 
> 	# but its just as much work as doing
> 	# $$hash{$_} = $self foreach(@{$self->{arr}});
> }

If I understood you correctly, you want to pass a hash-ref via an
instance method and initialize each key of this hash (keys are stored in
the attribute of the object) with the object? How about:

    sub set {
        my ($self, $hash) = @_;
        @$hash{ @{$self->{arr}} } = ($self) x keys @{$self->{arr}};
    }

Whenever you want to initialize a hash-slice with something other than
undef, this scheme will work:

    @hash{@values} = ($val) x @values;

[...]

Tassilo
-- 
$_=q!",}])(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;


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

Date: Wed, 02 Oct 2002 10:23:58 +0200
From: Janek Schleicher <bigj@kamelfreund.de>
Subject: Re: Set many slots in a hash to the same defined value.
Message-Id: <3d9abaa3$0$11472$afc38c87@auth.de.news.easynet.net>

Teh (tî'pô) wrote at Wed, 02 Oct 2002 10:13:56 +0200:

> Say I want to set several key in a hash, if I just want them to exist
> I can say:
> my %hash;
> @hash{qw/a b c/} = ();
> 
> but what if I want to set them to a specific value (1 for example),
> the simplest way I can come up with is:
> my %hash;
> my @keys = qw/a b c/;
> @hash{@keys} = split(/ /, "1 "x scalar @keys);
> 
> Is there a better way in terms of simplicity and/or efficiency?

I don't know whether they're better,
but here are two another ones:

$hash{$_} = 1 for qw/a b c/;

or

my %hash = map {$_ => 1} qw/a b c/;


Greetings,
Janek



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

Date: Wed, 2 Oct 2002 09:47:50 +0100
From: "Pete Smith" <pete@NOSPAMpopcornwebdesign.co.uk>
Subject: Re: Sticky forms without CGI.pm?
Message-Id: <anebrn$71u$1$8300dec7@news.demon.co.uk>

> > I loathe writing second-hand HMTL with CGI.pm but I need the sticky form
> > feature. Any ideas how to do it without CGI.pm? Do I need to access
> > $REQUEST_URI or $QUERY_STRING? Parse this with regex's?
>
> It looks like you have to parse the query string without the use of
CGI.pm.
> It's fairly simple, but you have to be careful with URL encoding
characters.

How about (off the top of my head, so please excuse any typos):


  use URI::Escape;
  use CGI;

  my $q = new CGI;

  my $inputs = '';
  foreach ($q->param()) {
    $inputs .= qq/<INPUT type="hidden" name="$_" value="/ .
uri_escape($q->param($_)) . qq/">\n/;
  }

  print $inputs;


Will work as long as you don't use multiple choice lists or file fields, I
think.

Pete




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

Date: Wed, 02 Oct 2002 11:20:35 +0200
From: Malte Ubl <ubl@schaffhausen.de>
Subject: Re: Sticky forms without CGI.pm?
Message-Id: <aneh4c$5ic$1@news.dtag.de>

Pete Smith wrote:
> How about (off the top of my head, so please excuse any typos):
> 
> 
>   use URI::Escape;
>   use CGI;
> 
>   my $q = new CGI;
> 
>   my $inputs = '';
>   foreach ($q->param()) {
>     $inputs .= qq/<INPUT type="hidden" name="$_" value="/ .
> uri_escape($q->param($_)) . qq/">\n/;
>   }
> 
>   print $inputs;
> 
> 
> Will work as long as you don't use multiple choice lists or file fields, I
> think.

No, unless that is really what you want, you shouldn't uri_escape the 
values of an input field. They should be in the encoding of the rest of 
the page.

->malte




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

Date: Tue, 1 Oct 2002 22:19:48 -0700
From: "Tan Nguyen" <nospam@nospam.com>
Subject: Re: Store as BLOB vs as file
Message-Id: <3d9a816e$1_2@nopics.sjc>


"ColdCathoid" <me@me.com> wrote in message

> This may be slightly off topic and I do apoligise. The code to handle what
I
> want to do is written in perl. So .. yeah .. onto my question.
>
> I wrote some cgi in perl that allows users of my website to upload images
to
> my server. What I am wondering is what would be the best method to store
the
> files. Currently I have it set up to have the files written to a folder
and
> have information relating to them stored in a db table.
>
> The other option is to have the images written to a mysql database as a
> binary file along with all other corresponding data.
>
> When I go to access them out later for viewing which is better? Does one
> perform better then another? Are there any issues with storing binaries in
a
> mysql db?
>
> My setup is  Apache 1.3.20, perl 5.005 w/ DBI support and MySQL

Well, this is apparently off-topic as you could have asked the same
questions to database/cgi/asp groups.

It's already hard to answer without knowing your loads, data size etc. but
I'll give it a try.  My gut tells me that you should store these images into
mysql rather than keep them as separate files. This is all for the benefits
of retrieving your data. With everything in the database, you save the time
of  doing extra IO by locating the image, openning and reading it based on
the info from the database. However, this is pure guessing so the performace
in reality might be drastically different.
I haven't heard any serious problems with storing binaries with mysql. I
used to store 50-60 Gb of data without any problems.




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

Date: Wed, 2 Oct 2002 17:08:09 +1000
From: "Tom Beer" <tom.beer@btfinancialgroup.spamfilter.com>
Subject: Unable to trap error from child
Message-Id: <ane60q$dra$1@merki.connect.com.au>

Hi all,

My script forks a number of children, then waits for them to complete. The
problem I am experiencing is that the error code returned after my wait call
($?) does not match the value I use in the exit statement for my child
process. eg. if I use

exit 256;

in the child then

wait; print "error code = $?\n"

Then I get the following output:

"error code = 0";

Does anyone know what could be going wrong? Am I misunderstanding the value
of '$?'? Note that I have several children running at once - is it possible
that they are overwriting the value in $??

Thanks in advance,

Tom.

A cut-down sample of my code is as follows (NB. Unfortunately I can't post
runnable code because of proprietary routines we are using).

    foreach $nextnameps (@inputfiles) {
        if ($childcount == $maxchildcount) {
            $childpid = wait;
            print "$childpid exited with q = $?; x = $!\n";
            --$childcount;
           }

        if (fork) {$childcount++;}
        else {
            $status = system("blahblah");

            if ($status == 0) {
                # blah blah cleanup
                exit 0;
            } else {
                system("blahblah");
                print "$$ exiting with status $status\n";
                exit $status;
            }
        }
    }

This gives output similar to:

32384 exiting with status 256
32384 exited with q = 0; x = A file or directory in the path name does not
exist.


--
------------------------------------------------------------
Tom Beer
BT Financial Group
[+61] 2 9259 2627
------------------------------------------------------------




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

Date: Wed, 2 Oct 2002 10:25:26 +0100
From: news@roaima.freeserve.co.uk
Subject: Re: Unable to trap error from child
Message-Id: <62eena.1f8.ln@moldev.cmagroup.co.uk>

Tom Beer <tom.beer@btfinancialgroup.spamfilter.com> wrote:
> My script forks a number of children, then waits for them to complete.

There's a lot of well written code "out there" that alread does this. Take
a look at perldoc perlipc for starters.

> The problem I am experiencing is that the error code returned after
> my wait call ($?) does not match the value I use in the exit statement
> for my child process. eg. if I use

>	exit 256;

You can't do that. Exit can only take an 8-bit value (i.e. 0..255). Larger
values wrap round, and since 256 & 0xff == 0, your child is effectively
exiting with status 0.

> A cut-down sample of my code is as follows (NB. Unfortunately I can't post
> runnable code because of proprietary routines we are using).

You should post a sample that illustrates the problem at hand. It
doesn't matter whether it's (slightly) different to your proprietary
code. (Note, I haven't tried your code - the fundamental problem is with
your understanding of exit and wait.)

	$pid = wait;
	$ss = $?;
	print "Wait gave exit status $ss for pid $pid\n";

However, in your example, you're taking the result of system("...") and
trying to use that as exit status. This is a different thing altogether.

Read perldoc -f system:

    The return value is the exit status of the program as returned by the
    "wait" call.  To get the actual exit value divide by 256.

> This gives output similar to:
> 32384 exiting with status 256

This message is from your system("") call and corresponds to status 0,
so your code then needs to be like this:

	$ss = system ("blahblah") >> 8;
	print "System gave exit status $ss for an unknown pid\n";

Chris
-- 
@s=split(//,"Je,\nhn ersloak rcet thuarP");$k=$l=@s;for(;$k;$k--){$i=($i+1)%$l
until$s[$i];$c=$s[$i];print$c;undef$s[$i];$i=($i+(ord$c))%$l}


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

Date: 02 Oct 2002 08:34:36 +0200
From: Lieven Marchand <lieven.marchand@just.fgov.be>
Subject: Re: Use Java! Was: becoming a better programmer
Message-Id: <861y79wdqb.fsf@just.fgov.be>

Donald Fisk <hibou00000nospam@enterprise.net> writes:

> (defun fac (n)
>   (collect-sum (scan-range :from 1 :upto n)))

> (NB I have not tested the third solution, only proven it.)

I'd suggest collect-product, or for maximum obfuscation 
(exp (collect-sum (scan-range :from 1 :upto n)))

-- 
Never argue with a fool in public. People might not see the difference.


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

Date: 2 Oct 2002 07:02:47 GMT
From: "Tassilo v. Parseval" <Tassilo.Parseval@post.rwth-aachen.de>
Subject: Re: Which one is more efficient regarding constant scalars?
Message-Id: <ane5mn$mtk$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Vespasian:

> Which of the following is more time/memory efficient or are they the
> same?
> 
> 1) 
> use constant PI => 3.14; 
> 
> or,
> 
> 2) 
> *PI = \3.14;

The difference is negligible. But then, those two statements are not
equivalent. The first creates an inlined subroutine:

    sub PI () { 3.14 }

while the second assigns 3.14 to the scalar slot of the typeglob *PI.
That -being a scalar - has the advantage of interpolating nicely in
strings:

    print "PI is $PI";

But it's not really a constant since it's value can be altered.

By the way, you could write the first also with a typeglob:

    BEGIN { *PI = sub () { 3.14 } }

That needs to be in a begin block because the prototype requires compile
time.

Tassilo
-- 
$_=q!",}])(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;


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

Date: 2 Oct 2002 05:30:25 GMT
From: "~greg" <gm@magpage.com>
Subject: why does where matter?
Message-Id: <ane09h$61j$0@216.155.33.35>


This subroutine pretty-prints the contents of 2-dim arrays. 
It's output is intended to replace a calculated array with a static definition.

 . 
example:  
   PrintArray( '$Test' );  

where $Test is a reference to a 2-dim array.

My question is:  why doesn't it work 
when the array reference is declared *after* the the sub definition?  
(see output below)


~greg

~~~~~~~~~~~~~~~~~~~~~~~


use strict;

my $Test1 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]];

sub PrintArray
{
  my $a = shift;
  my $a_ = eval $a;
  print "my $a=\n[\n",    join(  ",\n",  map { '  ['  . join( ',',  @{$_} ) . ']' }  @{$a_}  ),    "\n];\n\n" ;
}

my $Test2 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]];



PrintArray( '$Test1' );
PrintArray( '$Test2' );

--------------------------------output:
my $Test1=
[
  [1,2,3],
  [4,5,6],
  [7,8,9],
  [10,11,12]
];

my $Test2=
[

];










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

Date: Tue, 1 Oct 2002 23:00:22 -0700
From: "Tan Nguyen" <nospam@nospam.com>
Subject: Re: why does where matter?
Message-Id: <3d9a8af4_3@nopics.sjc>


"~greg" <gm@magpage.com> wrote

> where $Test is a reference to a 2-dim array.
>
> My question is:  why doesn't it work
> when the array reference is declared *after* the the sub definition?

Ah, it's the devil of forgetting to check for return value from eval $@.

You should read the docs for further info on eval. Basically, eval tries to
evaluate things in the current context. In your sub, eval parses the
expression "$Test2" and could never find it, hence, an error.




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

Date: Wed, 02 Oct 2002 09:20:22 +0200
From: Malte Ubl <ubl@schaffhausen.de>
Subject: Re: why does where matter?
Message-Id: <anea2v$ar$1@news.dtag.de>

~greg wrote:
> This subroutine pretty-prints the contents of 2-dim arrays. 
> It's output is intended to replace a calculated array with a static definition.
> 
> . 
> example:  
>    PrintArray( '$Test' );  

Why would you pass the variable by name? Leaving out the quotes and the 
weird eval stuff would make everything work perfectly fine. Eval is not 
intended to replace symbolic variables for lexicals. Use real references.

-> malte



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

Date: Wed, 2 Oct 2002 16:41:43 +1000
From: "mostuff" <stuff@nowhere.com>
Subject: windows registry
Message-Id: <Dywm9.844$U5.66779@nasal.pacific.net.au>

hi. i've tried using win32::registry and win32::tieregistry to get the value
of the HKEY_CLASSES_ROOT\htmlfile\shell\open\command\(Default) key but
nothing i seem to do will get the value of it. how do i get the value of it?

thanx in advance.




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

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.  

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


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