[17933] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 93 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jan 18 18:11:36 2001

Date: Thu, 18 Jan 2001 15:10:13 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <979859413-v10-i93@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Thu, 18 Jan 2001     Volume: 10 Number: 93

Today's topics:
    Re: Need script help - consolidation <mjcarman@home.com>
        Perl - sys admin question <tommylebrun@yahoo.com>
    Re: Perl/IIS and open() (Maggert)
    Re: Perl/IIS and open() <jew208@psu.edu>
        Question regarding encrypting passwords <donald.j.miller@noaa.gov>
    Re: remove non word characters EXCEPT blank (Craig Berry)
    Re: remove non word characters EXCEPT blank (Tad McClellan)
    Re: remove non word characters EXCEPT blank <ren.maddox@tivoli.com>
    Re: SQL::Statement 0.1012 on MacPerl, and Perl 5.004 <nospam@nospam.com>
    Re: techniques for finding slow code? <mjcarman@home.com>
    Re: Unicode conversion module wanted. (Honza Pazdziora)
        WIN32::DUN <ediril@ece.wpi.edu>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Thu, 18 Jan 2001 09:07:42 -0600
From: Michael Carman <mjcarman@home.com>
Subject: Re: Need script help - consolidation
Message-Id: <3A6706BE.E19704F3@home.com>

bigdawg wrote:
> 
> The following is a perl script that I wrote that works, but 
> would be better served using arrays.  Help if you can please.

So you only want advice on arrays? How about just a quick general
critique? There's too much code for me to want to go through every line
in detail, so I'll only point out the first place I see something.
 
> #!/usr/bin/perl

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

All non-trivial programs should start with these -- they cause Perl to
give you all sorts of free advice on your scripts. Consult the manpages
for a detailed description of what they do and why they're so helpful.

>  my $copy_tricerat     = "1";

No need to stringify this.
my $copy_tricerat = 1;

>  my $copy_UPI          = "1";
>  my $copy_repl         = "1";
>  my $copy_depositpro   = "1";

[...]
I see a pattern. How about a hash?

my %copy_opts = (
    tricerat   => 1,
    UPI        => 1,
    repl       => 1,
    depositpro => 1,
    #...
);

>    %NETRESOURCE=(LocalName=>"x:",RemoteName=>"\\\\snap1\\backups");

Your hash values are literal strings, so you don't need the
interpolation of double-qoutes. If you use single quotes you won't have
to escape the backslashes.

%NETRESOURCE=(LocalName=>'x:',RemoteName=>'\\snap1\backups');

Futhermore, Perl is smart. You can use forward slashes for everything
(ala Unix) so you don't have to worry about "\backups" becoming
"<backspace>ackups." Perl will Do The Right Thing.

%NETRESOURCE=(LocalName=>'x:',RemoteName=>'//snap1/backups');

>    #- Also, we're in a catch 22 'cause you can't create a log 
>    #- unless a drive is mapped, and can't log the mapping 
>    #- without the mapped drive!  So if this fails,
>    #- you'll never know!
> 
> #--------------------------------------------------------------
>    system("if exist x: subst x: /d");
>    system("if exist x: net use x: /d");

How about checking the return value from the system call? Then you'll
know if the mapping failed and you can die() with an appropriate
message.

>  if ($forcecopy == "1") {

No need to stringify the value. If $forcecopy is used as a boolean, you
don't have to do a comparison at all!

if ($forcecopy) {

>    copy_daily();
>    copy_weekly();
>    copy_monthly();
>    delete_daily();
>    delete_weekly();
>    delete_monthly();
>    closelog();
>    die;

Why are you die()ing here? This isn't a fatal error! Use exit() instead.

>  if ($wday == "0" && $mday =~ /[25,26,27,28,29,30,31]/) {

Are you *sure* this is doing what you think it is? [] in a regex creates
a character class. How about this instead:

if ($wday == 0 && $mday >= 25) {

> sub copy_daily {
> 
>   #----CHECK FOR EXISTENCE OF DAILY DIR
>     $dailydir="x:\\daily";
>     unless (-e $dailydir) {
>         mkdir ($dailydir, 0777);

You should check whether or not mkdir() was successful, and respond
appropriately if it wasn't.

At a glance, it looks like the rest of your program is pretty much the
same, so I'll stop.

As Damian pointed out, most of your subs are redundant. You should be
able to factor out the common portions, pass in some parameters, and
reduce the length of your code considerably. Keep after it.

-mjc


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

Date: Thu, 18 Jan 2001 19:05:28 GMT
From: T <tommylebrun@yahoo.com>
Subject: Perl - sys admin question
Message-Id: <947epg$l9b$1@nnrp1.deja.com>

I am wanting to do some reporting from my utmp file. I am using the
example shown on pages 294-297 of the 'Perl for System Administration'
book. The part I am having trouble with is setting up the template for
the 'C data structure'. My 'struct utmp' in my utmp.h file is exactly
like the Digital Unix example on page 295 of the book. However, the
author does not give an example for this type of structure. Does anyone
know what the template should look like for this type of structure?
Thanks,      T.


Sent via Deja.com
http://www.deja.com/


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

Date: Thu, 18 Jan 2001 20:15:08 GMT
From: mag@ionet.net (Maggert)
Subject: Re: Perl/IIS and open()
Message-Id: <3a674d18.590774598@news.ionet.net>

On Thu, 18 Jan 2001 17:57:56 GMT, jewoltman@my-deja.com wrote:

>Thanks all for your suggestions, but nothing
>works.
>Here is the exact code I'm using (to test with)

	Try this.
>
>use CGI ':standard';
$file = './test.html';
>print header;
>open (FILE, "<$file") or die eval
>(print "Error<BR>$!");
>for $line (<FILE>)
>{
>	print $line;
>}
>

	IIS is a total crap webserver when it comes to perl. On one
IIS server I had to put the < in to open the file so I always do that
now just in case. The ./ should be enough of a path statement, but if
it isn't you could always use $ENV{'DOCUMENT_ROOT'}/logs or something
to that effect. This is of course if the file is in the document root.




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

Date: Thu, 18 Jan 2001 22:49:50 GMT
From: "John Woltman" <jew208@psu.edu>
Subject: Re: Perl/IIS and open()
Message-Id: <iqK96.1196$7b2.83684@bgtnsc05-news.ops.worldnet.att.net>

A little bit closer to solving it, perhaps.  The script will work if I put
it in the root directory of the web server, but none of the subdirectories
(cgi-bin for example).  Is there some weird setting I need to change in IIS
to get this to work?  Hmmmmmm.
I did put the < in my open statement.
 - John woltman

"Maggert" <mag@ionet.net> wrote in message
news:3a674d18.590774598@news.ionet.net...
> On Thu, 18 Jan 2001 17:57:56 GMT, jewoltman@my-deja.com wrote:
>
> >Thanks all for your suggestions, but nothing
> >works.
> >Here is the exact code I'm using (to test with)
>
> Try this.
> >
> >use CGI ':standard';
> $file = './test.html';
> >print header;
> >open (FILE, "<$file") or die eval
> >(print "Error<BR>$!");
> >for $line (<FILE>)
> >{
> > print $line;
> >}
> >
>
> IIS is a total crap webserver when it comes to perl. On one
> IIS server I had to put the < in to open the file so I always do that
> now just in case. The ./ should be enough of a path statement, but if
> it isn't you could always use $ENV{'DOCUMENT_ROOT'}/logs or something
> to that effect. This is of course if the file is in the document root.
>
>




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

Date: Thu, 18 Jan 2001 17:20:10 -0500
From: "Donald J. Miller III" <donald.j.miller@noaa.gov>
Subject: Question regarding encrypting passwords
Message-Id: <3A676C1A.743836BF@noaa.gov>

This is a multi-part message in MIME format.
--------------D839726E27266A47F21EB0DF
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hello,

I would like to use Net::FTP to retrieve some files from a server.
However, I would prefer not to send the passwords as plaintext. I've
looked at some of the Crypt:: modules on CPAN, but I am not sure which
one(s) would be best to use for encrypting a password that would also be
decryptable by the server.

Any ideas?

Thanks,

Don

--------------D839726E27266A47F21EB0DF
Content-Type: text/x-vcard; charset=us-ascii;
 name="donald.j.miller.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Donald J. Miller III
Content-Disposition: attachment;
 filename="donald.j.miller.vcf"

begin:vcard 
n:Miller III;Donald J. 
tel;fax:631-244-0167
tel;work:631-244-0122 (Voice)     
x-mozilla-html:FALSE
url:http://www.werh.noaa.gov
org:NWS Eastern Region;Meteorological Services Division
adr:;;630 Johnson Avenue;Bohemia;NY;11716;631-244-0122 (Voice)   631-244-0167 (Fax)
version:2.1
email;internet:donald.j.miller@noaa.gov
title:Operational Support Meteorologist (W/ER1x3)
fn:Donald J. Miller III
end:vcard

--------------D839726E27266A47F21EB0DF--



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

Date: Thu, 18 Jan 2001 19:10:51 -0000
From: cberry@cinenet.net (Craig Berry)
Subject: Re: remove non word characters EXCEPT blank
Message-Id: <t6eftro56gru67@corp.supernews.com>

dts@netkonnect.net wrote:
: I am trying to come up with a simple way to remove non word characters
: from a string, while still preserving blank spaces.  The following
: works for removing all non-word characters including blanks;
: 
: $var1='this is(%#';
: $var2=$var1;
: $var2=~ s/[^a-zA-Z_0-9]//g;
: print "var1 is $var1 and var2 is $var2";
: 
: is there s simple trick in Perl that would allow me to modify the \W to
: exclude blanks?

Turn your approach around:

  s/[^\w ]//g;

If you don't need \w to be locale-aware,

  tr/A-Za-z0-9_ //cd;

is more efficient.

-- 
   |   Craig Berry - http://www.cinenet.net/~cberry/
 --*--  "The hills are burning, and the wind is raging; and the clock
   |   strikes midnight in the Garden of Allah." - Don Henley


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

Date: Thu, 18 Jan 2001 19:43:48 GMT
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: remove non word characters EXCEPT blank
Message-Id: <slrn96eate.5cb.tadmc@tadmc26.august.net>

dts@netkonnect.net <dts@netkonnect.net> wrote:

>I am trying to come up with a simple way to remove non word characters
>from a string, while still preserving blank spaces.

>$var1='this is(%#';
>$var2=$var1;
>$var2=~ s/[^a-zA-Z_0-9]//g;

   $var2 =~ s/[^a-zA-Z0-9_ ]//g;

But pattern matches are for strings, you are dealing with
characters so this will be much faster:

   $var2 =~ tr/a-zA-Z0-9_ //cd;


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: 18 Jan 2001 12:46:09 -0600
From: Ren Maddox <ren.maddox@tivoli.com>
Subject: Re: remove non word characters EXCEPT blank
Message-Id: <m3n1coog0e.fsf@dhcp11-177.support.tivoli.com>

dts@netkonnect.net writes:

> $var2=~ s/[^a-zA-Z_0-9]//g;

You can include the predefined character classes in another character
class, so just add \s to what you have (and we might as well use \w
for what you already have):

  $var2 =~ s/[^\w\s]+//g;

(Adding the + is just an optimization.)

-- 
Ren Maddox
ren@tivoli.com


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

Date: 18 Jan 2001 20:27:31 GMT
From: The WebDragon <nospam@nospam.com>
Subject: Re: SQL::Statement 0.1012 on MacPerl, and Perl 5.004
Message-Id: <947jjj$evu$0@216.155.33.69>
Keywords: modules, SQL::Statement, DBI, sorting, sorting

on 01/11/2001 03:32 AM, Jochen Wiedmann at joe@ispsoft.de wrote:
 
>> this sort section starting on line 236 
>> 
>> @$rows = sort {
>> $i = 0;
>> do {
>> $colNum = $sortCols[$i++];
>> $desc = $sortCols[$i++];
>> $c = $a->[$colNum];
>> $d = $b->[$colNum];
>> if (!defined($c)) {
>> $result = -1;
>> } elsif (!defined($d)) {
>> $result = 1;
>> } elsif ($c =~ /^\s*[+-]?\s*\.?\s*\d/  &&
>> $d =~ /^\s*[+-]?\s*\.?\s*\d/) {
>> $result = ($c <=> $d);
>> } else {
>> $result = $c cmp $d;
>> }
>> if ($desc) {
>> $result = -$result;
>> }
>> } while ($result == 0  &&  $i < @sortCols);
>> $result;
>> } @$rows;
>> 
>> gives me an odd result when sorting this database using the DBD::CSV 
>> module
>> 
>> I'm doing some preliminary testing now that I've finally got all the 
>> little brats to work together in MacPerl :), and I get this
>> 
>> # Argument "86nfgzmgh-hk-fg.ark" isn't numeric in ncmp, <GEN0> chunk 
>> 2849. File 'Primus 8.5GB:Applications:MacPerl 
>> Ÿ:site_perl:SQL:Statement.pm'; Line 247 # Argument 
>> "0wiztlmhrmgil-fghk.ark" isn't numeric in ncmp, <GEN0> chunk 2849. 
>> File 'Primus 8.5GB:Applications:MacPerl 
>> Ÿ:site_perl:SQL:Statement.pm'; Line 247
>> [big snip]
>> Count:14 Game: utother, File: ut-logo-map-space.zip, Title: 
>> ut-logo-map-space, 
>> Size: 13, Review: /nalicity/review.asp?Id=6498, Rating: 7
>> Count:15 Game: utother, File: ut-whitepalace][.zip, Title: 
ut-whitepalace][, 
>> Size: 2486, Review: /nalicity/reviews/ut-whitepalace][.html, Rating: 
8.5
>> Count:16 Game: utother, File: 13mutants-sp-ut.zip, Title: 
13mutants-sp-ut, 
>> Size: 3572, Review: /nalicity/reviews/13mutants-sp-ut.html, Rating: 7
>> Count:17 Game: utother, File: 9dragonsintro-utsp.zip, Title: 
>> 9dragonsintro-utsp, Size: 2905, Review: 
>> /nalicity/reviews/9dragonsintro-utsp.html, Rating: 3
>> 
>> when executing this statement:
>> 
>> #!perl -w
>> use strict;
>> use DBI;
>> 
>> my $connect_dir = "Primus 8.5GB:Applications:MacPerl 
Ÿ:Nalicity:ratings:DBI 
>> testing:tables:";
>> 
>> my $dbh = 
DBI->connect("dbi:CSV:f_dir=${connect_dir};csv_sep_char=\,", '', 
>> '', 
>> { RaiseError => 1 }) 
>> or die "Can't connect to database: $DBI::errstr";
>> 
>> # I even tried this, but no effect. :-(
>> $dbh->{csv_tables}->{'testmapslist'}->{'types'} = 
>> [ Text::CSV_XS::IV(), Text::CSV_XS::PV(), Text::CSV_XS::PV(), 
>> Text::CSV_XS::PV(), Text::CSV_XS::PV(), Text::CSV_XS::IV(), 
>> Text::CSV_XS::PV(), Text::CSV_XS::NV(), ];
>> 
>> my $sth = $dbh->prepare(" SELECT * FROM testmapslist WHERE gametype = 
>> 'utother' AND rating >= 0 ORDER BY coercename DESC ");
>> $sth->execute;
>> 
>> my($id, $gametype, $coercename, $filename, $title, $size, $review, 
$rating);
>> $sth->bind_columns(\($id, $gametype, $coercename, $filename, $title, 
$size, 
>> $review, $rating));
>> 
>> my $count = 1;
>> while ($sth->fetchrow) {
>> print "Count:$count Game: $gametype, File: $filename, Title: $title, 
Size: 
>> $size, Review: $review, Rating: $rating\n";
>> ++$count;
>> }
>> 
>> $dbh->disconnect;
>> 
>> exit 0;
>> 
>> I thought first of doing something like specifying the types using 
>> 
>> SELECT id(num), gametype(char), coercename(char), etc.. WHERE .. 
>> blah blah
>> 
>> but that obviously didn't work, despite the seeming simplicity and 
>> obviousness of doing it that way, although it is also quite messy. 
>> 
>> Is there any way around this? If you've patched this in later 
>> versions of SQL::Statement, is there a way I can fix it for this 
>> version until someone ports a more recent one? 
>> 
>> Basically, I've created the 'coercenames' to force the 
>> 'numerically-starting' filenames to sort to the bottom, and did a 
>> character reversal on them so that when you reverse the sort, it 
>> always does the right thing. :) (sort of a Schwartzian Transform) .. 
>> normally I was creating this on the fly while importing the data 
>> from my flat-file, and either padding it with the rating, or not, 
>> depending whether I wanted to sort by title, or by rating and then 
>> title. 
>> 
>> What with the SQL ability to sort WHILE reading the data, it may not 
>> be necessary, but it seems to be the only way I can get the numerics 
>> to sort to the bottom always, and still remain in the correct order 
>> otherwise. 
>> 
>> Why is it treating it like a numeric despite the obviousness that it 
>> is a string? after looking at some other sort algorithms recently, I 
>> thought to myself "wouldn't it be simpler to write it as this?" 
>> { $a <=> $b 
>>   || 
>>   $a cmp $b 
>> }
>> 
>> ... or am I off-base here? Do you now have a better way to determine 
>> if a field value is numeric or string?
>> 
>> Your thoughts (and time) would be appreciated. 
>> 
>> I hope I haven't included too much information, but I thought being 
>> thorough and giving a full explanation while showing what I tried 
>> was better than a half-assed question. :)
 

> Hi, Scott,
> 
> I highly agree with you, that the RE that determines whether
> a column contains a number should be changed. Any suggestions,
> besides ^[+-]\d+$, possibly including floats?
> 
> Thanks,
> 
> Jochen

I mailed Jochen back and indicated that I'd be posting this query to the 
MacPerl lists and to comp.lang.perl.misc as well in search of a better 
answer.. responses welcome. I'm stumped. :)

*muttergrumbleregexes*

-- 
send mail to mactech (at) webdragon (dot) net instead of the above address. 
this is to prevent spamming. e-mail reply-to's have been altered 
to prevent scan software from extracting my address for the purpose 
of spamming me, which I hate with a passion bordering on obsession.  


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

Date: Thu, 18 Jan 2001 11:40:56 -0600
From: Michael Carman <mjcarman@home.com>
Subject: Re: techniques for finding slow code?
Message-Id: <3A672AA8.B4D25814@home.com>

dtbaker_dejanews@my-deja.com wrote:
> 
> Michael Carman <mjcarman@home.com> wrote:
> 
>> You didn't read closely enough. :) If you had you would have seen
>> part about the Devel::DProf module.
> 
> I am a little concerned if it will install/run in ActivePerl, 
> but I guess if it is available thru the ppm install, it *should* 
> be ok.

It works fine in ActivePerl. Actually, I believe it comes standard,
which means you *already* have it installed.
 
> I am hoping there is a little more documentation in the module
> readme? I took a look at the CPAN docs, and it is a little 
> unclear where the profile output file will pop out.

The module manpage has the best (most complete) documentation. Is that
what you looked at? If you have ActivePerl installed, you should also
have all the docs in HTML format. It's in there. I've never tried to use
it with CGI though, so you may have to do a little experimentation.
Perhaps someone else in clp.misc can help you with that.

> Lastly, it sounded like the granularity will be only as fine as my
> non-built-in subs... which will help me to a point, but then within
> one sub in particular i know there is some significant REGX and
> looping, and it will be a pain to figure out which of these is 
> killing me unless I can edit and isolate them out into separate
> subroutines of their own.

Well now, if the computer could do everything for you, you wouldn't have
a job, now would you? :)

Really, though, it's pretty simple to get a rough idea of efficiency.
How many loops? Are they nested? Is there anything done inside a loop
which could be pulled out? Sometimes you can sacrifice memory to improve
speed. How complex are your regexes? Sometimes it's better to use two
simple ones than one messy one. Do any of them have variables in the
pattern? If so the regex has to be recompiled each time (unless the
variable doesn't change, in which case you can use the /o modifier -- do
you?). Do you use $&, $', or $` anywhere? That slows down *all* regexes.

There are certainly other things to look for as well, those are just the
ones that spring to mind. They're all just tweaks, though. The best way
to improve efficiency is to find a better algorithm. That's where the
use of Benchmark comes in. Find your bottleneck, rewrite it a few
different ways, and see which is quickest.

-mjc


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

Date: Tue, 16 Jan 2001 22:27:45 GMT
From: adelton@fi.muni.cz (Honza Pazdziora)
Subject: Re: Unicode conversion module wanted.
Message-Id: <slrn969in0.cc8ri.adelton@aisa.fi.muni.cz>

On Tue, 16 Jan 2001 18:34:44 GMT, GC <glchy@my-deja.com> wrote:
> 
> #!/usr/local/bin/perl
> 
> use Text::Iconv;
> $converter = Text::Iconv->new("iso8859-1", "utf-8");

				'8859-1', 'UTF-8'

> Actually my objective is to convert some string in any code set (e.g
> jis) to Unicode. But i dont know what to replace the "tocode" to get
> Unicode.
> 
> Am i missing some other modules?

No, it's just that Solaris iconv may name the charsets differently
from other iconvs. Do man iconv(1) to see where the conversion tables
are (on my Solaris 2.7 it's /usr/lib/iconv) and then see what the
names on your system are.

Alternativelly you can make symlinks to match more widely used names.

Yours,

-- 
------------------------------------------------------------------------
 Honza Pazdziora | adelton@fi.muni.cz | http://www.fi.muni.cz/~adelton/
   .project: Perl, DBI, Oracle, MySQL, auth. WWW servers, MTB, Spain.
Petition for a Software Patent Free Europe http://petition.eurolinux.org
------------------------------------------------------------------------


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

Date: Thu, 18 Jan 2001 14:03:46 -0500
From: Emrah Diril <ediril@ece.wpi.edu>
Subject: WIN32::DUN
Message-Id: <Pine.OSF.4.21.0101181402450.8552-100000@ece.wpi.edu>


I am trying to find the module Win32::DUN, but it looks like it dropped
off the face of the earth. Does anyone know of any modules to do DailUp
Networking stuff? 

Reply via e-mail please.
Thanks.


Emrah



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

Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.

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


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