[19699] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1894 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Oct 9 06:05:34 2001

Date: Tue, 9 Oct 2001 03:05:09 -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: <1002621909-v10-i1894@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Tue, 9 Oct 2001     Volume: 10 Number: 1894

Today's topics:
    Re: Any help with this program? <goldbb2@earthlink.net>
    Re: binmode with "perl -p" doesn't work (Eric Bohlman)
    Re: binmode with "perl -p" doesn't work <bart.lateur@skynet.be>
    Re: hash problem (Clinton A. Pierce)
    Re: hash problem <goldbb2@earthlink.net>
        How do I get the next line? <khirv@hotmail.com>
    Re: How do I get the next line? <markus.cl@gmx.de>
    Re: How do I get the next line? <Thomas@Baetzler.de>
    Re: How to run perl under bash not sh (Tim Hammerquist)
    Re: How to run perl under bash not sh <invalid@nowhere.com>
    Re: How to run perl under bash not sh <andrew@erlenstar.demon.co.uk>
    Re: How to run perl under bash not sh <lt@toetsch.at>
    Re: Illegal Instruction - out of the blue (Clinton A. Pierce)
    Re: Multiplexing strings (Damian James)
        Newbie basic question on splitting strings (Richard)
    Re: Obfuscation <goldbb2@earthlink.net>
    Re: Obfuscation <tim_odomNOSPAM@NOSPAMhotmail.com>
        please check my script (Kimo R.)
    Re: please check my script (Damian James)
        Regular expression whole string matching (JJ)
    Re: Regular expression whole string matching <markus.cl@gmx.de>
        Shouldn't /c preserve pos() when matching in list conte <david.bouman@nl.xo.com>
    Re: size of an object? (Clinton A. Pierce)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 08 Oct 2001 21:20:01 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Any help with this program?
Message-Id: <3BC250C1.66684BCE@earthlink.net>

Jon Fanti wrote:
> 
> Hi,
> 
> Sorry, it would help if the clock on this PC was correct (D'oh!).
> 
> I'm still a Perl newbie(ish) and am trying to write a script that runs
> through a dir and emails me with a list of any files that look like
> *.xml, *.xml.lock (upper and lowercase). As well as print this on
> screen. The major problems I have are:
> 
> a. I can't work out how to get this to search sub directories.

File::Find will do this for you.

> b. how to email me all found files in one email. At the moment it
> sends a separate email for each file found.

Start a sendmail processes using perl's version of popen (which is
open(HANDLE, "|programname")), and write all your files found to that
process, then use perl's version of pclose (which is just close(HANDLE))
to tell sendmail that you're done and that you want to know if it was
successful queuing the mail [note that sendmail usually doesn't send a
piece of mail right away, it stores it in a file for later, taking it's
own sweet time]

#!/usr/bin/perl -w
use strict;
# always, *always*, ALWAYS, use "-w" and "use strict;"

my $mailto = 'asdf@somedomain.com';

open( SENDMAIL, "|sendmail -i -U -t" )
    or die "Error starting concurrent system command: $!";
print SENDMAIL <<HEADER;
To: $mailto
From: me
Subject: *.xml and *.xml.lock files.

HEADER

use File::Find;
find( sub {
    if( /\.xml(?:\.lock)?$/i ) {
        print STDOUT "found $_\n";
        print SENDMAIL "found $_\n";
    }
}, "/data/users" );

if( close SENDMAIL ) {
    print "Done.\n";
} else {
    my ($sig, $ret) = ($?&255, $?>>8);
    die "sendmail died from signal $sig\n" if $sig;
    die "sendmail exited with code $ret\n" if $ret;
    die "Error running sendmail: $!\n" if $!;
    die "Unknown error!";
}
__END__

It would be better to use a mail sending module than to use the sendmail
program, but I'm too lazy to give you code which does that.

-- 
    "Just how stupid are you Kuno?"
    "Verily, Tatewaki Kuno knows no limits."


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

Date: 9 Oct 2001 07:48:27 GMT
From: ebohlman@omsdev.com (Eric Bohlman)
Subject: Re: binmode with "perl -p" doesn't work
Message-Id: <9pua4b$6a9$3@bob.news.rcn.net>

John Lin <johnlin@chttl.com.tw> wrote:
> In addition, I would also propose a "perl -g" switch for "glob pre-processing".
> For example, in UNIX platforms:

> perl -e "print @ARGV" *.pl *.txt *.cpp

> The OS will do globbing first then pass the list to perl.
> But some platforms (such as Win32) won't.  On Windows, people have to write

> perl -e "BEGIN { @ARGV = map {glob} @ARGV } print @ARGV" *.pl *.txt *.cpp

> If we have "-g" option, which is a short hand for

> BEGIN { @ARGV = map {glob} @ARGV }

> then the orginal script can still apply

> perl -ge "print @ARGV" *.pl *.txt *.cpp

> thus, helps the portability of scripts on those non-UNIX platforms.
> What do you think about it?
> Has this problem and suggested solution also been discussed in the past?

There are several useful workarounds; the one I use was developed by 
Gurusamy Sarathy.  I have my PERL5OPT environment variable set to "-MWild" 
and in my perl/lib directory I have Wild.pm, which consists of:

# Wild.pm - emulate shell @ARGV expansion on shells that don't
use File::DosGlob;
@ARGV = map {
             my @g = File::DosGlob::glob($_) if /[*?]/;
             @g ? @g : $_;
            } @ARGV;
1;



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

Date: Tue, 09 Oct 2001 08:02:52 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: binmode with "perl -p" doesn't work
Message-Id: <ggb5st0joc3ug1jp84hmoe2v8mft0dug49@4ax.com>

Eric Bohlman wrote:

>There are several useful workarounds; the one I use was developed by 
>Gurusamy Sarathy.  I have my PERL5OPT environment variable set to "-MWild" 
>and in my perl/lib directory I have Wild.pm, which consists of:
>
># Wild.pm - emulate shell @ARGV expansion on shells that don't
>use File::DosGlob;
>@ARGV = map {
>             my @g = File::DosGlob::glob($_) if /[*?]/;
>             @g ? @g : $_;
>            } @ARGV;
>1;

That looks... clunky. The script, I mean, not the workaround. A my()
declaration in an if statement... you're never sure what you'll get.
This statement, or a variation on it, is sometimes used to create a
static variable @g. You definitely don't want that.

This is safer -- and doesn't need the variable:

	# Wild.pm - emulate shell @ARGV expansion on shells that don't
	use File::DosGlob;
	@ARGV = map {
	            /[*?]/ ? File::DosGlob::glob($_)  : $_;
                   } @ARGV;
	1;

Plus:  if one of your "arguments" is a command line switch, you likely
don't want to replace it.

-- 
	Bart.


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

Date: Tue, 09 Oct 2001 03:45:24 GMT
From: clintp@geeksalad.org (Clinton A. Pierce)
Subject: Re: hash problem
Message-Id: <opuw7.155711$K6.74717633@news2>

In article <hhl3stksdmvp05biipcaqql5k43o83jmme@4ax.com>,
	Thomas Bätzler <thomas@baetzler.de> writes:
> On 8 Oct 2001 09:22:31,  winter7@e-mailanywhere.com (winter7) wrote:
> 
>>I want to manage same hash with some forked process without interference.
>>I tried following code.
> 
> This won't work - forking means by definition that the child process
> receives an identical copy of the parent's address space - so the
> child's hash is not the parents hash. You might want to investigate
> the IPC:: modules for shared memory and semaphores.

Or, if he's got locking going on, he could use a tied hash (as with
dbmopen).  This might be a simpler approach than involving full-blown
IPC mechanisms.

Just a thought.

-- 
    Clinton A. Pierce            Teach Yourself Perl in 24 Hours  *and*
  clintp@geeksalad.org                Perl Developer's Dictionary
"If you rush a Miracle Man,     for details, see http://geeksalad.org     
	you get rotten Miracles." --Miracle Max, The Princess Bride


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

Date: Mon, 08 Oct 2001 23:54:15 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: hash problem
Message-Id: <3BC274E7.6E191955@earthlink.net>

Clinton A. Pierce wrote:
> 
> In article <hhl3stksdmvp05biipcaqql5k43o83jmme@4ax.com>,
>         Thomas Bätzler <thomas@baetzler.de> writes:
> > On 8 Oct 2001 09:22:31,  winter7@e-mailanywhere.com (winter7) wrote:
> >
> >>I want to manage same hash with some forked process without
> >>interference.  I tried following code.
> >
> > This won't work - forking means by definition that the child process
> > receives an identical copy of the parent's address space - so the
> > child's hash is not the parents hash. You might want to investigate
> > the IPC:: modules for shared memory and semaphores.
> 
> Or, if he's got locking going on, he could use a tied hash (as with
> dbmopen).  This might be a simpler approach than involving full-blown
> IPC mechanisms.

He'll still have to use IPC stuff for synchronization due to cacheing.

-- 
    "Just how stupid are you Kuno?"
    "Verily, Tatewaki Kuno knows no limits."


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

Date: Tue, 9 Oct 2001 15:35:50 +0800
From: "blongk" <khirv@hotmail.com>
Subject: How do I get the next line?
Message-Id: <3bc2a930$1_2@news.tm.net.my>

Hi

I have 2 files. File A contains inactive user id's and file B contains all
the id's (including inactive users) and their email addresses. File A has
its contents in this form:

id5
id9
id23

and so on.. and file B has it like this

id1
emailadd1
id2
emailadd2
id3
emailadd3   and so on .....


I would like to find all the inactive users in file B based on the inactive
user id's in file A by comparing file A and B. So if the id matches, the
user id and its email address (which is the next line) will be printed into
another file. I just could not figure out how to get the email address. I
have used command "comm" to compare the files but couldn't get both id and
its respective email address.
I highly apprecite any help. Thank you.




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

Date: Tue, 9 Oct 2001 11:30:24 +0200
From: "Markus Dehmann" <markus.cl@gmx.de>
Subject: Re: How do I get the next line?
Message-Id: <9pug83$kaunv$1@ID-101658.news.dfncis.de>

"blongk" <khirv@hotmail.com> wrote in message
news:3bc2a930$1_2@news.tm.net.my...
> Hi
>
> I have 2 files. File A contains inactive user id's and file B contains all
> the id's (including inactive users) and their email addresses. File A has
> its contents in this form:
>
> id5
> id9
> id23
>
> and so on.. and file B has it like this
>
> id1
> emailadd1

my solution could maybe be shorter. first, all inctive ids are loaded into a
hash. the advantage of this is: both files don't have to be in a special
order, like id1, id2, id3, ... idn. The order doesn't matter. Note that in
the hash each id is stored together with its following newline.

# Both files should end in a newline
# First, load inactive id's into a hash
open (F1, "inactive.txt") or die ($!);
while( <F1> ){
 $inactive{$_} = 1;
}
close F1;

# Second, read all ids and compare each with the inactive
open (F2, "all_ids.txt") or die ($!);
while( (my $id = <F2>) && (my $mail = <F2>)){
 if( exists $inactive{$id} ){
  print "Found id $id with e-mail $mail";
 }
}
close F2;


PS: what is comm??




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

Date: Tue, 09 Oct 2001 11:42:26 +0200
From: =?ISO-8859-1?Q?Thomas_B=E4tzler?= <Thomas@Baetzler.de>
Subject: Re: How do I get the next line?
Message-Id: <oih5st885egt7tfro4fqacau9nho20vktj@4ax.com>

On Tue, 9 Oct 2001 15:35:50 +0800, "blongk" <khirv@hotmail.com> wrote:
>I have 2 files. File A contains inactive user id's and file B contains all
>the id's (including inactive users) and their email addresses.

#!/usr/bin/perl -w

use strict;

my $input_A = 'file_a';
my $input_B = 'file_b';

my %user;

open( IN, $input_B ) or die "Can't open file B '$input_B': $!\n";

while( defined( my $uid = <IN> ) ){
  chomp( $uid );
  die "Premature end of file B!\n" unless defined( my $email = <IN> );
  chomp( $email );
  $user{$uid} = $email;
}

close( IN );

open( IN, $input_A ) or die "Can't open file A '$input_A': $!\n";

while( defined( my $uid = <IN> ) ){
  chomp( $uid );
  print "Inactive user $uid <$user{$uid}>\n" if exists $user{$uid};
}

__END__

Writing out the report to a summary file is left as an exercise for the
reader :-)

HTH,
-- 
use strict;my($i,$t,@r)=(0,'5 -.@BHJPT4acd6e2hk2lmn2o4r2s3tuz',map{ord}
split//,unpack('u*','L#`T&)QD5#0`#!!`#%1D)#08`#P05!!(3``$$"``#"0L&``('.
'"`P<!`````0$`'));$t=~s/(\d)(.)/$2x$1/eg;map{$t.=substr$t,$i,1,''while
$_--;$i++}@r;print"$t\n";# Thomas@Baetzler.de - http://baetzler.de/perl


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

Date: Tue, 09 Oct 2001 02:50:26 GMT
From: tim@vegeta.ath.cx (Tim Hammerquist)
Subject: Re: How to run perl under bash not sh
Message-Id: <slrn9s4qa2.5tl.tim@vegeta.ath.cx>

Me parece que Larry Alkoff <invalid@nowhere.com> dijo:
[ snippage ]
> Now I realize that this is a perl group but I find it very strange that
> perl is somehow using a version of sh that I can't even find on my system.
> Maybe it's built-in?

bash is just one of many *nix utils that behave differently depending on
how they're invoked.  vim also operates this way; in my $PATH there is
only one hard-linked "vim" executable.  Everything else is just a
symlink to it. So why I call gvim, it actually calls the "vim"
executable, but invokes the GUI because it was _invoked_ as 'gvim'.

bash works the same way.  it's the same executable, but it pretends it's
sh because that's how you called it.

> In any case my problem of how to use bash in my particular perl script
> has been solved although I am awaiting some help on debugging it.
> But there really should be an easy way in perl to ask it nicely to use
> whatever shell I want.

You've already been told of some work-arounds:

    system('/bin/bash', '-c', $COMMAND);

should work if you really must use your aliases.  I would recommend,
however, that you ween yourself off your precious aliases.

> Thanks All
> Larry

Tim Hammerquist
-- 
We are Pentium of Borg. Division is futile. You will be approximated.
    -- seen in someone's .signature


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

Date: Tue, 09 Oct 2001 03:25:32 GMT
From: Larry Alkoff <invalid@nowhere.com>
Subject: Re: How to run perl under bash not sh
Message-Id: <0mq4st04bl12bg4lemdv2hnacikmptso6a@4ax.com>

Tony Curtis <tony_curtis32@yahoo.com> wrote:

> >> On Mon, 08 Oct 2001 23:20:32 GMT,
> >> Larry Alkoff <invalid@nowhere.com> said:
> 
> > In my normal shell I type alias or al (my alias for
> > alias) and get alist of my aliases.
> 
> > I then type the command dkdkdk and get a bash: dkdkdk:
> > command not found error message.
> 
> > I then type the command sh which should be loading bash
> > via the soft link in /bin.  BTW, which shows /bin/sh -
> > the soft link.
> 
> > However ...
> 
> > When I type al I get the error msg sh: al: command not
> > found
> 
> Correct.  sh does not have aliases.  but bash does.

ok

> 
> > When I type alias I draw a complete blank indicating
> > that something knows about aliases but there aren't any
> > defined.
> 
> This is probably because under linux, sh is actually bash
> pretending to be "dumb".  Presumably it is accepting
> "alias" as a command because it's really bash, but won't
> let you actually define anything, because it's pretending
> to be a Bourne shell.

It would be nice if I could slocate a "sh" but I can't.
AFAIK the only sh on my system is a soft link to bash
but it sure doesn't act the same as bash.

> 
> > When I type my normal test command dkdkdk I get the
> > message sh: dkdkdk: command not found.
> 
> That's right, there are no aliases in sh, nor is there a
> standard command called "dkdkdk" (and there isn't one on
> your path either).
 
dkdkdk is my command that I _know_ doesn't exist!


> > Now I realize that this is a perl group but I find it
> > very strange that perl is somehow using a version of sh
> > that I can't even find on my system.  Maybe it's
> > built-in?
> 
> It's using /bin/sh.

But there ain't no /bin/sh except as a soft link to /bin/bash.

> 
> > In any case my problem of how to use bash in my
> > particular perl script has been solved although I am
> > awaiting some help on debugging it.  But there really
> > should be an easy way in perl to ask it nicely to use
> > whatever shell I want.
> 
> Already demonstrated:
> 
>     system '/bin/bash', '-c', ...
> 
> (if there are no shell meta-characters in a scalar
> argument to system() then no interpreter is involved.
> Unless you run your own like this with the LIST syntax).

I could not complete the above.
I'm just not familiar with the perl syntax.

 
> hth
> t

How about helping me with the exact syntax alluded to in this:

> But you can change:
>   system($command)
> to:
>   system('/bin/bash','-c',$command)
> (and so on).

which I interpreted as the following but it gives me syntax errors:

#!/usr/bin/perl -w
# lba: so  -  scrolled output as a perl script by David Efflandt

my $cmd=join" ",@ARGV;
system('/bin/bash','-c', '$cmd | less ');

but I can't get it to work :-(

I'd really like some syntax help here.  I'm not a newbie to compter stuff -
I've been doing it since 1975 in assembler and c  but I don't know perl - yet.


Thanks, Larry Alkoff N2LA



-- 
Larry Alkoff N2LA
My address is:  larryalk is_at mindspring dot com


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

Date: 09 Oct 2001 07:01:56 +0100
From: Andrew Gierth <andrew@erlenstar.demon.co.uk>
Subject: Re: How to run perl under bash not sh
Message-Id: <87d73xl5bf.fsf@erlenstar.demon.co.uk>

>>>>> "Abigail" == Abigail  <abigail@foad.org> writes:

 Abigail> Huh? The Bourne shell doesn't have aliases.

The "sh" program on a system that complies with the SUSv2 (or aspires to)
is required to support aliases.

-- 
Andrew.


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

Date: Tue, 9 Oct 2001 09:31:23 +0200
From: Leopold Toetsch <lt@toetsch.at>
Subject: Re: How to run perl under bash not sh
Message-Id: <slrn9s59ub.iv.lt@toetsch.at>

Tim Hammerquist <tim@vegeta.ath.cx> wrote:
> You've already been told of some work-arounds:

>     system('/bin/bash', '-c', $COMMAND);

> should work

Actually it needs -i too

expand_aliases is off for non-interactive shells.

perl -e'undef $/; open(S,q!/bin/bash -ic "ls-l"|!); $_=<S>; print;'

> Tim Hammerquist

HTH
leo
-- 
Leopold Toetsch <lt@toetsch.at> http://www.toetsch.at/linux/
-
You are at the west end of the Twopit Room.  There is a large hole in
the wall above the pit at this end of the room.


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

Date: Tue, 09 Oct 2001 03:37:06 GMT
From: clintp@geeksalad.org (Clinton A. Pierce)
Subject: Re: Illegal Instruction - out of the blue
Message-Id: <Chuw7.155685$K6.74705468@news2>

[Posted and mailed]

In article <3BC23771.9696D155@hotmail.com>,
	DrC <clarkvent@hotmail.com> writes:
> All of a sudden - out of the blue - all my Perl scripts are generating
> an "Illegal Instruction" error (or "Internal Server Error" in a
> browser). Nothing has changed to the server, and everything was working
> perfectly up until an hour ago.

ALL of your perl scripts?  As in:

	perl -e '1;'

(Or other small "test" scripts) is throwing a SIGILL? 

If that's the case then you've probably got a corrupted/altered copy of:

	perl (/usr/bin/perl)
	one of perl's libraries (libperl.so)
	one of the libraries that perl links to (libc, etc...)

And you've got something to go investigate.  I highly doubt that 
"Nothing has changed".  How about "I didn't change anything" or "As
far as I know, nothing has changed."  

Unix systems, even doing almost nothing, are always changing somehow.  
Show me a Unix system that's completely idle, and it's turned off.
[Wait!  There's always random bit-flipping due to cosmic radiation
and atomic decay!  Even at rest, it's still running!  Er..]


-- 
    Clinton A. Pierce            Teach Yourself Perl in 24 Hours  *and*
  clintp@geeksalad.org                Perl Developer's Dictionary
"If you rush a Miracle Man,     for details, see http://geeksalad.org     
	you get rotten Miracles." --Miracle Max, The Princess Bride


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

Date: 9 Oct 2001 03:39:59 GMT
From: damian@qimr.edu.au (Damian James)
Subject: Re: Multiplexing strings
Message-Id: <slrn9s4s6t.9mm.damian@puma.qimr.edu.au>

On 5 Oct 2001 15:35:04 GMT, Abigail said:
>
>#!/opt/perl/bin/perl -wl
>
>use strict;
>
>my $M = 2;
>
>print sub {
>     my $s = "";
>     {
>         $s .= shift @{$_ [0]};
>         push @_ => shift;
>         redo if @{$_ [0]};
>     }
>     $s} -> (map {[/.{$M}/g]} qw {aabbccddeeff AABBCCDDEEFF 001122334455});

I really like this loop. Maybe it's because I've been playing with
Lisp lately, maybe its just because spinning, whirly things are fun
to watch (even if you only run them in your head).

This reminds me of the machine for making wood veneers: the log is
rotated, the blade slices a continuous thin layer. Nice.

Cheers,
Damian
-- 
@:=grep!(m!$/|#!..$|),split//,<DATA>;@;=0..$#:;while($:=@;){$;=rand
$:--,@;[$;,$:]=@;[$:,$;]while$:;push@|,shift@;if$;[0]==@|;select$,,
$,,$,,1/80;print qq x\bxx((@;+@|)*$|++),@:[@|,@;],!@;&&$/}  __END__
Just another Perl Hacker,### http://home/pacific.net.au/~djames.hub


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

Date: 9 Oct 2001 02:37:26 -0700
From: webmaster@ukparents.co.uk (Richard)
Subject: Newbie basic question on splitting strings
Message-Id: <b9f5ddd3.0110090137.5179b397@posting.google.com>

Hi
I am a VB programmer who has inherited a series of complex Perl code
pages, and so far I have muddled my way through with everyhting I want
to do, but I have come unstuck with what I am sure i a really simple
query.  I'd be really grateful if someone could help me with it.  I
have a string which always has the same format:  numbers(up to 4 as
low as 1) followed directly (ie no spaces) by some text of an unknown
length.

eg:  123Wibble,  2332Wobble, 12Foobars  etc

Basically I need to access in a string for use in a PRINT statement
the numbers, so that I would have for example (as output):

The numbers are:  123,  or The Numbers are 2332 etc.

I can't tell what on earth I should be using to do this, I feel its
likely it should be a split and RegExp but I can't get my head round
the syntax.  Can anyone tell me the answer.

Many thanks in advance.

Richard


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

Date: Mon, 08 Oct 2001 23:10:12 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Obfuscation
Message-Id: <3BC26A94.708240D9@earthlink.net>

Tim from AZ wrote:
> Does anyone know of a decent program that I could use to make my perl
> code harder to decifer?

Acme::Bleach? Lingua::Romana::Perligata?

Regardless of what you do, though, somebody could just choose to call
your program using:
    perl -MO=Deparse yourscript.pl
and get an output script which closely resembles the original.

Also, Damien Conway hasn't yet written a deparser which will produce
perligata type output, so if you want a program which is written in perl
latin, you have to write it that way from scratch, and can't trivially
convert an existing script into it.

-- 
    "Just how stupid are you Kuno?"
    "Verily, Tatewaki Kuno knows no limits."


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

Date: Tue, 09 Oct 2001 05:06:36 GMT
From: "Tim from AZ" <tim_odomNOSPAM@NOSPAMhotmail.com>
Subject: Re: Obfuscation
Message-Id: <wBvw7.89327$W8.2428209@bgtnsc04-news.ops.worldnet.att.net>

I know it wouldn't be foolproof (*cause fools are so ingenius) but I just
need a deterrent.

Tim from AZ


"Tim from AZ" <tim_odomNOSPAM@NOSPAMhotmail.com> wrote in message
news:Iwjw7.43549$3d2.2417159@bgtnsc06-news.ops.worldnet.att.net...
> Does anyone know of a decent program that I could use to make my perl code
> harder to decifer?
>
>




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

Date: 9 Oct 2001 00:18:32 -0700
From: kimor79@hotmail.com (Kimo R.)
Subject: please check my script
Message-Id: <e5a5752b.0110082318.2275ab66@posting.google.com>

Hello, I am a beginner at perl so don't be too harsh please.

I have a server with a user file in the format:
user1 Password = "blah"
        some other info
user2 Password = "blah"
        some other info
etc...

I wrote a script to add users to the file from an html form. The
script checks to see if the username already exists then adds the
user:

#!/usr/bin/perl

print "Content-type: text/html\n\n";

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
        @pairs = split(/&/, $ENV{'QUERY_STRING'});
| elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
        read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
        @pairs = split(/&/, $buffer);
{ else {
        print "Content-type: text/html\n\n";
        print "<P>Use Post or Get";
}
# I copied this from a perl book

foreach $pair (@pairs) {
        ($key, $value) = split (/=/. $pair);
        $key =~ tr/+/ /;
        $key =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg;
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg;

        $value =~s/<!--(.|\n)*-->//g;

        if ($formdata[$key}) {
              $formdata{$key} .= ", $value";
        } else {
               $formdata{$key} = $value'
        }
}

$user = $formdata{'name'};
$passwd = $formdata{'passwd1'};

open(USER, "</user/file");
@users = <USER>;
close(USER);
# each line from the file is stored as an element in the @users array?
is there a way to just read the username and ignore everything else?

$match = 0;
for $username (@users) {
    if ($username =~ /^$user /) {
       $match++;
    }
}
# yes there's a space after ^$user, so if there's a user1, user12 can
be added

@users = "void";

if ($match eq 0) {
   open(FILE, ">>/user/file");
   flock(FILE, 2);
   print FILE "$user Password = $passwd\n\tsome other info\n";
   flock(FILE, 8);
   close(FILE);
   print "some html stuff";
} elsif ($match != 0) {
   print "Choose a different username";
}


The script works pretty much like I want it to. I just want to see if
there are easier (or better) ways of doing it and if I was missing
anything. Also how can I make sure the script is only accessed from my
html form? And another thing...

Each time the file is updated, the program has to restart. I have a
cron job that checks every 5 minutes to see if the file has been
update. I tried adding this in my perl script:

$program = "/shell/script/to/restart/program";
system ($program);

But it didn't run the shell script. any ideas?

One last thing...
How can I encrypt the value of $passwd before writing it to the file?

Thanks,
Kimo R.


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

Date: 9 Oct 2001 08:58:04 GMT
From: damian@qimr.edu.au (Damian James)
Subject: Re: please check my script
Message-Id: <slrn9s5era.he3.damian@puma.qimr.edu.au>

On 9 Oct 2001 00:18:32 -0700, Kimo R. said:
>Hello, I am a beginner at perl so don't be too harsh please.

I hope you don't take the following as harsh. When I refer to 'perldoc
something' below I mean that you should type that on a command line on
a machine with perl installed -- this a a way to access the online
documentation.

>I wrote a script to add users to the file from an html form. The
>script checks to see if the username already exists then adds the
>user:
>
>#!/usr/bin/perl
>

It is a very good idea to start all your Perl programs by enabling
warnings, and various restrictions on what perl will allow you to get
away with. 

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

You will find that many potential problems are thus solved before they
arise, and many common typos will be picked up instead of causing an
obscure bug.

[snip]
># I copied this from a perl book
[snip]

The book gave you bad (or out of date) advice: you are much better off
using the CGI module included with all recent perls. The entire snipped
section can be replaced with:

use CGI;
my $cgi_query = CGI->new();
my %formdata = map { $_, $cgi->param($_) } $cgi->param(); 

The last line is included only to populate the hash you are using
below. As you can see, it is hardly needed.

See 'perldoc CGI'

>$user = $formdata{'name'};
>$passwd = $formdata{'passwd1'};

'strict' will require you to declare these variables:

my $user   = $cgi->param('name');
my $passwd = $cgi->param('passwd1');

>open(USER, "</user/file");
>@users = <USER>;
>close(USER);
># each line from the file is stored as an element in the @users array?
>is there a way to just read the username and ignore everything else?

Yes -- there is no need to 'slurp' the file:

open USER, '/user/file' or die "Can't open /user/file: $!\n";
	# always check the return value of open()
my $match = 0;
$match++ if /^$user\b/o while <USER>;
close USER;

># yes there's a space after ^$user, so if there's a user1, user12 can
>be added

Note that '\b' in the regular expression above matches the 'word
boundary'. You will want to refer to the perlre man page ('perldoc
perlre').

>@users = "void";

No need to do this if you ensure that lexically scoped variables
(created with the my() comand) go out of scope when they are no longer
needed. See the perldata man page.

>if ($match eq 0) {

This is wrong: you are mathcing two numeric values with the string
equality operator -- and if you had enabled warnings, perl would have
complained bitterly. You probably mean to use '==' to check the
equality of two numeric values.

Instad, however, you might want to take advantage of the fact that 0,
undef and the empty string always evaluate as false, while everything
else evaluates as true:

if ($match) {
>   print "Choose a different username";
	exit;
}
else {
>   open(FILE, ">>/user/file");
>   flock(FILE, 2);
>   print FILE "$user Password = $passwd\n\tsome other info\n";
>   flock(FILE, 8);
>   close(FILE);
>   print "some html stuff";
}

>...
>The script works pretty much like I want it to. I just want to see if
>there are easier (or better) ways of doing it and if I was missing
>anything.

Yes -- see above. You should note that rolling your own CGI
implementation is fraught with danger, and there are all sorts of
security pitfalls that are hard to anticipate in advance. Using a
standard module gives you the advantage that it has been extensively
peer reviewed by the Perl community.

>...
>$program = "/shell/script/to/restart/program";
>system ($program);
>
>But it didn't run the shell script. any ideas?

You should try reporting errors:

system($program) and die "Can't run $program: $!\n"
# see 'perldoc -f system()' for why you need to say and instead of
# or.

>One last thing...
>How can I encrypt the value of $passwd before writing it to the file?
>

perldoc -f crypt();

You should familiarise yourself with the online documentation :-).

HTH,
Cheers,
Damian
-- 
@:=grep!(m!$/|#!..$|),split//,<DATA>;@;=0..$#:;while($:=@;){$;=rand
$:--,@;[$;,$:]=@;[$:,$;]while$:;push@|,shift@;if$;[0]==@|;select$,,
$,,$,,1/80;print qq x\bxx((@;+@|)*$|++),@:[@|,@;],!@;&&$/}  __END__
Just another Perl Hacker,### http://home.pacific.net.au/~djames.hub


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

Date: 9 Oct 2001 02:31:37 -0700
From: javatasse@yahoo.com (JJ)
Subject: Regular expression whole string matching
Message-Id: <b2e92ea9.0110090131.4cf46f6f@posting.google.com>

How do I check if a regular expression matches the whole string, like
checking an email address.
Thanks


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

Date: Tue, 9 Oct 2001 11:35:40 +0200
From: "Markus Dehmann" <markus.cl@gmx.de>
Subject: Re: Regular expression whole string matching
Message-Id: <9pughv$kjdlg$1@ID-101658.news.dfncis.de>

"JJ" <javatasse@yahoo.com> wrote in message
news:b2e92ea9.0110090131.4cf46f6f@posting.google.com...

> How do I check if a regular expression matches the whole string, like
> checking an email address.
> Thanks

You should use ^ to mark the beginning and $ to mark the end of the string.
Like this (not tested):

$mail = "me@me.com";
if( $mail =~ m/^[^@]+\@[^\.]+\.[a-z]+$/ ){
    print "vaild";
}

The string must begin with the not-@ and must end with [a-z]. There is
nothing allowed to be in front of this or behind this.






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

Date: Tue, 09 Oct 2001 11:38:11 +0200
From: David Bouman <david.bouman@nl.xo.com>
Subject: Shouldn't /c preserve pos() when matching in list context? (Was: Re:  What good is "\G" ...)
Message-Id: <3BC2C583.36D0D89F@nl.xo.com>

Joe Schaefer wrote:

> ... another is that your regexps destroy pos() upon failure.

Ah! this reminds me of something I noticed about /c:

  perl5 -le '$_="12345a"; 1 while m/\d/cg; print pos'

outputs 5 as expected, whereas something like:

  perl5 -le '$_="12345a"; @d = m/\d/cg; print pos'

doesn't. In fact pos() is undefined just as if /c was never there..

It's same in both perl 5.005_03 & 5.6.0 but it doesn't make sense
to me.

--
David.


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

Date: Tue, 09 Oct 2001 03:42:29 GMT
From: clintp@geeksalad.org (Clinton A. Pierce)
Subject: Re: size of an object?
Message-Id: <Fmuw7.155701$K6.74713290@news2>

[Posted and mailed]

In article <9pt1en$5lhmg6@news1s.iddeo2.es>,
	fxn@retemail.es (F. Xavier Noria) writes:
> We have a structure consisting of a reference to a hash, whose values
> can be references to hashes, references to arrays, or scalars, and
> this follows down for some levels. (Is the object that constructs the
> module XML::Simple if you know it.)
> 
> Is there a way to know its size in memory?

Are you just looking for a rough guess?  This usually works:

	1. Note process size.
	2. Create lots of instances of the structure.
	   If you copy, beware of deep-copy of references issues
	3. Note process size.
	4. Divide the difference in the process sizes by the number of
           copies.  There's your rough estimate.

Remember that perl will grab memory in "chunks" which will exceed what
it actually needs.  Do this a couple of times for different values 
of "lots" to smooth out your estimate.

-- 
    Clinton A. Pierce            Teach Yourself Perl in 24 Hours  *and*
  clintp@geeksalad.org                Perl Developer's Dictionary
"If you rush a Miracle Man,     for details, see http://geeksalad.org     
	you get rotten Miracles." --Miracle Max, The Princess Bride


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

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


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