[18747] in Perl-Users-Digest
Perl-Users Digest, Issue: 915 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed May 16 18:05:57 2001
Date: Wed, 16 May 2001 15:05:23 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <990050723-v10-i915@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Wed, 16 May 2001 Volume: 10 Number: 915
Today's topics:
\nhelp <jtjohnston@courrier.usherb.ca>
ANNOUNCE: Java 3.1 <mark@zzo.com>
ANNOUNCE: Net::Netmask version 1.9 - lots of new stuff (David Muir Sharnoff)
Re: Bits and bytes (Dave Bailey)
Re: Executing CGI on a Mac, help? <jeremy.broadhead@tufts.edu>
General Progress of Perl 6 <RAY_electronic_design@t-online.de>
Re: General Progress of Perl 6 <bart.lateur@skynet.be>
HELP.....Need help with Perl Script <carsonmtv@MailAndNews.com>
Re: How to invoke another script in a CGI script? (Eric Bohlman)
Re: How to invoke another script in a CGI script? (Tad McClellan)
Re: importing var's help wanted nobull@mail.com
Re: importing var's help wanted (Tad McClellan)
Re: Initialisation of object nobull@mail.com
Re: Initialisation of object <uri@sysarch.com>
Re: Initialisation of object nobull@mail.com
Re: loosing contents of var <mirvine@esatclear.ie>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 16 May 2001 17:12:06 -0400
From: jtjohnston <jtjohnston@courrier.usherb.ca>
Subject: \nhelp
Message-Id: <3B02ED26.84971366@courrier.usherb.ca>
Hi,
I am parsing the contents of a text file.
I have certain lines that start with "&&4;" I would like to attach these
lines to the previous line as follows. But I don't know how. I figure I
have to search and replace for "\n&&4;" but ... ?
BEFORE:
Here is an example of before.
&&4; here is the second line.
AFTER:
Here is an example of after. &&4; here is the second line.
Thanks,
John
(An email post & reply would be helpful. :)
------------------------------
Date: Wed, 16 May 2001 14:56:19 -0700
From: <mark@zzo.com>
Subject: ANNOUNCE: Java 3.1
Message-Id: <tg5u4n6gh7j933@corp.supernews.com>
NAME
Java - Perl extension for accessing a JVM remotely or locally
SYNOPSIS
use Java;
$java = new Java;
$frame = $java->create_object("java.awt.Frame","Frame's Title");
$frame->setSize(400,400);
$frame->show();
DESCRIPTION
This module allows you to talk to a JVM on a local or remote
machine. You can create objects, call functions, access fields,
deal with arrays, get events & all the nonsense you can do in
Java - from Perl.
CHANGES in 3.1
Instantiated function names can now have '_'s safely in them.
See 'Changes' file for more info.
Only Java.pm has changed. The Java code has not changed from 3.0.
Java-3.1.tar.gz has been uploaded to CPAN & is also available at:
http://www.zzo.com/Java/getit.html
Enjoy,
Mark
------------------------------------------
Mark Ethan Trostler mark@zzo.com
Computing Solutions http://www.zzo.com
------------------------------------------
------------------------------
Date: 16 May 2001 01:34:21 -0700
From: muir@idiom.com (David Muir Sharnoff)
Subject: ANNOUNCE: Net::Netmask version 1.9 - lots of new stuff
Message-Id: <tg5t4pl5hnauf6@corp.supernews.com>
You can find this in CPAN at
http://www.cpan.org/authors/id/MUIR/modules/Net-Netmask-1.9.tar.gz
or (until CPAN picks it up) at
ftp:://ftp.idiom.com/users/muir/CPAN/modules/Net-Netmask-1.9.tar.gz
-Dave
...CHANGELOG...................................................
Added deleteNetblock() to match storeNetblock().
Carol Lerche <cafl@securify.com> contributed findOuterNetblock()
and findAllNetblocks().
Kevin Baker <kevin@apnic.net> sent in patches that suggested a new
handling of error conditions; extra error conditions to test for; and
a test script to exercise the error conditions.
Bruce Peikon <Bruce.Peikon@msdw.com> sent a contribution which suggested
that enumerate could do so by network.
Dominic Mitchell <dom@semantico.com> sent in code that suggested the
creation of cidrs2contiglists() and range2cidrlist().
A couple of documentation fixes from Igor Vinokurov <igor@rtsnet.ru>.
...POD..........................................................
NAME
Net::Netmask - parse, manipulate and lookup IP network blocks
SYNOPSIS
use Net::Netmask;
$block = new Net::Netmask (network block)
$block = new Net::Netmask (network block, netmask)
$block = new2 Net::Netmask (network block)
$block = new2 Net::Netmask (network block, netmask)
print $block->desc() # a.b.c.d/bits
print $block->base()
print $block->mask()
print $block->hostmask()
print $block->bits()
print $block->size()
print $block->maxblock()
print $block->broadcast()
print $block->next()
print $block->match($ip);
print $block->nth(1, [$bitstep]);
for $ip ($block->enumerate([$bitstep])) { }
for $zone ($block->inaddr()) { }
my $table = {};
$block->storeNetblock([$table])
$block->deleteNetblock([$table])
$block = findNetblock(ip, [$table])
$block = findOuterNetblock(ip, [$table])
@blocks = findAllNetblock(ip, [$table])
@blocks = range2cidrlist($beginip, $endip);
@listofblocks = cidrs2contiglists(@blocks);
DESCRIPTION
Net::Netmask parses and understands IPv4 CIDR blocks. It's built with an
object-oriented interface. Nearly all functions are methods that operate
on a Net::Netmask object.
There are methods that provide the nearly all bits of information about
a network block that you might want.
CONSTRUCTING
Net::Netmask objects are created with an IP address and optionally a
mask. There are many forms that are recognized:
'140.174.82.0/24' The preferred form.
'140.174.82.0:255.255.255.0'
'140.174.82.0', '255.255.255.0'
'140.174.82.0', '0xffffff00'
'140.174.82.0 - 140.174.82.255'
'140.174.82.4' A /32 block.
'140.174.82' Always a /24 block.
'140.174' Always a /16 block.
'140' Always a /8 block.
'140.174.82/24'
'140.174/16'
'default' 0.0.0.0/0 (the default route)
There are two constructor methods: `new' and `new2'. The difference is
that `new2' will return undef for invalid netmasks and `new' will return
a netmask object even if the constructor could not figure out what the
network block should be.
With `new', the error string can be found as $block->{'ERROR'}. With
`new2' the error can be found as Net::Netmask::errstr or
$Net::Netmask::error.
METHODS
base() Returns base address of the network block as a
string. Eg: 140.174.82.0. Base does not give an
indication of the size of the network block.
mask() Returns the netmask as a string. Eg: 255.255.255.0.
hostmask() Returns the host mask which is the oposite of the
netmask. Eg: 0.0.0.255.
bits() Returns the netmask as a number of bits in the
network portion of the address for this block.
Eg: 24.
size() Returns the number of IP addresses in a block. Eg:
256.
broadcast() The blocks broadcast address. (The last IP address
inside the block.) Eg: 192.168.1.0/24 =>
192.168.1.255
next() The first IP address following the block. (The IP
address following the broadcase address.) Eg:
192.168.1.0/24 => 192.168.2.0
match($ip) Returns a true if the IP number $ip matches the
given network. That is, a true value is
returned if $ip is between base() amd
broadcast(). For example, if we have the
network 192.168.1.0/24, then
192.168.0.255 => 0
192.168.1.0 => "0 "
192.168.1.1 => 1
...
192.168.1.255 => 255
$ip should be a dotted-quad (eg:
"192.168.66.3")
It just happens that the return value is the
position within the block. Since zero is a
legal position, the true string "0 " is
returned in it's place. "0 " is numerically
zero though. When wanting to know the position
inside the block, a good idiom is:
$pos = $block->match($ip) || die;
$pos += 0;
maxblock() Much of the time, it is not possible to determine
the size of a network block just from it's base
address. For example, with the network block
'140.174.82.0/27', if you only had the
'140.174.82.0' portion you wouldn't be able to
tell for certain the size of the block.
'140.174.82.0' could be anything from a '/23'
to a '/32'. The maxblock() method gives the
size of the larges block that the current
block's address would allow it to be. The size
is given in bits. Eg: 23.
enumerate([$bitstep) Returns a list of all the IP addresses in the
block. Be very careful not to use this function
of large blocks. The IP addresses are returned
as strings. Eg: '140.174.82.0', '140.174.82.1',
... '140.174.82.255'.
If the optional argument is given, step through
the block in increments of a given network
size. To step by 4, use a bitstep of 30 (as in
a /30 network).
nth($index, [$bitstep]) Returns the nth element of the array that enumerate
would return if it were called. So, to get the
first usable address in a block, use nth(1). To
get the broadcast address, use nth(-1). To get
the last usable adress, use nth(-2).
inaddr() Returns an inline list of tuples. There is a tuple
for each DNS zone name in the block. If the
block is smaller than a /24, then the zone of
the enclosing /24 is returned.
Each tuple contains: the DNS zone name, the
last component of the first IP address in the
block in that zone, the last component of the
last IP address in the block in that zone.
Examples: the list returned for the block
'140.174.82.0/23' would be: '82.174.140.in-
addr.arpa', 0, 255, '83.174.140.in-addr.arpa',
0, 255. The list returned for the block
'140.174.82.64/27' would be: '82.174.140.in-
addr.arpa', 64, 95.
storeNetblock([$t]) Adds the current block to an table of network
blocks. The table can be used to query which
network block a given IP address is in.
The optional argument allows there to be more
than one table. By default, an internal table
is used. If more than one table is needed, then
supply a reference to a HASH to store the data
in.
deleteNetblock([$t]) Deletes the current block from a table of network
blocks.
The optional argument allows there to be more
than one table. By default, an internal table
is used. If more than one table is needed, then
supply a reference to a HASH to store the data
in.
FUNCTIONS
findNetblock(ip, [$t]) Search the table of network blocks (created with
storeNetBlock) to find if any of them contain
the given IP address. The IP address is
expected to be a string. If more than one block
in the table contains the IP address, the
smallest network block will be the one
returned.
The return value is either a Net::Netmask
object or undef.
findOuterNetblock(ip, [$t])
Search the table of network blocks (created
with storeNetBlock) to find if any of them
contain the given IP address. The IP address is
expected to be a string. If more than one block
in the table contains the IP address, the
largest network block will be the one returned.
The return value is either a Net::Netmask
object or undef.
findAllNetblock(ip, [$t])
Search the table of network blocks (created
with storeNetBlock) to find if any of them
contain the given IP address. The IP address is
expected to be a string. All network blocks in
the table that contain the IP address will be
returned.
The return value is a list of Net::Netmask
objects.
range2cidrlist($startip, $endip)
Given a range of IP addresses, return a list of
blocks that span that range.
For example, range2cidrlist('216.240.32.128',
'216.240.36.127'), will return a list of
Net::Netmask objects that corrospond to:
216.240.32.128/25
216.240.33.0/24
216.240.34.0/23
216.240.36.0/25
cidrs2contiglists(@listOfBlocks)
`cidrs2contiglists' will rearange a list of
Net::Netmask objects such that contigueous sets
are in sublists and each sublist is
discontigeous with the next.
For example, given a list of Net::Netmask
objects corrosponding to the following blocks:
216.240.32.128/25
216.240.33.0/24
216.240.36.0/25
`cidrs2contiglists' will return a list with two
sublists:
216.240.32.128/25 216.240.33.0/24
216.240.36.0/25
The behavior for overlapping blocks is not
currently defined.
COPYRIGHT
Copyright (C) 1998, 2001 David Muir Sharnoff. All rights reserved.
License hereby granted for anyone to use this module at their own risk.
Please feed useful changes back to muir@idiom.com.
--
--
Notice: Your mouse has been moved. Windows will now restart so this
change can take effect.
------------------------------
Date: 16 May 2001 20:50:43 GMT
From: dave@sydney.daveb.net (Dave Bailey)
Subject: Re: Bits and bytes
Message-Id: <slrn9g5fpc.qf.dave@sydney.daveb.net>
On Wed, 16 May 2001 16:02:58 GMT, James Weisberg <chadbour@wwa.com> wrote:
>Dave Bailey <dave@sydney.daveb.net> wrote:
>>On Tue, 15 May 2001 23:11:53 GMT, James Weisberg <chadbour@wwa.com> wrote:
[...]
>>>sub decode {
>>> my @s = reverse split //, unpack("B32", shift); # portable? fast?
>>> $" = ""; print "[$#s] bitstr: (@s)\n";
>>>
>>> my($i, $j, $v, $p);
>>> for( $i = $j = $v = 0, $p = 1; $j < $#s; $j++, $p <<= 1 ) {
>>> $v|= $p if($s[$i++]); # fiddle 2^p
>>> }
>>>
>>> return $v;
>>>}
>>
>>You don't need this function. Replace it with vec($vector,0,32) in
>>your example.
>
> Right, but I need a decoding function for the bitstring I am
>constructing. The above was just an example of how that might work
>once I know how many bits I need to sift thru to calculate a value.
I see. You should still be able to use vec instead of split and unpack,
but I'm not sure how well the methods compare.
[encoding instructions snipped for brevity]
> So you see, I may have an arbitrarily long bitstring, encoding some
>number of elements, and now I need to walk down that string and figure
>out the values. [...] Really want I want to know is if I have to use
>split() or substr() to walk down the bitstring to see which 0's and 1's
>are set. [...]
You can do it without using those functions, yes.
> The problem with the fetch function, in terms of how I need to use it,
>is that the caller must know a priori how many bits need fetching (for $b).
But in your scheme, that can determined by the result of a previous
vec_fetch, right? Of course, vec_store and vec_fetch would have to be
modified to operate 2 bits at a time. This being the case, perhaps the
best solution would be to implement the encoding and decoding in C, then
glue it to Perl using XS.
>My scheme above encodes 2, 4, 8, 16, and 24. The storage function is called
>to store data to disk; and then the fetch function is used by a completely
>independent program to retreive the data. So the size of the data element
>must be encoded in the bitstring itself. What I have achieved above is
>some pretty good compression, at the expense of some processing. Now I
>just want to see if I can process a bit faster. If you are interested, I
>can post both pieces of code and explain how they work.
Sure. Perhaps you could benchmark your encoding and decoding functions on
the following arrays, as well:
map { 0 } (1..10000); # test speed of 2-bit encodings (0 or -1)
map { 12 } (1..10000); # test speed of 4-bit encodings (1-16)
map { 123 } (1..10000); # test speed of 8-bit (17-272)
map { 12345 } (1..10000); # test speed of 16-bit (273-65808)
map { 1234567 } (1..10000); # test speed of 24-bit (65809-16843024)
This might help us figure out if there are ways to optimize the code.
--
Dave Bailey
davidb54@yahoo.com
------------------------------
Date: Wed, 16 May 2001 20:26:54 GMT
From: Jeremy Broadhead <jeremy.broadhead@tufts.edu>
Subject: Re: Executing CGI on a Mac, help?
Message-Id: <3B02E364.BFE55B49@tufts.edu>
Ken,
I'm also trying to use MacPerl 5.2.0r4 to interpret Perl CGI scripts on
a Mac running vqServer. I can't even get the server to communicate with
MacPerl. What is this AppleEvent CGI application that you speak of? Is
it built into WebSTAR?
Jeremy
Kenneth Knight wrote:
>
> In article <butlerl-ya02408000R0705012131020001@news.attcanada.ca>,
> butlerl@attcanada.ca (L. Butler) wrote:
>
> > In article <krk-7D1222.11100607052001@news.speakeasy.net>, Kenneth Knight
> > <krk@speakeasy.org> wrote:
> >
> > Ken,
> >
> > What web server is the other mac running. If it is Personal web sharing,
> > you might want to set it so that the web sharing will launch MacPerl when
> > the ACGI is called. It kind of sounds like you have just the perl file
> > saved as text. I think on the Mac you need to save it as an applet, and
> > have you web server set-up to open MacPerl when the request for the cgi
> > comes in. I have done it on tests at the office, but I can't remember
> > right off hand where the configuration is for that under the web sharing
> > extension.
>
> I thought I had included that information. My mistake. The server where
> this perl cgi is to run is using WebSTAR (don't know which version). I
> am uploading the script as a CGI application as you are supposed to do
> with MacPerl. You're right the cgi application sends appleevetns to
> MacPerl which runs the actual code.
>
> ** Ken **
>
> --
> ** Kenneth Knight Web Design, IT Consultant, Software Engineer **
> ** krk@speakeasy.org http://www.speakeasy.org/~krk **
--
Jeremy Broadhead
Frontline Service Provider
Tufts University School of Medicine
Departments of Microbiology & Physiology
"It was a fearsomely archaic place, and had begun to exude the faint
miasmal odour which clings about houses that have stood too long." -
H.P. Lovecraft
------------------------------
Date: Wed, 16 May 2001 20:11:20 +0100
From: "Raymund Hofmann" <RAY_electronic_design@t-online.de>
Subject: General Progress of Perl 6
Message-Id: <9dui9l$9ja$06$1@news.t-online.com>
As i use Perl (5.6) quite heavily now for different jobs as:
- Controlling / initializing external hardware via RS232 (Production).
- Reading / Processing EDIF netlists and glueing everything together to get
to my PCB layout software
- Design rule checks on netlists
- Synchronizing directories on different machines
- Coordinate part Inventory handling
I am quite interested in what Perl 6 will possibly bring for me. There are
some things (I come from C/C++) I am thinking could be useful to have them
included. I have read some information spreaded over the internet about Perl
6 (RFC's etc.) so I assume that many wishes i have may be addressed in Perl
6.
Especially to be able to "design" data structures which are more strictly
checked when using them could be helpful on larger programs handling more
complicated data.
I guess Perl 6 will not be useable by me earlier than in a year or so.
But still i am curious about Perl 6 and would like someone to give summaries
of the important things coming with Perl 6.
Raymund Hofmann
------------------------------
Date: Wed, 16 May 2001 20:03:44 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: General Progress of Perl 6
Message-Id: <r2n5gtk2g2f3t32h5vnvah7v51r9osrbm7@4ax.com>
Raymund Hofmann wrote:
>I guess Perl 6 will not be useable by me earlier than in a year or so.
I guess there won't be more than an alpha of Perl6 in a year or so.
>But still i am curious about Perl 6 and would like someone to give summaries
>of the important things coming with Perl 6.
Check out the first two "apocalypses" (= "revelations", more or less)
from Larry Wall.
<http://www.perl.com/pub/2001/04/02/wall.html>
<http://www.perl.com/pub/2001/05/03/wall.html>
That's about it for the moment, unless you count Damian Conway's
interpretation on how this would affect how one writes code in Perl6,
AKA the "exegesis".
<http://www.perl.com/pub/2001/05/08/exegesis2.html>
--
Bart.
------------------------------
Date: Wed, 16 May 2001 17:57:01 -0400
From: Steve Jones <carsonmtv@MailAndNews.com>
Subject: HELP.....Need help with Perl Script
Message-Id: <3B0391C4@MailAndNews.com>
I am trying to create yet another paid to read email program and am having
trouble with the script. What I am actually using is a preexisting click
affiliate script. That is, a program that users signup for, and are emailed
their code so they can put that code on a webpage of their own. They will
recieve credits in thier account, for unique ip address clicks. The problem
is that, the script logs their ip address, so as too not pay them for
clicking
the link on their own webpage. It will only pay other surfers who would
click
that button, banner, or link.
I need the script changed, so as to allow only the person that registers to
be
able to continue to click the link and recieve credit for it. The reason,
is
the member that signs up with this script will be emailed weekly with a link
in the email body crediting them for the click, and then sending them to the
advertisers website. The way the script is now, it will send them to the
advertiser, but since the ip it finds is the same as the ip that the member
used to sign up, it will not give them credit.
I have checked out a few books from the library, but am a far cry from
understanding what needs to be done. If you know perl like the back of your
hand, I would be very interested in hearing from you. The script could be
provided to you as long as you were interested in changing it. Perhaps we
could even arrive at a form of payment for services.
I would be interested in hearing from anyone, seriously interested in
helping
me with this matter. I would ask though, that you contact me by email, and
I
will respond asap. Of course I will be checking the Newsgroups as well, but
would be able to respond to you quicker by email. I can be reached at:
chevylonghorn@netzero.net
Thank You for your time,
R. Begley
------------------------------
Date: 16 May 2001 18:20:30 GMT
From: ebohlman@omsdev.com (Eric Bohlman)
Subject: Re: How to invoke another script in a CGI script?
Message-Id: <9dugde$ert$1@bob.news.rcn.net>
In comp.lang.perl.misc kalasend at YAHOO dot COM <dontuspamme@nospammers.com> wrote:
> hi,
> I found that in a CGI script, statements like these won't work:
> system("X Y Z") && die "$!\n";
> or,
> $all_files = `find .`;
> Problem could be that the CGI script is not associated with any shell?
> (I made this assumption because the same CGI script works well when I did
> "perl -w XXX.cgi" on the command line)
What's more likely is that you're simply not in the working directory you
think you are. Try doing an explicit chdir() to the directory you want to
be in. Remember, also, that your script isn't running as you; it's
running as whoever the server runs it as, generally a low-privileged user
like 'nobody', so it won't have the same access permissions as when you
run it yourself from the shell.
------------------------------
Date: Wed, 16 May 2001 13:46:02 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: How to invoke another script in a CGI script?
Message-Id: <slrn9g5f6q.nms.tadmc@tadmc26.august.net>
kalasend at YAHOO dot COM <dontuspamme@nospammers.com> wrote:
>hi,
>
> I found that in a CGI script, statements like these won't work:
^^^^^^^^^^
What, exactly, does that mean?
You have not given enough information for us to help you.
> system("X Y Z") && die "$!\n";
> or,
> $all_files = `find .`;
Those _could_ both work fine.
Do you see the die() output in the server logs?
Is the "X" program in the path for the CGI process?
Or do you use an absolute path to it in your real code?
(how about showing us your read code?)
Is the CGI process using the current working directory where you
want the find(1) to start?
> Problem could be that the CGI script is not associated with any shell?
No that cannot be it.
It is likely either the path that your CGI inherits, or the
current working directory that it inherits.
>(I made this assumption because the same CGI script works well when I did
>"perl -w XXX.cgi" on the command line)
Then it is somehow due to the difference in environment, which makes
it a CGI question rather than a Perl question.
> Then how can I invoke other scripts when running as CGI?
system or backticks should both work fine.
We need real code if we are to be able to provide more direct help.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 16 May 2001 20:07:12 +0100
From: nobull@mail.com
Subject: Re: importing var's help wanted
Message-Id: <u9r8xpun8f.fsf@wcl-l.bham.ac.uk>
Gary Pitman <gpitman@nbdot.net> writes:
> but i would like to also make these variables usable in this form, i
> tried several different syntax variations of:
>
> foreach $field (sort ($query->param))
> {
> foreach $value ($query->param($field))
> {
> ("\$"."$field"=$value);
> }
> }
The correct syntax for what I think you are trying to do is simply
$$field=$value
Do not do this it is extreemly dangerous.
See also the import_names() method in CGI.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Wed, 16 May 2001 15:59:25 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: importing var's help wanted
Message-Id: <slrn9g5n0t.nun.tadmc@tadmc26.august.net>
Gary Pitman <gpitman@nbdot.net> wrote:
>I have several .cgi pages that pass info from one form to the next,
>using some of the info on each page.
>I am trying to make a shortcut to assigning each imported name/value
>pair on each page. I can pass on the param hash with:
>
>foreach $field (sort ($query->param))
>{
> foreach $value ($query->param($field))
> {
> print $query->hidden("$field"," $value");
> }
>}
>
>but i would like to also make these variables usable in this form,
^^^^^^^^^^^^
I think you mean if
$field = 'date';
$value = 'May 16, 2001';
then you want the effect of
$date = 'May 16, 2001';
in the "receiving" program.
Is that right?
If so, then I advise that you revise what you want.
If you agree to put them in a hash instead, your code will be
very much more robust:
my %p; # "p" for "parameters"
$p{date} = 'May 16, 2001'; # $p{$field} = $value in the loop
Then instead of
print $date; # using evil symrefs
to access the field, you just do
print $p{date}; # no symrefs
>i
>tried several different syntax variations of:
> ("\$"."$field"=$value);
>Any pointers would be appreciated,
You appear to be wanting to use "Symbolic references".
Don't.
http://www.plover.com/~mjd/perl/varvarname.html
http://www.plover.com/~mjd/perl/varvarname2.html
http://www.plover.com/~mjd/perl/varvarname3.html
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 16 May 2001 20:24:04 +0100
From: nobull@mail.com
Subject: Re: Initialisation of object
Message-Id: <u9ofstumgb.fsf@wcl-l.bham.ac.uk>
"Michael Ströck" <michael@stroeck.com> writes:
> my $msg = new ForumMessage;
>
> $msg->{thread_id} = $cgi->param('thread_id');
> $msg->{user_id} = $cgi->param('user_id');
> However, I'd like to do something like:
> my $msg = new ForumMessage;
> $msg->{
> thread_id => $cgi->param('thread_id'),
> user_id => $cgi->param('user_id'),
> }
> But obviously that doesn't work. Can anybody tell
> me how to do it ?
The most direct way is:
%$msg = (
%$msg,
thread_id => scalar $cgi->param('thread_id'),
user_id => scalar $cgi->param('user_id')
);
This is ugly.
You can make it less ugly by creating a method like this:
$msg->set_attributes(
thread_id => scalar $cgi->param('thread_id'),
user_id => scalar $cgi->param('user_id')
);
# Where...
sub ForumMessage::set_attributes {
my $self=shift;
%$self = (%$self, @_);
}
Or even:
$msg->set_attributes_from_cgi($cgi => 'thread_id','user_id');
sub ForumMessage::set_attributes_from_cgi {
my $self=shift;
my $cgi=shift;
%$self = (%$self, map { $_ => scalar $cgi->param($_) } @_);
}
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Wed, 16 May 2001 19:49:38 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Initialisation of object
Message-Id: <x7ofst846k.fsf@home.sysarch.com>
>>>>> "n" == nobull <nobull@mail.com> writes:
n> This is ugly.
n> You can make it less ugly by creating a method like this:
n> $msg->set_attributes(
n> thread_id => scalar $cgi->param('thread_id'),
n> user_id => scalar $cgi->param('user_id')
n> );
n> # Where...
n> sub ForumMessage::set_attributes {
n> my $self=shift;
n> %$self = (%$self, @_);
n> }
why the copy of the hash?
while( my( $key, $val ) = splice( @_, 0, 2 ) ) {
$self->{$key} = $val ;
}
n> $msg->set_attributes_from_cgi($cgi => 'thread_id','user_id');
n> sub ForumMessage::set_attributes_from_cgi {
n> my $self=shift;
n> my $cgi=shift;
n> %$self = (%$self, map { $_ => scalar $cgi->param($_) } @_);
n> }
even simpler here:
$self->{$_} = $cgi->param($_) for @_ ;
and since you only have one cgi object, i would have not used the OO
style and just called param() directly.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Learn Advanced Object Oriented Perl from Damian Conway - Boston, July 10-11
Class and Registration info: http://www.sysarch.com/perl/OOP_class.html
------------------------------
Date: 16 May 2001 21:35:17 +0100
From: nobull@mail.com
Subject: Re: Initialisation of object
Message-Id: <u9eltpuj5m.fsf@wcl-l.bham.ac.uk>
Uri Guttman <uri@sysarch.com> writes:
> >>>>> "n" == nobull <nobull@mail.com> writes:
> n> # Where...
> n> sub ForumMessage::set_attributes {
> n> my $self=shift;
> n> %$self = (%$self, @_);
> n> }
>
> why the copy of the hash?
>
> while( my( $key, $val ) = splice( @_, 0, 2 ) ) {
> $self->{$key} = $val ;
> }
Conversely, why introduce an unecessary explict loop for something
that be done more readably with a single list assignment?
Particulatly considering the list assignment is also faster. I'm
guessing the OP's class is unlikley to have a lot of big elements so
tidal memory usage is not a real issue.
use Benchmark;
my @list1 = map { rand 100000 } 1..100;
my @list2 = map { rand 100000 } 1..100;
timethese(5000, {
loop => sub {
my $self = { @list1 };
@_ = @list2;
while( my( $key, $val ) = splice( @_, 0, 2 ) ) {
$self->{$key} = $val ;
}
},
copy => sub {
my $self = { @list1 };
@_ = @list2;
%$self = ( %$self, @_);
}
});
__END__
Benchmark: timing 5000 iterations of copy, loop...
copy: 21 wallclock secs (19.69 usr + 0.18 sys = 19.87 CPU) @ 251.64/s (n=5000)
loop: 25 wallclock secs (21.09 usr + 0.60 sys = 21.69 CPU) @ 230.52/s (n=5000)
> n> sub ForumMessage::set_attributes_from_cgi {
> n> my $self=shift;
> n> my $cgi=shift;
> n> %$self = (%$self, map { $_ => scalar $cgi->param($_) } @_);
> n> }
>
> $self->{$_} = $cgi->param($_) for @_ ;
Agreed. That _is_ simpler (I've not bothered to benchmark it but I
doubt there's much in it).
> and since you only have one cgi object,
There is no evidence of how many CGI objects the OP had. He
used OO style I saw no reason to change it.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Wed, 16 May 2001 19:19:49 GMT
From: "Mark Irvine" <mirvine@esatclear.ie>
Subject: Re: loosing contents of var
Message-Id: <ppAM6.32804$PF4.62969@news.iol.ie>
Hi,
In article <u9itj1xypv.fsf@wcl-l.bham.ac.uk>, nobull@mail.com wrote:
> mirvine@compsoc.com writes:
>
>> my $sth = $dbh->prepare($query) or die "$!.\nQUERY=$query\n";
>
> The DBI prepare() method does not return its error status in $!.
>
> To find out what is wrong with the SQL in $query look at
> $dbh->errstr().
>
Thankyou for your response, but, there is nothing wrong with the sql
syntax, other than the fact that there is no list of items in the
brackets between '...WHERE TESTERNO IN()...'.
at the first die statment it contains:
...WHERE TESTERNO IN('M1E510','M1F518','M1F519')
but at the $sth statment in contains nothing?
...WHERE TESTERNO IN()
My question really is why is there a difference here?
After posting earlier today, I finally got the script to run by changing:
----8<-----------------
my @with_quotes;
foreach my $tester (@testernos){
push(@with_quotes,"'".$tester."'");
}
my $testernos = join ",",@with_quotes;
my $query = qq`SELECT sum(pdmdt_f),sum(mdt_f) FROM $table WHERE
TESTERNO IN ($testernos)
...
-------8<---------------
To:
-------8<--------------
my @with_quotes;
foreach my $tester (@testernos){
push(@with_quotes,$tester); #don't add single quotes here
}
my $testernos = join "','",@with_quotes; #add single quotes here
#put $testernos between single quotes, gives same end result
#i.e., a list in single quotes, seperated by commas
my $query = qq`SELECT sum(pdmdt_f),sum(mdt_f) FROM $table WHERE
TESTERNO IN ('$testernos')
...
------8<-----------------
Regardless of the DBI error, do you not find it strange that the
$testerno variable seems to lose it's value? Is the error with perl, or
the dbd-db2 module? (I really don't see where the error in my code is
since it does run with the slight modification shown above).
Thanks again for your response, I'll make use of the $dbh->errstr in
future.
Cheers,
Mark Irvine
------------------------------
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 915
**************************************