[22243] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4464 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jan 25 00:10:36 2003

Date: Fri, 24 Jan 2003 21:05:06 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 24 Jan 2003     Volume: 10 Number: 4464

Today's topics:
        Global <ntk00@hotmail.com>
    Re: IO::Socket - timeout not working <spam@thecouch.homeip.net>
    Re: Is there a better way to format numbers to use comm <usenet@tinita.de>
    Re: Is there a better way to format numbers to use comm <dragnet@internalysis.com>
    Re: Perl & CGI Security <GPatnude@adelphia.net>
    Re: Perl & CGI Security <uri@stemsystems.com>
        Split question <tombrooks.nospam@hotmail.com>
    Re: Split question <lockner@cse.psu.edu>
    Re: Split question <bongie@gmx.net>
    Re: Split question (Tad McClellan)
    Re: Split question <jurgenex@hotmail.com>
    Re: Split question <jurgenex@hotmail.com>
        Splitting a configuration file into a hash (Kevin Newman)
    Re: Splitting a configuration file into a hash <uri@stemsystems.com>
    Re: Splitting a configuration file into a hash (Tad McClellan)
    Re: Splitting a configuration file into a hash <bongie@gmx.net>
    Re: Splitting a configuration file into a hash <uri@stemsystems.com>
    Re: using expect <tombrooks.nospam@hotmail.com>
    Re: using expect (Charles DeRykus)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sat, 25 Jan 2003 03:55:21 GMT
From: "webmaster" <ntk00@hotmail.com>
Subject: Global
Message-Id: <JUnY9.108614$Yo4.5315126@news1.calgary.shaw.ca>

Hello,      I'm a beggining perl programmer and have a perl script that has
this segment of code:

BEGIN {
   $Global::Some_Variable= 1;

   [snip]
}

What is this package $Global? Search all over and can't find any
documentation on it. Where is it defined

Thanks
NK










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

Date: Fri, 24 Jan 2003 22:00:41 -0500
From: Mina Naguib <spam@thecouch.homeip.net>
Subject: Re: IO::Socket - timeout not working
Message-Id: <m5nY9.16559$IL2.205140@weber.videotron.net>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1



zen13097@zen.co.uk.invalid wrote:
| I'm having problems geting timeouts working with IO::Socket.
| Take the sample code below - run it, then telnet to port 9999 on the
| same machine. The code prints a 'Connected' message, sets a 5 second
| timeout, and tries to read some data from the socket.
|
| What's happening is that the timeout is being triggered (I'm seeing the
| "[timeout]" message from the signal handler) but the recv() call isn't
| exiting until it has received some data.
|
| Am I doing something wrong?
|
| (Perl V5.6.1, Linux)

Your syntax is correct, however the logic isn't.  This little (untested)
example should give you an idea of how a timeout should work:

- -cut here-
print "Type here: ";
eval {
~        $SIG{ALRM} = sub {print " . . Timed out . ."; die;};
~        alarm(5);
~        $data = <>;
};
print "\nOk\n";
- -cut here-

Best of luck.


-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE+Mf3ZeS99pGMif6wRAkpFAKD0JHRYQe7zMe3s6KVQ+l/lQmNg0wCfanza
vQYT7bjztzqy9GVxNBmUOZs=
=SKiA
-----END PGP SIGNATURE-----



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

Date: 25 Jan 2003 03:43:04 GMT
From: Tina Mueller <usenet@tinita.de>
Subject: Re: Is there a better way to format numbers to use commas?
Message-Id: <tinh993pj$3lu$tina@news01.tinita.de>

Marc Bissonnette <dragnet@internalysis.com> wrote:

> Many thanks to you, Michael and Uri for helping me understand this.

> I'm just moving into learning / using more of the 'symbolic' (probably 
> bad term) use of perl,

'idiomatic' maybe?

hth, tina

-- 
http://www.tinita.de/        \  enter__| |__the___ _ _ ___
http://Movies.tinita.de/      \     / _` / _ \/ _ \ '_(_-< of
http://PerlQuotes.tinita.de/   \    \ _,_\ __/\ __/_| /__/ perception


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

Date: Sat, 25 Jan 2003 04:39:44 GMT
From: Marc Bissonnette <dragnet@internalysis.com>
Subject: Re: Is there a better way to format numbers to use commas?
Message-Id: <Xns930DF15A0B385dragnetinternalysisc@207.35.177.134>

Tina Mueller <usenet@tinita.de> wrote in news:tinh993pj$3lu
$tina@news01.tinita.de:

> Marc Bissonnette <dragnet@internalysis.com> wrote:
> 
>> Many thanks to you, Michael and Uri for helping me understand this.
> 
>> I'm just moving into learning / using more of the 'symbolic' (probably 
>> bad term) use of perl,
> 
> 'idiomatic' maybe?

LOL ok, well, yes, that would be more appropriate :) I'm just used to being 
able to read the bulk of the code as more-or-less an english-type subset 
and understand the function right away. Obviously, this means much of my 
code is longer and not as efficient as many of the regulars in this NG, but 
I'm getting there, slowly but surely :)

-- 
Marc Bissonnette
Perl CGI. MySQL Databases. Dynamic Web Content Control.
http://www.internalysis.com
Looking for a new ISP? http://www.canadianisp.com


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

Date: Sat, 25 Jan 2003 03:50:01 GMT
From: "codeWarrior" <GPatnude@adelphia.net>
Subject: Re: Perl & CGI Security
Message-Id: <JPnY9.2192$ni5.904883@news1.news.adelphia.net>


"Tad McClellan" <tadmc@augustmail.com> wrote in message
news:slrnb33m1v.g70.tadmc@magna.augustmail.com...
> Tintin <me@privacy.net> wrote:
> > "codeWarrior" <GPatnude@adelphia.net> wrote in message
> > news:GCcY9.1169$ni5.718675@news1.news.adelphia.net...
> >> "Alex Banks" <alex@alexbanks.com> wrote in message
> >> news:3e3110c9$0$232$cc9e4d1f@news.dial.pipex.com...
>
> >> > Would be grateful if someone could recommend an up-to-date security
> > guide
> >> > for CGI Perl.
> >>
> >> Look into SSL....
> >
> > Insecure Perl CGI scripts are still insecure running over HTTPS
>
>
> But... but... but one of the "S"s in SSL is for "Secure",
> so using SSL must be in the same universe of discourse.
>
> That's all it takes for the Warrior to offer his "knowledgable" "help".
>
> Isn't there a TFM somewhere that you should be reading smartie pants?
>
>
> --
>     Tad McClellan                          SGML consulting
>     tadmc@augustmail.com                   Perl programming
>     Fort Worth, Texas

I still can't think of a reason why anyone would ever want to eval user
input or make system calls using user input through CGI --






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

Date: Sat, 25 Jan 2003 04:00:21 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Perl & CGI Security
Message-Id: <x7u1fxkhey.fsf@mail.sysarch.com>

>>>>> "c" == codeWarrior  <GPatnude@adelphia.net> writes:

  c> I still can't think of a reason why anyone would ever want to eval
  c> user input or make system calls using user input through CGI --

then you don't have much imagination. plenty of external programs are
run under cgi all over the place. constraining which ones are run and
with what arguments is a serious security issue. and it has nothing to
do with ssl. ssl deals with communications and not cgi.

as for eval, it is rarely needed in general. most uses you see in this
group are not good ones. eval is for when you have no other cleaner and
safer way to do something in perl. generating specialized code is one of
the few legimate uses and not for newbies. 

now, please choose a more appropriate nick name. my IRC rule is that the
smarter the nick, the dumber the person. :)

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org
Damian Conway Perl Classes - January 2003 -- http://www.stemsystems.com/class


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

Date: Fri, 24 Jan 2003 22:35:51 -0500
From: "Thomas Brooks" <tombrooks.nospam@hotmail.com>
Subject: Split question
Message-Id: <NLudnb_Hi8uKm6-jXTWc3Q@comcast.com>

I am doing some splitting in perl
--------------------------------------
$data = "domain-user";

@fields = split /-/, $data;

print "domain is $fields[0]\n";
print "username is $fields[1]\n";
--------------------------------------

As you see it is simply splitting the input where the left is the domain and
the right is the username. My only problem is if a user wants a dash in his
or her username.

So if $data = "domain-ima-user";

Then I will have fields[2], which I don't want. How can I fix this so it
only splits once, at the first dash found?





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

Date: Fri, 24 Jan 2003 22:43:23 -0500
From: Matthew Lockner <lockner@cse.psu.edu>
Subject: Re: Split question
Message-Id: <b0t14r$16fm$1@f04n12.cac.psu.edu>

Thomas Brooks wrote:

> I am doing some splitting in perl
> --------------------------------------
> $data = "domain-user";
> 
> @fields = split /-/, $data;
> 
> print "domain is $fields[0]\n";
> print "username is $fields[1]\n";
> --------------------------------------
> 
> As you see it is simply splitting the input where the left is the domain
> and the right is the username. My only problem is if a user wants a dash
> in his or her username.
> 
> So if $data = "domain-ima-user";
> 
> Then I will have fields[2], which I don't want. How can I fix this so it
> only splits once, at the first dash found?

After the split call:

my $domain = shift @fields;
my $user = join '-', @fields;

Terribly naive approach, but TMTOWTDI, I'm sure...



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

Date: Sat, 25 Jan 2003 05:03:20 +0100
From: "Harald H.-J. Bongartz" <bongie@gmx.net>
Subject: Re: Split question
Message-Id: <1155130.GvCFi5FrOz@nyoga.dubu.de>

Thomas Brooks wrote:

> I am doing some splitting in perl

Please don't multi-post, use the cross-posting feature of your news
client.

See my answer
        @fields = split /-/, $data, 2;
also in alt.perl.

Ciao,
        Harald
-- 
Harald H.-J. Bongartz <bongie@gmx.net>
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
A World Without Strings Is Chaos.
                                -- R. Smuntz



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

Date: Fri, 24 Jan 2003 22:04:11 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Split question
Message-Id: <slrnb3435r.gti.tadmc@magna.augustmail.com>

Thomas Brooks <tombrooks.nospam@hotmail.com> wrote:
> I am doing some splitting in perl
> --------------------------------------
> $data = "domain-user";
> 
> @fields = split /-/, $data;
> 
> print "domain is $fields[0]\n";
> print "username is $fields[1]\n";
> --------------------------------------
> 
> As you see it is simply splitting the input where the left is the domain and
> the right is the username. My only problem is if a user wants a dash in his
> or her username.
> 
> So if $data = "domain-ima-user";
> 
> Then I will have fields[2], which I don't want. How can I fix this so it
> only splits once, at the first dash found?


Use m// in a list context instead of split:

   my @fields = $data =~ /(.*?)-(.*)/;

or, give split a 3rd argument (you did read "perldoc -f split"
before posting, didn't you?):

   my @fields = split /-/, $data, 2;


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


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

Date: Sat, 25 Jan 2003 04:56:18 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Split question
Message-Id: <SNoY9.265$Ec.129@nwrddc02.gnilink.net>

Thomas Brooks wrote:
> I am doing some splitting in perl
> --------------------------------------

What the F**** ?
I just answered your very same question in the other NG.
Would you please stop multi-posting?

jue




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

Date: Sat, 25 Jan 2003 05:01:23 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Split question
Message-Id: <DSoY9.33$3E6.26@nwrddc01.gnilink.net>

Harald H.-J. Bongartz wrote:
> Thomas Brooks wrote:
>> I am doing some splitting in perl
> Please don't multi-post, use the cross-posting feature of your news
> client. [...]
> also in alt.perl.

Ahhh, that would be NG number 3.
I was ranting about comp.lang.perl.

Let's see how many more will pop up.

jue




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

Date: 24 Jan 2003 19:02:38 -0800
From: knewman00@earthlink.net (Kevin Newman)
Subject: Splitting a configuration file into a hash
Message-Id: <4c8e4398.0301241902.2f9c7229@posting.google.com>

Hi all,

I'm attempting to optimize/shorten a split statement that acts on a
configuration file to put the values into a hash.  The format of the
configuration file is:

key1a=value1a | key1b=value1b | key1c=value1c
key2a=value2a | key2b=value2b | key2c=value2c

etc...

Here is the current statement that works okay, but I'm sure that there
is a simpler why to do this:

	my @config  = (split /\|/, $record[$i]) ;
	
	foreach my $thing (@config) {
		my ($key, $value) = split (/\s*=\s*/,$thing,2) ;
		$config{$key} = $value;
	}


Any suggestions on how to improve this code?

Thanks,

kln


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

Date: Sat, 25 Jan 2003 03:45:22 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Splitting a configuration file into a hash
Message-Id: <x765sdlwoe.fsf@mail.sysarch.com>

>>>>> "KN" == Kevin Newman <knewman00@earthlink.net> writes:

  KN> I'm attempting to optimize/shorten a split statement that acts on a
  KN> configuration file to put the values into a hash.  The format of the
  KN> configuration file is:

  KN> key1a=value1a | key1b=value1b | key1c=value1c
  KN> key2a=value2a | key2b=value2b | key2c=value2c

please give a very clear definition of those keys and values. are the
keys single word tokens (\w+)? are the values also single strings
without spaces? (\S+)? are there spaces around all the | chars? if those
constraints are solid then you can do some neat tricks. you can slurp
the whole file in (choose a method for that) and then:

	my %config = $file_text =~ /(\w+)=(\S+)/g ;

done.

if some of those constraints aren't solid, then you have to do more work.

  KN> Here is the current statement that works okay, but I'm sure that there
  KN> is a simpler why to do this:

  KN> 	my @config  = (split /\|/, $record[$i]) ;

don't use array indexing unless you really need to. a while/for loop
would be simpler and faster. depending on the loop construct you can
also do neat tricks. 

also there is no need for the () around the whole split expression. the
@config provides list context all by itself.


	
  KN> 	foreach my $thing (@config) {

you don't need the temp var @config either. just use the split
expression:

	foreach my $thing (split /\|/, $record[$i]) {


  KN> 		my ($key, $value) = split (/\s*=\s*/,$thing,2) ;

that split expression tells me more about the config format. but you
don't need the 2 there as you only have 2 elements on the left and split
shouldn't find more than 2 parts if your format is correct.

  KN> 		$config{$key} = $value;
  KN> 	}

let's now assume a line by line read loop and the lesser contraints as
shown above. (you still need to nail down the config format specs.)

	while( my $conf_line = <CONF> ) {

		foreach my $key_val (split /\|/, $conf_line ) {

			my ($key, $value) = split( /\s*=\s*/, $key_val ) ;
			$config{$key} = $value;
		}
	}

but if the key=val has some constraints like no spaces (you seem to
allow spaces around the =) then you can simplify it as above:


	while( my $conf_line = <CONF> ) {

		while( $conf_line =~ /(\S+)\s*=\s*(\S+)/ ) {
			$config{$1} = $2 ;
		}
	}

so as you can see you must specify your config (or other) formats very
carefully. the tighter the specs, the easier it is to parse out the
key/value pairs. just showing examples is not the same as a proper
specification.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org
Damian Conway Perl Classes - January 2003 -- http://www.stemsystems.com/class


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

Date: Fri, 24 Jan 2003 21:56:04 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Splitting a configuration file into a hash
Message-Id: <slrnb342mk.gpc.tadmc@magna.augustmail.com>

Kevin Newman <knewman00@earthlink.net> wrote:
> 
> I'm attempting to optimize/shorten a split statement that acts on a

[snip data, repeated below]

> Here is the current statement that works okay, but I'm sure that there
> is a simpler why to do this:
> 
> 	my @config  = (split /\|/, $record[$i]) ;


Do you really want space characters at the start of some of the keys
and at the end of some of the values?


> 	foreach my $thing (@config) {
> 		my ($key, $value) = split (/\s*=\s*/,$thing,2) ;
                                            ^^^ ^^^
                                            ^^^ ^^^

My guess is you've gotten those flipped around with the split pattern. 

Is this your real code?


> 		$config{$key} = $value;
> 	}
> 
> 
> Any suggestions on how to improve this code?


This in not "optimized" nor "simpler" nor "improved" but
it _is_ shorter.  :-)

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

my %config;
while ( <DATA> ) {
   chomp;
   my %h = /(.*?)=(.*?)(?:\||$)/g;   # m//g in list context
   @config{keys %h} = values %h;     # hash slice
}

print "$_ ==> $config{$_}\n" for sort keys %config;;

__DATA__
key1a=value1a | key1b=value1b | key1c=value1c
key2a=value2a | key2b=value2b | key2c=value2c
-------------------------


But we're not playing golf here...

I like your way much better. You for sure could eliminate the
temporary @config array. You might want to use m// in list
context instead of split. Different guts for the while above:

-------------------------
       chomp;
       foreach my $thing ( split /\|/ ) {
               my ($key, $value) = $thing =~ /(.*?)=(.*)/;
               $config{$key} = $value;
       }
-------------------------


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


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

Date: Sat, 25 Jan 2003 04:58:17 +0100
From: "Harald H.-J. Bongartz" <bongie@gmx.net>
Subject: Re: Splitting a configuration file into a hash
Message-Id: <1156399.HRJby8oCEs@nyoga.dubu.de>

Uri Guttman wrote:
>>>>>> "KN" == Kevin Newman <knewman00@earthlink.net> writes:
>   KN>                 my ($key, $value) = split (/\s*=\s*/,$thing,2) ;
> 
> that split expression tells me more about the config format. but you
> don't need the 2 there as you only have 2 elements on the left and
> split shouldn't find more than 2 parts if your format is correct.

When I saw this line, I thought that Kevin introduced the '2' to allow
values that contain a '=' themselves, like

        key = value=1

But of course, we don't have a spec ...

> while( $conf_line =~ /(\S+)\s*=\s*(\S+)/ ) {
                                         ^ /g
Won't work without the /g, IMHO.

Ciao,
        Harald
-- 
Harald H.-J. Bongartz <bongie@gmx.net>
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
How do you tell an extrovert computer scientist?
He looks at *your* shoes when he talks to you.


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

Date: Sat, 25 Jan 2003 04:05:32 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Splitting a configuration file into a hash
Message-Id: <x7ptqlkh6c.fsf@mail.sysarch.com>

>>>>> "HH-B" == Harald H -J Bongartz <bongie@gmx.net> writes:

  HH-B> Uri Guttman wrote:

  >> while( $conf_line =~ /(\S+)\s*=\s*(\S+)/ ) {
  HH-B>                                          ^ /g
  HH-B> Won't work without the /g, IMHO.

my mind wrote the /g by my fingers never typed it.

:)

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org
Damian Conway Perl Classes - January 2003 -- http://www.stemsystems.com/class


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

Date: Fri, 24 Jan 2003 22:16:11 -0500
From: "Thomas Brooks" <tombrooks.nospam@hotmail.com>
Subject: Re: using expect
Message-Id: <FYSdnUsaGY_hnK-jXTWc3A@comcast.com>

I finally figured it out. I used carriage returns (/r), but I needed a sleep
1 after the spawn... that did the trick.


"Thomas Brooks" <tombrooks.nospam@hotmail.com> wrote in message
news:g-OcnRPj8rnJFbajXTWcqA@comcast.com...
> I am trying to use expect to run passwd to change a user password. Not
> trying to run this via WWW... just to automate a script that I execute as
> root.
>
> Here is what I have:
> =====================
> #!/usr/bin/perl -w
> use Expect;
>
> $account  = $ARGV[0];
> $password = $ARGV[1];
>
> $setpwd = Expect->spawn("/usr/bin/passwd $account");
>
> $setpwd->expect(undef, "New password: ");
>
> print $setpwd "$password\n";
>
> $setpwd->expect(undef, "Retype new password: ");
>
> print $setpwd "$password\r";
>
> $setpwd->soft_close();
>
>
>
> and this is what happens:
> ============================
> [root@secure tmp]# perl test6.pl testuser testpw
> Changing password for user testuser
> New password: testpw
>
>
>
> and it just sits there.. does nothing. Any ideas? redhat 7.2
>
> thanks
>
>
>




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

Date: Sat, 25 Jan 2003 03:51:26 GMT
From: ced@bcstec.ca.boeing.com (Charles DeRykus)
Subject: Re: using expect
Message-Id: <H9941q.n@news.boeing.com>

In article <g-OcnRPj8rnJFbajXTWcqA@comcast.com>,
Thomas Brooks <tombrooks.nospam@hotmail.com> wrote:
>...
>...
>and it just sits there.. does nothing. Any ideas? redhat 7.2
>

May not help but you might want to check debug/error returns
for clues... ,eg, 

 $Expect::Debug=3;
 ..
 $prompt = 'Re-enter new password:';
 ($match,$error) = $spawn->expect(10, $prompt);
 die "Never got: '$prompt' on  $host, $error" if $error;
 ...

--
Charles DeRykus 


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

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


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