[19049] in Perl-Users-Digest
Perl-Users Digest, Issue: 1244 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jul 4 14:06:10 2001
Date: Wed, 4 Jul 2001 11:05:10 -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: <994269910-v10-i1244@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Wed, 4 Jul 2001 Volume: 10 Number: 1244
Today's topics:
[Q] use, require, and BEGIN <kj0@mailcity.com>
Re: [Q] use, require, and BEGIN <pne-news-20010704@newton.digitalspace.net>
Re: auto installer <somewhere@in.paradise.net>
changing hash dynamically <ub98aa@brocku.ca>
Re: changing hash dynamically <pne-news-20010704@newton.digitalspace.net>
Re: Chmod Problem (Tad McClellan)
clean up array from "holes"... (Alexvalara)
Re: clean up array from "holes"... <tom.melly@ccl.com>
Re: clean up array from "holes"... (Anno Siegel)
Re: clean up array from "holes"... <uri@sysarch.com>
Re: clean up array from "holes"... ctcgag@hotmail.com
Re: clean up array from "holes"... (Anno Siegel)
Re: copy array of pointers, like malloc. <ren@tivoli.com>
flash and perl <Deneb.Pettersson@lmf.ericsson.se>
Re: flash and perl <somewhere@in.paradise.net>
Re: flash and perl <ubl@schaffhausen.de>
Re: How can I generate unique number combinations? <goldbb2@earthlink.net>
Re: Is REG_DATE a special type of mysql column? ctcgag@hotmail.com
Re: Net::Telnet logging on problem..... <mbudash@sonic.net>
Re: Net::Telnet logging on problem..... <r1ckey@home.com>
Re: Odd scalar equality problem (Vassilis Rigas)
Re: Perl on AS/400 <goldbb2@earthlink.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 4 Jul 2001 13:25:52 -0400
From: kj0 <kj0@mailcity.com>
Subject: [Q] use, require, and BEGIN
Message-Id: <9hvjj0$esp$1@panix3.panix.com>
Please, I would appreciate some help with Perl concepts here.
I am confused on the use of "use" and "require" statements within
BEGIN blocks, particularly in the context of programming for
inheritance. Can someone give me a general/typical example of the
BEGIN blocks for two classes A and B, such that B is a subclass of A,
showing the kinds of "use" and "require" commands one would typical
use?
Also, is there any reason to include use/require commands outside of a
BEGIN block?
Thanks!
KJ
------------------------------
Date: Wed, 04 Jul 2001 20:07:16 +0200
From: Philip Newton <pne-news-20010704@newton.digitalspace.net>
Subject: Re: [Q] use, require, and BEGIN
Message-Id: <fdm6ktghnjsb47vnf68iiv6efrqq2sbmi5@4ax.com>
On 4 Jul 2001 13:25:52 -0400, kj0 <kj0@mailcity.com> wrote:
> Can someone give me a general/typical example of the
> BEGIN blocks for two classes A and B, such that B is a subclass of A,
> showing the kinds of "use" and "require" commands one would typical
> use?
Something like this, maybe?
=== A.pm
package A;
use strict;
require Exporter;
our @ISA = qw(Exporter);
our $VERSION = '1.00';
=== B.pm
package B;
use strict;
use A;
our @ISA = qw(A);
our $VERSION = '3.14';
> Also, is there any reason to include use/require commands outside of a
> BEGIN block?
Yes. Especially use tends to be used outside of a BEGIN block, since it
carries its own little BEGIN block around itself; see `perldoc -f use`.
Briefly, "use Foo;" is the same as "BEGIN { require Foo; import Foo; }".
As, for example, with the 'use strict;' that is often recommended.
Cheers,
Philip
--
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.
------------------------------
Date: Wed, 4 Jul 2001 23:40:17 +1000
From: "Tintin" <somewhere@in.paradise.net>
Subject: Re: auto installer
Message-Id: <f2F07.30$Fk6.565369@news.interact.net.au>
"Reuben Logsdon" <rlogsdon@io.com> wrote in message
news:Pine.LNX.4.33.0107040242210.8117-100000@fnord.io.com...
> On Wed, 4 Jul 2001, Tintin wrote:
>
> > I'm looking for ideas/suggestions on writing a auto installer Perl CGI
> > script.
> >
> > My idea is to have an install script which a user puts on their server,
> > which when run, presents a form with various directory settings,
defaults
> > etc. and then retrieves the required files from my website.
> >
> > Obviously using the LWP modules would make life easier, but I know a lot
of
> > sites don't have it installed, so I either need to wrap a portion of the
> > module into the script or use a very basic socket connection.
> >
> > I assume this sort of function has been done many times before, so I'd
just
> > like some feedback on the pitfalls or otherwise of doing this.
> Hi Tintin,
>
> Many web hosting companies forbid sockets privileges for CGI scripts
> (almost all free web hosting companies are like this) and so it might be
> best to give your customers an install.pl and a data.tar file, with no
> network dependencies, rather than risk putting sockets code into the
> install.pl.
>
> Also, for this to work, your customers will have to install at least one
> script successfully (detect/set path to perl, detect/set extension,
> upload in ascii, set permissions) and will then have to create an
> extrator folder and set writable permissions on that. Many customers will
> fail along the way. Also, many web sites run CGI Wrap by default which
> will cause any script to fail if it's in a writable folder or if it's not
> owned by the site owner, so you have to be extra careful about your
> extraction process and instructions to the customer.
>
> Most CGI vendors I have seen use a server-side FTP bot to perform installs
> to other servers. Those bots can work around most user errors. There is
> one at nickname.net that I helped write. It is limited in that some
> customers don't trust it with username/pass and it can't see intranet
> sites, but otherwise it is very helpful for inexperienced customers.
Yes, I'm beginning to think that this is the easiest way to go. That way I
can control what modules I need to use.
------------------------------
Date: Wed, 04 Jul 2001 12:36:28 -0400
From: Umair Tariq Bajwa <ub98aa@brocku.ca>
Subject: changing hash dynamically
Message-Id: <3B43460C.F745C126@brocku.ca>
--------------2894705D0F9FBF3E15362520
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
I am trying to store information in seperate hashes and I would like to
change the name of the hash within the for loop. Like that.
my (%info1,%info2,%info3,%info4);
$counter = 0;
foreach $parameter (@paramlist)
{
if ($parameter =~ m/employee_id$counter/)
{
$counter = $counter + 1;
$value = param($parameter);
$info$counter{$parameter} = $value;
}
else
{
$value = param($parameter);
$info$counter{$parameter} = $value;
}
}
does anyone know how can i change the hash within the for loop. i know
the way i am doing is wrong. Any idea? Thanks in advance.
Umair
--------------2894705D0F9FBF3E15362520
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
I am trying to store information in seperate hashes and I would like to
change the name of the hash within the for loop. Like that.
<p>my (%info1,%info2,%info3,%info4);
<p>$counter = 0;
<p>foreach $parameter (@paramlist)
<br>{
<br> if ($parameter =~ m/employee_id$counter/)
<br> {
<br> $counter = $counter
+ 1;
<br> $value
= param($parameter);
<br> <b><i><u>$info$counter</u></i></b>{$parameter}
= $value;
<br> }
<br> else
<br> {
<br> $value
= param($parameter);
<br> <b><i><u>$info$counter</u></i></b>{$parameter}
= $value;
<br> }
<br>}
<p>does anyone know how can i change the hash within the for loop. i know
the way i am doing is wrong. Any idea? Thanks in advance.
<p>Umair</html>
--------------2894705D0F9FBF3E15362520--
------------------------------
Date: Wed, 04 Jul 2001 20:07:14 +0200
From: Philip Newton <pne-news-20010704@newton.digitalspace.net>
Subject: Re: changing hash dynamically
Message-Id: <l5m6kt8iugieltckm96oao34bq5t9bs3u0@4ax.com>
On Wed, 04 Jul 2001 12:36:28 -0400, Umair Tariq Bajwa <ub98aa@brocku.ca>
wrote:
> I am trying to store information in seperate hashes and I would like to
> change the name of the hash within the for loop. Like that.
You can do that, but it's generally considered not a good idea. Whenever
you have variables names with "counting numbers" in them, you probably
want another data structure instead. In this case, you might want an
array of hashes instead, since your indexes appear to be small
non-negative numbers.
> my (%info1,%info2,%info3,%info4);
>
> $counter = 0;
>
> foreach $parameter (@paramlist)
> {
> if ($parameter =~ m/employee_id$counter/)
> {
> $counter = $counter + 1;
> $value = param($parameter);
> $info$counter{$parameter} = $value;
> }
> else
> {
> $value = param($parameter);
> $info$counter{$parameter} = $value;
> }
> }
my(@info);
my $counter = 0;
foreach my $parameter (@paramlist)
{
if ($parameter =~ /employee_id$counter/)
{
$counter++;
}
$info[$counter]{$parameter} = param($parameter);
}
> does anyone know how can i change the hash within the for loop.
Yes, but I'm not telling. (It's called "symbolic references".) Use
proper references instead (as the above example does, transparently,
through the autovivification of @info's elements into hashrefs).
Cheers,
Philip
--
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.
------------------------------
Date: Wed, 4 Jul 2001 08:23:21 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Chmod Problem
Message-Id: <slrn9k62lo.cuv.tadmc@tadmc26.august.net>
BUCK NAKED1 <dennis100@webtv.net> wrote:
>I mean, did I make a mistake in my coding below?
Yes. You failed to check the return value from the chmod() function.
You should check the return value from the chmod() function.
Please check the return value from chmod() before asking further
questions about your chmod() problem.
chmod 0644, $_ or die "could not chmod($_) $!";
>I used ...
Please fix the profoundly horrid formatting if you want live
people to read and understand your code.
I skipped attempting to read your original post for that very reason.
Format your code sensibly, and more folks will pay attention when
you ask questions about the code.
># chmod all files to 644
>use File::Find;
>find sub {-f or return;
>if(my $new = $_)
Why do you have the if() test? What purpose did you have in
mind when you wrote it?
Q: When will the if() condition be false?
A: only if you happen to have a file named '0' (without quotes)
Do you want to skip chmod()ing the zero file for some reason?
If so, then this would be more clear:
return if $_ eq '0';
>{ chmod 0644, $_; rename $_, $new; } }, $tmpdir ;
Why do you rename() it to exactly the same name?
You have copied $new from $_ and never modified it, both variables
contain the identical value.
You should check the return value from rename() too:
rename $_, $new or die "could not rename '$_' to '$new' $!";
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 04 Jul 2001 16:10:10 GMT
From: alexvalara@aol.com (Alexvalara)
Subject: clean up array from "holes"...
Message-Id: <20010704121010.02822.00000077@ng-bg1.aol.com>
Hi everyone,
Is it possible to get rid off "holes" in an array...
for example: i have something that looks like
($ref1,$ref2,"",$ref3,"",$ref4,"","") and i want to transform it into
($ref1,$ref2,$ref3,$ref4).
Thank you in advance.
Alexandros
------------------------------
Date: Wed, 4 Jul 2001 17:41:04 +0100
From: "Tom Melly" <tom.melly@ccl.com>
Subject: Re: clean up array from "holes"...
Message-Id: <3b434720$0$3759$ed9e5944@reading.news.pipex.net>
"Alexvalara" <alexvalara@aol.com> wrote in message
news:20010704121010.02822.00000077@ng-bg1.aol.com...
> Hi everyone,
> Is it possible to get rid off "holes" in an array...
> for example: i have something that looks like
> ($ref1,$ref2,"",$ref3,"",$ref4,"","") and i want to transform it into
> ($ref1,$ref2,$ref3,$ref4).
>
How about:
@array = ("", "hello", "", "goodbye");
foreach(@array){
push @array2, $_ if $_;
}
@array = @array2;
You could avoid the need for the second array by using splice.
------------------------------
Date: 4 Jul 2001 16:56:18 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: clean up array from "holes"...
Message-Id: <9hvhri$196$1@mamenchi.zrz.TU-Berlin.DE>
According to Tom Melly <tom.melly@ccl.com>:
> "Alexvalara" <alexvalara@aol.com> wrote in message
> news:20010704121010.02822.00000077@ng-bg1.aol.com...
> > Hi everyone,
> > Is it possible to get rid off "holes" in an array...
> > for example: i have something that looks like
> > ($ref1,$ref2,"",$ref3,"",$ref4,"","") and i want to transform it into
> > ($ref1,$ref2,$ref3,$ref4).
> >
>
> How about:
>
> @array = ("", "hello", "", "goodbye");
> foreach(@array){
> push @array2, $_ if $_;
> }
> @array = @array2;
This is what the grep function is for:
@array = grep length, @array;
Anno
------------------------------
Date: Wed, 04 Jul 2001 16:57:38 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: clean up array from "holes"...
Message-Id: <x7k81o6399.fsf@home.sysarch.com>
>>>>> "TM" == Tom Melly <tom.melly@ccl.com> writes:
TM> @array = ("", "hello", "", "goodbye");
TM> foreach(@array){
TM> push @array2, $_ if $_;
TM> }
TM> @array = @array2;
that is what grep is for:
@array = grep $_, @array ;
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: 04 Jul 2001 17:04:12 GMT
From: ctcgag@hotmail.com
Subject: Re: clean up array from "holes"...
Message-Id: <20010704130412.503$rg@newsreader.com>
alexvalara@aol.com (Alexvalara) wrote:
> Hi everyone,
> Is it possible to get rid off "holes" in an array...
> for example: i have something that looks like
> ($ref1,$ref2,"",$ref3,"",$ref4,"","") and i want to transform it into
> ($ref1,$ref2,$ref3,$ref4).
>
> Thank you in advance.
I'll take my first crack at using grep in perl and suggest this:
@a = ($ref1,$ref2,"",$ref3,"",$ref4,"","") ;
@a = grep $_ , @a;
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service
------------------------------
Date: 4 Jul 2001 17:08:37 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: clean up array from "holes"...
Message-Id: <9hviil$196$2@mamenchi.zrz.TU-Berlin.DE>
According to Uri Guttman <uri@sysarch.com>:
> >>>>> "TM" == Tom Melly <tom.melly@ccl.com> writes:
>
> TM> @array = ("", "hello", "", "goodbye");
> TM> foreach(@array){
> TM> push @array2, $_ if $_;
> TM> }
> TM> @array = @array2;
>
> that is what grep is for:
>
> @array = grep $_, @array ;
That fails to convey "0".
Anno
------------------------------
Date: 04 Jul 2001 00:30:49 -0500
From: Ren Maddox <ren@tivoli.com>
Subject: Re: copy array of pointers, like malloc.
Message-Id: <m3ofr1tg52.fsf@dhcp9-173.support.tivoli.com>
On 04 Jul 2001, alexvalara@aol.com wrote:
> Hi everyone, I would like to copy an array of pointers but i don's
> want to copy the pointers! i need to make a clone of an array but
> having pointers with diferrent addresses...
It's not quite clear, but perhaps you want:
$ perldoc -q copy
Found in /usr/lib/perl5/5.6.0/pod/perlfaq4.pod
How do I print out or copy a recursive data structure?
--
Ren Maddox
ren@tivoli.com
------------------------------
Date: Wed, 04 Jul 2001 16:23:22 +0300
From: Deneb Pettersson <Deneb.Pettersson@lmf.ericsson.se>
Subject: flash and perl
Message-Id: <3B4318C9.84F657F3@lmf.ericsson.se>
hi there, i'm doing a perl script atm which makes up a text form given
variables, and at some point i will also have a picture catalogue with
pictures corresponding to the different options, so that once you have
chosen and press enter you will get a slideshop with your options....
this is nice, but then i jsut realised, that if it is possible to do
this into a flash then it would be alot cooler....
anyway, this is still under production, and thus i have no code to post,
i'm more intrested in teh general idea if it is possible to do flash or
even shockwave from perl???
------------------------------
Date: Wed, 4 Jul 2001 23:49:06 +1000
From: "Tintin" <somewhere@in.paradise.net>
Subject: Re: flash and perl
Message-Id: <waF07.31$Ml6.603272@news.interact.net.au>
"Deneb Pettersson" <Deneb.Pettersson@lmf.ericsson.se> wrote in message
news:3B4318C9.84F657F3@lmf.ericsson.se...
> hi there, i'm doing a perl script atm which makes up a text form given
> variables, and at some point i will also have a picture catalogue with
> pictures corresponding to the different options, so that once you have
> chosen and press enter you will get a slideshop with your options....
> this is nice, but then i jsut realised, that if it is possible to do
> this into a flash then it would be alot cooler....
>
>
> anyway, this is still under production, and thus i have no code to post,
> i'm more intrested in teh general idea if it is possible to do flash or
> even shockwave from perl???
Depends what you mean. Are you talking about writing a Perl script to
generate actual shockwave files?
------------------------------
Date: Wed, 04 Jul 2001 16:29:57 +0100
From: Malte Ubl <ubl@schaffhausen.de>
Subject: Re: flash and perl
Message-Id: <3B433676.AE14DC3F@schaffhausen.de>
Deneb Pettersson schrieb:
>
> hi there, i'm doing a perl script atm which makes up a text form given
> variables, and at some point i will also have a picture catalogue with
> pictures corresponding to the different options, so that once you have
> chosen and press enter you will get a slideshop with your options....
> this is nice, but then i jsut realised, that if it is possible to do
> this into a flash then it would be alot cooler....
Take a look at Flash Generator. Then when you get so far that you ask
yourself which data source to use, take a look at Perl again.
Bye,
->malte
------------------------------
Date: Wed, 04 Jul 2001 10:33:51 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: How can I generate unique number combinations?
Message-Id: <3B43294F.5A2A1820@earthlink.net>
Chris Spurgeon wrote:
>
> Given a series of digits, I want to generate all of the different ways
> they can be arranged. So, for example, for the numbers 1, 2, 3, 4 and
> 5 I would generate output like this...
>
> 12345
> 12354
> 12435
>
> ...snip...
>
> 54231
> 54312
> 54321
use Algorithm::Permute; # from cpan
my $p = Algorithm::Permute->new [1..5];
while( my @a = $p->next ) {
print @a, "\n";
}
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: 04 Jul 2001 16:53:58 GMT
From: ctcgag@hotmail.com
Subject: Re: Is REG_DATE a special type of mysql column?
Message-Id: <20010704125358.225$LV@newsreader.com>
Diane Strahl <diane@dmswebsupport.com> wrote:
This is not a perl question.
> Can someone tell me if there's something special about using REG_DATE as
> a COLUMN name in a mysql database when updating the database from a perl
> script?
Apparently not.
> it is also updating the REG_DATE column. The REG_DATE column is type
> TIMESTAMP.
There is a reason for the "STAMP" in the name TIMESTAMP. It is doing
what it is supposed to do, stamping the time of last update to the record.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service
------------------------------
Date: Wed, 04 Jul 2001 15:27:52 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: Net::Telnet logging on problem.....
Message-Id: <mbudash-A6D05F.08275104072001@news.sonic.net>
In article <O9C07.4584$A51.1159941@monolith.news.easynet.net>, "Yasser
Nabi" <yasser.nabi@uk.easynet.net> wrote:
> Hi,
>
> Below is my test script :
>
> #!/usr/bin/perl
>
> use Net::Telnet ();
>
> $host="<my host>";
>
> $username="admin";
>
> $passwd="<my password>";
>
> $t = new Net::Telnet(Timeout => 10,
>
> Dump_Log => "/tmp/dump.log",
>
> Input_log => "/tmp/input.log",
>
> Output_Log => "/tmp/output.log",
>
> Port => "10001");
>
> $t->open($host);
>
> $t->waitfor('/login: /');
>
> print "Recieved login prompt -- sending $username\n";
>
> $t->print("$username");
>
> $t->waitfor('/password: /');
>
> print "Recieved password prompt -- sending password\n";
>
> t->print("$passwd");
>
> Ive tried loads of different things but just cant get it to log on...
you're not checking for an error on your Net::Telnet::open(). after
that, i've always used:
$t->login($username, $passwd);
rather than explicit waitfor's and print's... perhaps that would work
for you...
hth-
--
Michael Budash ~~~~~~~~~~ mbudash@sonic.net
------------------------------
Date: Wed, 04 Jul 2001 17:14:58 GMT
From: Rickey Ingrassia <r1ckey@home.com>
Subject: Re: Net::Telnet logging on problem.....
Message-Id: <maI07.12100$wr.62100@news1.frmt1.sfba.home.com>
In comp.lang.perl.misc Yasser Nabi <yasser.nabi@uk.easynet.net> wrote:
>Hi,
>Below is my test script :
>#!/usr/bin/perl
>use Net::Telnet ();
>$host="<my host>";
>$username="admin";
>$passwd="<my password>";
>$t = new Net::Telnet(Timeout => 10,
>Dump_Log => "/tmp/dump.log",
>Input_log => "/tmp/input.log",
>Output_Log => "/tmp/output.log",
>Port => "10001");
>$t->open($host);
>$t->waitfor('/login: /');
>print "Recieved login prompt -- sending $username\n";
>$t->print("$username");
>$t->waitfor('/password: /');
>print "Recieved password prompt -- sending password\n";
>t->print("$passwd");
--snip--
It seems to me you're making this harder than it needs to be. I use the
Telnet module daily, and the following code works on 98.865% of the devices
I connect to. The prompt may need to be adjusted depending on the device
you're connecting to.
use Net::Telnet;
my $host = new Net::Telnet( Host => <IP ADDRESS>,
Errmode => "return",
Prompt => '/[?>:#\]][ ]*$/' # generic prompt
);
# just use input_log not dump_log or output_log
# input_log is human readable
$host->input_log("/tmp/input.log");
# use login not print statements
$host->login("$username","$password");
# use cmd not print, requires accurate prompt setting
my @who = $host->cmd("who");
$host->cmd("exit");
$host->close;
print @who."\n";
--
____ __
______/_ | ____ | | __ ____ ___.__.
\_ __ \ |/ ___\| |/ // __ < | |
| | \/ \ \___| <\ ___/\___ |
|__| |___|\___ >__|_ \\___ > ____|
_________________\/_____\/____\/\/_____
---------------------------------------
------------------------------
Date: 4 Jul 2001 09:37:16 -0700
From: vrigas@hotmail.com (Vassilis Rigas)
Subject: Re: Odd scalar equality problem
Message-Id: <70623463.0107040837.597c906a@posting.google.com>
Ren Maddox <ren@tivoli.com> wrote in message news:<m3g0cenluw.fsf@dhcp9-173.support.tivoli.com>...
> On 3 Jul 2001, vrigas@hotmail.com wrote:
>
> > Ok thats the problem:
> >
> > I read a file and I have to compare every line with a string posted
> > from a html form.
> >
> > I get the forms value in an index (@value).
> > the $value[0] scalar is the name of file I 'd like to check and
> > edit...
> > the $value[1] scalar is the line I need to match...
> >
> > That is the code:
> >
> > open (READFILE,">$value[0]");
>
> I assume that ">" is a typo, but you really, really should always
> check the success of open:
Yes it is a typo indeed, sory for the confusion.
>
> open READFILE, "<", $value[0] or die "Could not read $value[0], $!\n";
>
You guys are right, I have to use this for confirmation. I add it in
my code, and files are opening OK
> > @lines=<READFILE>;
> > close READFILE;
> >
> > open (EDITFILE,">$value[0]");
>
> open EDITFILE, ">", $value[0] or die "Could not create $value[0], $!\n";
>
> > foreach $line (@lines){
> > if ($line eq $value[1]) #### well it won't match :(
>
> Well, $line has a line ending on it. One of "\n", "\r", "\r\n",
> depending on how and where the file was generated. The normal
> solution is to use chomp(), unless the file is from a foreign system.
>
> > {
> > do stuf...
> > }
> print EDITFILE $line ;
> > }
> > close EDITFILE ;
> >
> > I wonder if I m doing something wrong. When I print the value[1] in
> > the result page, it comes with the write value.
> >
> > If I change the:
> > if ($line eq $value[1])
> > with
> > if ($line eq "a real line")
> > everything works fine.
>
> Really? Well that implies that you are removing the line ending from
> $line, either with chomp() or some other mechanism. Otherwise, it
> wouldn't match "a real line". So how is $value[1] being initialized?
> Perhaps it has a line ending.
>
Yes I do remove line endings with chomp()
> > if I put a line like this:
> > $value[1]="a real line";
> > it also works fine
>
> Yeah, that pretty much clenches it. $value[1] almost certainly has a
> line ending, or possibly some other special character.
>
> > I m out of ideas... any clue?
>
> Well, you could check the value of $value[1] with something like:
>
> printf "%3d [$_]\n", ord for $value[1] =~ /./gs;
>
> If $value[1] has a "\r\n" at the end, you'll get:
>
> 97 [a]
> 32 [ ]
> 114 [r]
> 101 [e]
> 97 [a]
> 108 [l]
> 32 [ ]
> 108 [l]
> 105 [i]
> 110 [n]
> 101 [e]
> ]13 [
> 10 [
> ]
>
> HTH...
That sound's a good idea. I m not familiar with this procetion, but I
ll try it any way.
Some people in IRC tald me that there must be a problem with pack()
funtion. I use a rutine that I copied/pasted from another script, to
convert the elements of a form to plain text, and it worked fine until
now. I m a newbe, and not familiar with the pack function. Is there a
possibility I m doing something wrong there?
the rutine is:
read(STDIN, $buffer , $ENV{"CONTENT_LENGTH"});
print "Content-type: text/html\n\n";
my(@value);
@pairs=split(/\&/,$buffer);
$i=0;
foreach $pair (@pairs){
($name[$i],$value[$i])=split(/\=/,$pair);
$value[$i] =~ tr/+/ /;
$value[$i] =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",
hex($1))/eg;
$i++;
}
Is it posible that I m having false when I compare a text line from a
file with a scalar with a value taken after the above rutine runned?
------------------------------
Date: Wed, 04 Jul 2001 10:26:44 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Perl on AS/400
Message-Id: <3B4327A4.D91959AA@earthlink.net>
coberbeck wrote:
>
> We are running an AS/400 V4R4 and would like to use a PERL script on
> our Net.Commerce 3.2 enhanced web-site. We have found the link to
> Perl 5.005
>
> http://www.iseries.ibm.com/tstudio/workshop/tiptools/perl.htm
>
> Has anyone had any experience with implementing PERL?
Larry Wall is the guy who invented and implemented Perl. Or did you
mean implementing programs written in Perl? Or did you mean porting the
interpreter, whose name is "perl" (all lowercase).
> Here is the script we would like to run from the HTTP web server to
> accept a post from Yahoo Store. Would it work?
Try it and find out.
Regardless, there's a number of things which you could do to make it
better.
> #!/usr/local/bin/perl
You should be using -w and use strict.
> #
> # Install (a modified version of) this program in your webserver's
> # cgi-bin directory.
> #
> # This demo program prints the order into /tmp/yahoo-order; you will
> probably want
> # to do something more interesting with it. Replace the function
> handle_order
> # It also puts the raw key-value fields into /tmp/yahoo-order.raw
Learn the Perl language, and if you're working from a demo, make sure
that it's one which is written for the newest perl available for your
system.
[snip comments]
> require 5.001;
The newest stable perl is 5.6. Use it.
> use strict;
>
> if ($ENV{'REQUEST_METHOD'} ne "POST") {
> die("Expecting a POST, bailing");
> }
You should be using CGI.pm, especially if you're new to the language and
to writing CGI programs.
use CGI;
use CGI::Carp qw(fatalsToBrowser);
> my $o;
Single letter variables are not a good idea... very poor style.
> read(STDIN,$o,$ENV{'CONTENT_LENGTH'});
Always check the return value from system calls.
> my %o;
> for (split(/&/,$o)) {
> $_ =~ s/\+/ /g;
> my($key,$val) = split(/=/,$_,2);
> for ($key,$val) {
> $_ =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr(hex($1))/ge;
> }
> $o{$key} = $val;
> }
The CGI.pm module does all of this for you.
> my @items;
> my $i;
> for ($i=1; $i<=$o{'Item-Count'}; $i++) {
> push(@items,{map {($_,$o{"Item-$_-$i"})} qw(Id Code Quantity
> Unit-Price Description Url)});
> }
I would move the fields outside the loop, and combine assignment with
declaration... and eliminate the toplevel my $i;
my @fields = qw(Id Code Quantity Unit-Price Description Url);
my @items = map {my $i = $_;
+{map {$_,param("Item-$_-$i")} @fields};
} 1 .. param("Item-Count");
> open(RAW,">/tmp/yahoo-order.raw");
> for (sort keys %o) {
> print RAW "$_ = $o{$_}\n";
> }
> close(RAW);
Always, yes, always, check the return value of open. Also, with newer
perls, a lexical filehandle will do an automatic close at the end of
it's scope.
{; local $\ = "\n";
open my $raw, ">", "/tmp/yahoo-order.raw"
or die "open( > /tmp/yahoo-order.raw ): $!;
print $raw $_, " = ", param $_ for sort param;
}
> &handle_order(\%o,@items);
Modern perls do not need an & for calling subroutine calls; in fact,
it's deprecated unless you are doing it intentionally for the purpose of
disabling the prototype.
> # A successful delivery is indicated by a good HTTP result code.
> print "Status: 200 OK\n";
> print "\n";
> exit(0);
This is an ok cgi header, but you should have more than just an empty
body.
print header, begin_html, h1 q(Form was submitted correctly), end_html;
exit;
>
> ######################################################################
> # Replace this function with something that does what you want
> # $info gets a hash ref of all the order fields, like Ship-Name.
> # @items gets an array of hash refs, one for each item.
>
> sub handle_order {
> my($info,@items)=@_;
>
> open(OUT,">/tmp/yahoo-order");
A lexical would probably be better than a non-localized bareword glob.
open( my $out, ">", "/tmp/yahoo-order" )
or die "Couldn't open /tmp/yahoo-order for writing: $!";
Always, yes, always, check the return value of open.
> print OUT "Order $info->{ID} at $info->{Date}\n";
> print OUT "\n";
> print OUT "Ship to:\n";
> print OUT " $info->{'Ship-Name'}\n";
> print OUT " $info->{'Ship-Address1'}\n";
> print OUT " $info->{'Ship-Address2'}\n";
> print OUT " $info->{'Ship-City'} $info->{'Ship-State'}
> $info->{'Ship-Zip'}\n";
> print OUT " $info->{'Ship-Country'}\n";
> print OUT " $info->{'Ship-Phone'}\n";
> print OUT "\n";
> print OUT "Bill to:\n";
> print OUT " $info->{'Bill-Name'}\n";
> print OUT " $info->{'Bill-Address1'}\n";
> print OUT " $info->{'Bill-Address2'}\n";
> print OUT " $info->{'Bill-City'} $info->{'Bill-State'}
> $info->{'Bill-Zip'}\n";
> print OUT " $info->{'Bill-Country'}\n";
> print OUT " $info->{'Bill-Phone'}\n";
> print OUT " $info->{'Bill-Email'}\n";
> print OUT "\n";
> print OUT "Shipping: $info->{'Shipping'}\n";
> print OUT "\n";
> print OUT "Payment: $info->{'Card-Name'} $info->{'Card-Number'},
> exp $info->{'Card-Expiry'}\n";
> print OUT "\n";
> print OUT "Items:\n";
This would probably look better with a printf.
my @to=qw(Name Address1 Address2 City State Zip Country Phone);
printf $out, "Order %s at %s\n\n".
"Ship to:\n\n %s\n %s\n %s\n %s %s %s\n %s\n %s\n\n".
"Bill to:\n\n %s\n %s\n %s\n %s %s %s\n %s\n %s\n\n".
"Shipping: %s\n\n".
"Payment: %s %s exp %s\n\n".
"Items:", map {param $_} (
map("Ship-$_", @to), map("Bill-$_", @to),
"Shipping", map("Card-$_",qw(Name Number Expiry))
);
> printf(OUT " %-15s %-40s %6s %8s %8s\n",
> "Code","Desc","Qty","Each","Total");
Ok, but qw would make this look a little better:
printf $out " %-15s %-40s %6s %8s %8s\n",
qw( Code Desc Qty Each Total );
> my $item;
> for $item (@items) {
You can combine the my and the for:
for my $item (@items) {
>
> printf(OUT " %-15s %-40s %6d %8.2f %8.2f\n",
> $item->{'Code'},
> $item->{'Description'},
> $item->{'Quantity'},
> $item->{'Price'},
> $item->{'Quantity'} * $item->{'Price'});
The map operator would help here:
printf $out " %-15s %-40s %6d %8.2f %8.2f\n",
map( $item->{$_}, qw(Code Description Quantity Price ),
$item->{Quantity} * $item->{Price};
Remember that inside of {} for hash dereferences, you do not need quotes
for bareword strings.
> }
>
> printf(OUT " %-15s %-40s %6s %8s %8.2f\n",
> "","Tax","","",$info->{'Tax-Charge'});
>
> printf(OUT " %-15s %-40s %6s %8s %8.2f\n",
> "","Shipping","","",$info->{'Shipping-Charge'});
>
> printf(OUT " %-15s %-40s %6s %8s %8.2f\n",
> "","Total","","",$info->{'Total'});
>
> close(OUT);
Always, yes, always, check the return value of close if the file is read
in any mode other than read-only. What if the filesystem is full?
>
> }
Anyway, I don't see any reason for this code to be in a subroutine
rather than in the main body of the code. It seems a dumb way to write
it.
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
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 1244
***************************************