[18402] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 570 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Mar 26 18:06:25 2001

Date: Mon, 26 Mar 2001 15:05:25 -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: <985647924-v10-i570@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Mon, 26 Mar 2001     Volume: 10 Number: 570

Today's topics:
    Re: Basic socket logic (server) discussion... <lafondd@sympatico.ca>
        can @ENV be reset in the script? <bing-du@tamu.edu>
        DBI AS/400? (Jamie)
    Re: Help with reading between two points in a file. <stephenk@cc.gatech.edu>
        Help: perl <mcao@u.washington.edu>
    Re: Help: perl <klacquement@syscor.com>
    Re: Help: perl (Tad McClellan)
    Re: How adding Hours & Days to current date? (Steve Lamb)
        How can I avoid long 'switch' statement? <djmarcus@ex-pressnet.com>
    Re: How can I avoid long 'switch' statement? <bart.lateur@skynet.be>
    Re: How can I avoid long 'switch' statement? <djmarcus@ex-pressnet.com>
    Re: How can I avoid long 'switch' statement? (Jay Tilton)
    Re: How can I avoid long 'switch' statement? <bart.lateur@skynet.be>
        HUP signal handling <N_O_S_P_A_M_syn_uw@hotmail.com>
        LISA 2001 Call for Papers <becca@usenix.org>
        LWP help <mail@ericmarques.net>
    Re: Multiple perl versions on same machine? (Andy Dougherty)
    Re: Newbie <nospam@email.com>
        one-liner assignment from regex <kimmfc@mydeja.com>
    Re: one-liner assignment from regex (John Joseph Trammell)
    Re: one-liner assignment from regex <klacquement@syscor.com>
    Re: one-liner assignment from regex <bigrich318@yahoo.com>
    Re: one-liner assignment from regex (Tad McClellan)
    Re: one-liner assignment from regex <bart.lateur@skynet.be>
    Re: Regex question (Charles DeRykus)
        Sockets <SiStie@nuclear-network.com>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Mon, 26 Mar 2001 22:50:15 GMT
From: "wdstaff" <lafondd@sympatico.ca>
Subject: Re: Basic socket logic (server) discussion...
Message-Id: <HIPv6.467622$f36.14654672@news20.bellglobal.com>

>
> W, why can't you send data to a socket while it's taking input?
>
> Chris
>
> --

Well, i tough that socket were blocked when they are utilize...
So, if i get this right, i could "send & receive" at the same time on the
same socket?
thx for your nice reply, cant stand freaks that denigrate peoples.





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

Date: Mon, 26 Mar 2001 15:06:31 -0600
From: Bing Du <bing-du@tamu.edu>
Subject: can @ENV be reset in the script?
Message-Id: <3ABFAF57.A9EC50F1@tamu.edu>

If I set LD_LIBRARY_PATH=/public/lib from the command line and export it
(I use zsh), the following script works fine.

#!/public/bin/perl
use SOAP::Lite;
print "success";

But if I set $ENV{'LD_LIBRARY_PATH'}="/public/lib" in the above script,
I got the error:

Can't load
'/public/lib/perl5/site_perl/5.005/sun4-solaris/auto/XML/Parser/Expat
/Expat.so' for module XML::Parser::Expat: ld.so.1: /public/bin/perl:
fatal: libe
xpat.so.0: open failed: No such file or directory at
/public/lib/perl5/5.00501/s
un4-solaris/DynaLoader.pm line 168.

 at /public/lib/perl5/site_perl/5.005/sun4-solaris/XML/Parser.pm line 15

BEGIN failed--compilation aborted at
/public/lib/perl5/site_perl/5.005/sun4-sola
ris/XML/Parser.pm line 19.
BEGIN failed--compilation aborted at
/public/lib/perl5/site_perl/5.005/SOAP/Lite
 .pm line 739.
BEGIN failed--compilation aborted at ./z.cgi line 6.

How should I get 'LD_LIBRARY_PATH' set in the script?

Thanks,

Bing



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

Date: Mon, 26 Mar 2001 20:06:24 GMT
From: jrdarkiii@rhtc.net (Jamie)
Subject: DBI AS/400?
Message-Id: <3abfa001.642799@nntp.rhtc.net>

I'll try not to be too vague...Here goes...Anyone had any experience
getting mainframe data on the web for businesses??  It seems there are
alot of AS/400's out there...Someone in the know let me in on how they
would approach this...DBI and CORBA and an application server...Does
this mean using Apache on a separate machine with DBI talking/
connecting with an ORB on the AS/400?  Using CORBA sounds like a job
for JAVA...  If anyone can fill me in with any links on this subject
or real world experience I'd appreciate it...Also, if there are any
hints on security, etc...I'd like to know where to start with all of
this...


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

Date: Mon, 26 Mar 2001 14:37:37 -0500
From: Stephen Kloder <stephenk@cc.gatech.edu>
Subject: Re: Help with reading between two points in a file.
Message-Id: <3ABF9A80.1A76D567@cc.gatech.edu>

Unknown wrote:
> 
> I new to Perl so forgive my inorance,
> I need to create a script that can read through a file and print all
> entries between two date stamps. The date/time stamps reads as like this
> Feb 7 02:02:02 [stuff].
> the file lists in desending order (newer items towards the bottom).
> With Perl how can I list all lines between two time stamps?

Check out the range operator:
while(<>) {
  print if /$start/ .. /$end/;
}

perldoc perlop
HTH
-- 
Stephen Kloder               |   "I say what it occurs to me to say.  
stephenk@cc.gatech.edu       |      More I cannot say." 
Phone 404-874-6584           |   -- The Man in the Shack
ICQ #65153895                |            be :- think.


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

Date: Mon, 26 Mar 2001 12:50:44 -0800
From: "M. Cao" <mcao@u.washington.edu>
Subject: Help: perl
Message-Id: <Pine.A41.4.33.0103261247500.62746-100000@homer38.u.washington.edu>


Hi,
I am new in perl, could you help me to fix the script below in perl.
I use " << EOF " in ksh, but it not worked in perl .. Thanks


use Getopt::Std;
getopt('f:l:u:m:');

if ( $opt_m ) {
/usr/bin/mailx -s "New Nestx Account" $opt_m << EOF
Create new account for $opt_f, uid = $opt_u
EOF;
}



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

Date: 26 Mar 2001 14:10:31 -0800
From: Kevin Lacquement <klacquement@syscor.com>
Subject: Re: Help: perl
Message-Id: <m2lmpsgp0o.fsf@aridhol.syscor.priv>

>>>>> "MC" == M Cao <mcao@u.washington.edu> writes:

    MC> Hi,
    MC> I am new in perl, could you help me to fix the script below in perl.
    MC> I use " << EOF " in ksh, but it not worked in perl .. Thanks

    MC> use Getopt::Std;
    MC> getopt('f:l:u:m:');

    MC> if ( $opt_m ) {
    MC> /usr/bin/mailx -s "New Nestx Account" $opt_m << EOF
                                                       ^
                              I think you need to lose the space here.

    MC> Create new account for $opt_f, uid = $opt_u
    MC> EOF;
    MC> }

Cheers,
Kevin

-- 
He that breaks a thing to find out what it is has left the path of
wisdom                                         -- Gandalf the Grey


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

Date: Mon, 26 Mar 2001 22:29:03 GMT
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Help: perl
Message-Id: <slrn9bvdev.9cd.tadmc@tadmc26.august.net>


[ Your Subject: header sucks. Are you too pressed for time to put
  the subject of your post in the Subject of your post?
]


M. Cao <mcao@u.washington.edu> wrote:
>
>I am new in perl, could you help me to fix the script below in perl.


Sure.


>I use " << EOF " in ksh, but it not worked in perl .. Thanks


They work fine in Perl, search for "here-doc" in perldata.pod.

Your primary problem is not how to quote strings (which is what
here-docs do). You appear to want to call an external program.
This Perl Frequently Asked Question reviews the ways of calling
external programs from Perl:

   "How can I capture STDERR from an external command?"


>use Getopt::Std;
>getopt('f:l:u:m:');
>
>if ( $opt_m ) {
>/usr/bin/mailx -s "New Nestx Account" $opt_m << EOF
>Create new account for $opt_f, uid = $opt_u
>EOF;
>}


You could use the system() function (note that the here-doc below
is a _shell_ here-doc, not a Perl one):


   !system qq(/usr/bin/mailx -s "New Nestx Account" $opt_m << EOF
Create new account for $opt_f, uid = $opt_u
EOF
) or die "problem running mailx";


But that is altogether too homely to look upon. Let's make our own
dog food a bit more palatable (UNTESTED):


   my $cmd = qq(/usr/bin/mailx -s "New Nestx Account" $opt_m )
           . qq("Create new account for $opt_f, uid = $opt_u");

   print "==>$cmd<==\n" if $debug;
   !system($cmd) or die "problem running mailx";


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


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

Date: Mon, 26 Mar 2001 20:52:23 -0000
From: grey@despair.rpglink.com (Steve Lamb)
Subject: Re: How adding Hours & Days to current date?
Message-Id: <slrn9bvb06.ien.grey@teleute.dmiyu.org>

On 24 Mar 2001 02:38:36 -0600, Logan Shaw <logan@cs.utexas.edu> wrote:
>If you're going to do it manually, you also need to take into account
>leap days and daylight savings time.  You didn't give the numbers for
>those.  Personally, I think it's better to use a module that has all
>that worked out already.

    Uhm, why?  If you want to know something 12 days in advance you just tack
on 12 days.  locatime() takes care of the rest AFAIK.

-- 
         Steve C. Lamb         | I'm your priest, I'm your shrink, I'm your
         ICQ: 5107343          | main connection to the switchboard of souls.
-------------------------------+---------------------------------------------


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

Date: Mon, 26 Mar 2001 14:08:27 -0500
From: "David J. Marcus" <djmarcus@ex-pressnet.com>
Subject: How can I avoid long 'switch' statement?
Message-Id: <tbv4t1o02baqf1@corp.supernews.com>

<<< inquiry also posted at comp.lang.perl.moderated>>>

Hi

I have many modules each with a common entry point. For discussion's sake:
    module A has A::func();
    module B has B::func();
    etc.

Based on input, $inp (where $inp is set to 'A', or 'B', etc), I want to call
the proper module's function.

The only way I can see of doing it is some form of a switch statement:
    if ($inp eq 'A')
        {    require A;
             A::func(...parms...);
        }
    elsif ($inp eq 'B')
        {    require B;
             B::func(...parms...);
        }
    ...etc.

The list of modules is quite long.

What I would like to do is invoke the function a-la:
    {    require $inp;
         $inp :: func(...parms...);
    }

Perl complains about this syntax.

I've considered using objects, but the modules are not really related. Nor
do I see how objects can solve the problem of not wanting to explicitly
enumerate the modules.

Is there a correct syntax for what I'm trying to do? Is there any way of
avoiding the long explicit enumeration of the modules?

-TIA
David





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

Date: Mon, 26 Mar 2001 19:15:34 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: How can I avoid long 'switch' statement?
Message-Id: <545vbts83jvcrbfqa3d5ihqrta9ck5fc9d@4ax.com>

David J. Marcus wrote:

>I have many modules each with a common entry point. For discussion's sake:
>    module A has A::func();
>    module B has B::func();
>    etc.
>
>Based on input, $inp (where $inp is set to 'A', or 'B', etc), I want to call
>the proper module's function.
>
>The only way I can see of doing it is some form of a switch statement:
>    if ($inp eq 'A')
>        {    require A;
>             A::func(...parms...);
>        }
>    elsif ($inp eq 'B')
>        {    require B;
>             B::func(...parms...);
>        }
>    ...etc.

Why don't you use OO? Simply treat func() as a class method.

	$inp->func(...parms...);

with $inp as a string. You do need to require 'A' and 'B' first.

The main difference that you need to think about, is that now you'll get
the class name (= package name; 'A' or 'B', in these two examples) as a
first parameter, in front of those of your own.

-- 
	Bart.


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

Date: Mon, 26 Mar 2001 14:45:58 -0500
From: "David J. Marcus" <djmarcus@ex-pressnet.com>
Subject: Re: How can I avoid long 'switch' statement?
Message-Id: <tbv73d6pek0i11@corp.supernews.com>


> >I have many modules each with a common entry point. For discussion's
sake:
> >    module A has A::func();
> >    module B has B::func();
> >    etc.
> >
> >Based on input, $inp (where $inp is set to 'A', or 'B', etc), I want to
call
> >the proper module's function.
> >
> >The only way I can see of doing it is some form of a switch statement:
> >    if ($inp eq 'A')
> >        {    require A;
> >             A::func(...parms...);
> >        }
> >    elsif ($inp eq 'B')
> >        {    require B;
> >             B::func(...parms...);
> >        }
> >    ...etc.
>
> Why don't you use OO? Simply treat func() as a class method.
>
> $inp->func(...parms...);
>
> with $inp as a string. You do need to require 'A' and 'B' first.
>
> The main difference that you need to think about, is that now you'll get
> the class name (= package name; 'A' or 'B', in these two examples) as a
> first parameter, in front of those of your own.

I had considered using objects in exactly this way, but I don't see how it
solves the problem.

To use this approach I would need to have each module be an object that
inherits from a common object and supply its own 'func' method. This means
that somewhen before I invoke the object I have to create an instance of it.
I believe I would have the same 'huge' cascading if/elsif statements at the
time of creation (that is, somewhere I have to have each and every module
explicitly declared). In my case, modules are being added( while the
application is running) to the directory from which this application is
running out of.

Did I misunderstand something?

I was hoping to find some form of:
    require $inp;
    $inp->func(...);

where the module name is dynamic (in a variable).

-TIA
David




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

Date: Mon, 26 Mar 2001 21:35:33 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: How can I avoid long 'switch' statement?
Message-Id: <3abfa1f8.128054945@news.erols.com>

On Mon, 26 Mar 2001 14:45:58 -0500, "David J. Marcus"
<djmarcus@ex-pressnet.com> wrote:

>[Recovered attribution to Bart Lateur <bart.lateur@skynet.be>]
>>
>> Why don't you use OO? Simply treat func() as a class method.
>>
>> $inp->func(...parms...);
>>
>> with $inp as a string. You do need to require 'A' and 'B' first.
>>
>> The main difference that you need to think about, is that now you'll get
>> the class name (= package name; 'A' or 'B', in these two examples) as a
>> first parameter, in front of those of your own.
>
>I had considered using objects in exactly this way, but I don't see how it
>solves the problem.
>To use this approach I would need to have each module be an object that
>inherits from a common object and supply its own 'func' method.

No object needs to be created to invoke a sub as a class method.
Sometimes the only thing to do is to stop thinking so much and just
try it.

file foo.pl
-----------
#!/usr/bin/perl -w
use strict;
#Packages A and B are included in this file for convenience.
#Make the appropriate modifications if they are independent modules.

foreach my $inp ('A', 'B') {
  $inp->func('bar', 'baz', 'fraz');
}

package A;
sub func {
  print "This is sub 'func' in package 'A'\n";
  print "Received the following arguments: ", map {"'$_' "} @_;
  print "\n\n";
}

package B;
sub func {
  print "This is sub 'func' in package 'B'\n";
  print "Received the following arguments: ", map {"'$_' "} @_;
  print "\n\n";
}

Output of foo.pl
----------------
This is sub 'func' in package 'A'
Received the following arguments: 'A' 'bar' 'baz' 'fraz' 

This is sub 'func' in package 'B'
Received the following arguments: 'B' 'bar' 'baz' 'fraz' 

Much less messy than a cascade of
'if (this) {require that; that::func(...)}' lines.  As long as you
remember to call each func only as a class method, they can shift off
the first element of @_ and throw it away.  They'll need to do a
little dancing to keep the first arg if you ever use the
'A::func(...)' flavor syntax.


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

Date: Mon, 26 Mar 2001 22:16:02 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: How can I avoid long 'switch' statement?
Message-Id: <fsfvbt4363dmjskucf1df7qgmki326vtgi@4ax.com>

David J. Marcus wrote:

>I was hoping to find some form of:
>    require $inp;
>    $inp->func(...);

Guess what: you can. Only, you must provide a full filename for require,
the 'Foo::Bar' -> 'Foo/Bar.pm' conversion magic doesn't work in such a
case.

	$inp = 'A';
	require "$inp.pm";
	$inp->func(...);

-- 
	Bart.


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

Date: Mon, 26 Mar 2001 22:55:00 +0200
From: Marc Bigler <N_O_S_P_A_M_syn_uw@hotmail.com>
Subject: HUP signal handling
Message-Id: <3ABFACA4.2B120598@hotmail.com>

Hello,

I would like to use the signal HUP to restart a perl base daemon. The
problem that I am getting is the following: the first kill -HUP on the
perl process will work perfectly, restarting my perl daemon, great! But
if then I do again a kill -HUP to restart it again then no reactions
from my daemon, it still works but it just seems that it doesn't want to

process anymore my HUP.

Here is the code I use to do that:

$SIG{HUP} = \&reinitialize;

sub reinitialize {

 print "HUP signal received, reinitializing server...\n";
 print "DEBUG: Closing server socket\n";
 close(SERVER_SOCKET) or warn "Can't close listen socket: $!\n";

 print "Killing childrens if any...\n";
 kill_childrens();

 unlink PID_FILE;
 chdir("/tmp");
 sleep 1;
 exec(SELF) or die("ERROR: Couln't restart");

}

That's all basically, so when I receive a HUP i restart my perl daemon
using exec(). SELF as a constant like this:

use constant SELF => '/tmp/my_perl_daemon.pl';

Anyone has an idea why this reaction ?

Thanks for the help

Regards,
Marc







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

Date: Mon, 26 Mar 2001 11:46:15 -0800
From: Becca Sibrack <becca@usenix.org>
Subject: LISA 2001 Call for Papers
Message-Id: <becca-4DABAF.11461526032001@reader.news.uu.net>
Keywords: system administrator, system administrations, sys admin, UNIX, LINUX, OS, database, LISA, technical conference, seminar, tutorials, classes, invited talks, refereed papers, white papers, computers, operating systems, applications, hardware, software, SUN, Microsoft, windows, windows NT, symposium, security, databases, development, maintenance, troubleshooting, performance tuning, methodology, training, scaling, integration, programming, network, networking, technology, protocol, Perl, TCP/IP, hubs, routers, servers, CISCO, Oracle, Sybase, Open Systems

15th Systems Administration Conference (LISA 2001)
December 2-7, 2001
San Diego, CA USA
http://www.usenix.org/events/lisa01
Co-sponsored by USENIX, the Advanced Computing 
Systems Association, and SAGE, the System Administrators Guild 

LISA is the only conference by and for system administrators.

For fifteen years, the USENIX and SAGE sponsored 
Systems Administration Conference (LISA) has been the 
leading forum for system administrators to meet, learn, and 
exchange ideas with one another on every aspect of computer 
and network management.

The LISA 2001 Program Committee invites you to contribute ideas, 
proposals, and papers for tutorials, invited talks program, refereed 
papers track, workshops, work-in-progress reports, and symposia 
track. Submissions that address any and all aspects of systems 
administration are welcome. For complete guidelines, please visit:
http://www.usenix.org/events/lisa01

Submissions are due June 5, 2001.

By participating in LISA, you are contributing to state-of-the-art
computer management, and spreading knowledge about one 
of the most important challenges of the next century--
living with the machines!

Please come and share your expertise with us! Visit our Web site for
more information: http://www.usenix.org/events/lisa01


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

Date: Mon, 26 Mar 2001 19:07:25 GMT
From: "Eric" <mail@ericmarques.net>
Subject: LWP help
Message-Id: <NrMv6.21412$PF4.29991@news.iol.ie>

I am using LWP::UserAgent
How do i set a timeout and maximum recieve data?


--
Eric Marques
mail@ericmarques.net




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

Date: Mon, 26 Mar 2001 19:36:13 -0000
From: doughera@maxwell.phys.lafayette.edu (Andy Dougherty)
Subject: Re: Multiple perl versions on same machine?
Message-Id: <slrn9bv6jt.cm6.doughera@maxwell.phys.lafayette.edu>

In article <Kett6.36378$tr5.4314900@news1.telusplanet.net>, 
	       $kr1pt_k177y@salmahayeksknockers.edu wrote:
>One of the servers I mind has perl already installed to /opt/perl5.
>Recently, I installed perl56 to /opt/perl56.

This should work just fine.  I have done this sort of thing for years.

>						Now, I want to compile PERL
>DBI for the /opt/perl56 install, but am having the problem that it is 
>looking for libraries/modules in the /opt/perl5 directory.
>
>Here is my compile command line:  /opt/perl56/bin/perl Makefile.PL
>
>Even after executing the binary from this location, it appears that it is
>looking for particular libraries in /opt/perl5, which is does not find.

Odd.  I have successfully done what you suggested for years, though
never with the DBI module.  It ought to just work.

>How do I set the environment so it knows where to look for things?

Ahh.  I wonder if perhaps you need to _unset_ things in your
environment?  Perhaps you have PERL5LIB or PERLLIB set in your 
environment?  For a standard perl install, there usually no reason
to set any environment variables (other than $PATH to find the perl
executable).  Perl itself knows where to find its own libraries.  You
usually don't need to set PERL5LIB at all.

-- 
    Andy Dougherty		doughera@lafayette.edu
    Dept. of Physics
    Lafayette College, Easton PA 18042


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

Date: Mon, 26 Mar 2001 14:07:47 -0500
From: "Y" <nospam@email.com>
Subject: Re: Newbie
Message-Id: <3abf9371$1@bluesky.reedtech.com>

Here's the code:

#!/usr/bin/perl

# Create Backup of Virtusertable

use File::Copy;

$file = virtusertable;
$filebak = "virtusertable.bak";

copy ($file, $filebak);


# Process email address and perform add/delete/change procedure

while(<>){
 split('\t',$_);
 if (/FORWARD/) {
  $address1 = $_[1];
  $address2 = $_[2];
  if (/ADD/) {
   open (FILE, ">> $file") || die $!;
   print FILE "$address1\t$address2\n";
  }
  elsif (/DELETE/){
   open (FILE, "$file") || die $!;
    %data = <FILE>;
    delete %data {"$address1 $address2"};
    }
   }
 }

I execute:  update.pl update.txt

contents of update.txt:
FORWARD user1@blg.com test@aol.com DELETE

here is virtusertable:

jwhitesi@blg.com jlm048@hotmail.com
user1@blg.com test@aol.com

thanks!!!

"Mark Jason Dominus" <mjd@plover.com> wrote in message
news:3abf7207.4751$12b@news.op.net...
> In article <3abf56c6@bluesky.reedtech.com>, Y <nospam@email.com> wrote:
> >I am trying to delete a line from an array and it isn't working:
> >
> >open (FILE, "$file") || die $!;
> > %data = <FILE>;
> > delete %data {"$address1 $address2"};
> >
> >What am I doing wrong?
>
> Everything.   First, %data is not an array.  It is a hash.
>
> You'd better show a small smaple of the data in the file, and show a
> specific example of what you are trying to do.  What you've shown so
> far is so peculiar and wrong that I can't figure out what you really
> want.
>
> --
> @P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub
p{
>
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^or
d
> ($p{$_})&6];$p{$_}=/
^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
> close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep
rand(2)if/\S/;print




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

Date: Mon, 26 Mar 2001 21:06:37 GMT
From: Kim C <kimmfc@mydeja.com>
Subject: one-liner assignment from regex
Message-Id: <thbvbt84fag81qbcsntacv5trp30p3c0h7@4ax.com>

Hi,

Is there a way to assign regex matches to a list without using $1, $2,
etc?  Perhaps something like the following (which does not work):

##### <guesscode> #####

@list = qw(one two three);

foreach (@list){
	($start , $end) = /(^.)(.$)/;
}

##### </guesscode> #####

Thanks,

Kim


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

Date: Mon, 26 Mar 2001 21:21:58 GMT
From: trammell@bayazid.hypersloth.invalid (John Joseph Trammell)
Subject: Re: one-liner assignment from regex
Message-Id: <slrn9bvao5.i5g.trammell@bayazid.hypersloth.net>

On Mon, 26 Mar 2001 21:06:37 GMT, Kim C <kimmfc@mydeja.com> wrote:
> Hi,
> 
> Is there a way to assign regex matches to a list without using $1, $2,
> etc?  Perhaps something like the following (which does not work):
> 
> ##### <guesscode> #####
> 
> @list = qw(one two three);
> 
> foreach (@list){
> 	($start , $end) = /(^.)(.$)/;
> }

Get your regexp right first.

  foreach (@list)
  {
	 print "OK\n" if /(^.)(.$)/;
  }



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

Date: 26 Mar 2001 14:12:09 -0800
From: Kevin Lacquement <klacquement@syscor.com>
Subject: Re: one-liner assignment from regex
Message-Id: <m2g0g0goxy.fsf@aridhol.syscor.priv>

>>>>> "KC" == Kim C <kimmfc@mydeja.com> writes:

[...]

    KC> 	($start , $end) = /(^.)(.$)/;

What does this actually match?  I'd think it gets everything, but what
would be in $1 and $2?

[...]

Cheers,
Kevin

-- 
He that breaks a thing to find out what it is has left the path of
wisdom                                         -- Gandalf the Grey


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

Date: Mon, 26 Mar 2001 16:23:18 -0600
From: "Rich" <bigrich318@yahoo.com>
Subject: Re: one-liner assignment from regex
Message-Id: <tbvg3br298fl9c@corp.supernews.com>


"Kim C" <kimmfc@mydeja.com> wrote in message
news:thbvbt84fag81qbcsntacv5trp30p3c0h7@4ax.com...
> Hi,
>
> Is there a way to assign regex matches to a list without using $1, $2,
> etc?  Perhaps something like the following (which does not work):
>
> ##### <guesscode> #####
>
> @list = qw(one two three);
>
> foreach (@list){
> ($start , $end) = /(^.)(.$)/;
> }

my @list = qw(one two three);

foreach (@list){
my ($start , $end) = (/^(.).+?(.)$/);
print "$start $end\n";
}

prints :
o e
t o
t e




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

Date: Mon, 26 Mar 2001 22:29:03 GMT
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: one-liner assignment from regex
Message-Id: <slrn9bvck9.9cd.tadmc@tadmc26.august.net>

Kim C <kimmfc@mydeja.com> wrote:
>
>Is there a way to assign regex matches to a list without using $1, $2,
>etc?  


Yes. That is what m// does when used in a list context.


>Perhaps something like the following 
                   ^^^^

No, you have it precisely right as-is.


>(which does not work):


Yes it does. The pattern does not match the string, so the memories 
are not set. That is what is _supposed_ to happen when a match fails.

Try it with a pattern than will match your strings.


>##### <guesscode> #####
>
>@list = qw(one two three);
>foreach (@list){
>	($start , $end) = /(^.)(.$)/;

        ($start , $end) = /(^.).*(.$)/;   # or whatever


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


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

Date: Mon, 26 Mar 2001 22:30:41 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: one-liner assignment from regex
Message-Id: <efgvbtsdkb9a2sa59176qp2q4576u3bptp@4ax.com>

Kim C wrote:

>Is there a way to assign regex matches to a list without using $1, $2,
>etc?

This question has been asked -- and answered -- several time in the last
few weeks alone. I'll just do it again one last time.

For simple cases, no /g modifier, just assign to an array, or LHS list.

	@matches = /$pattern/;

That doesn't work too well in combination with /g. There, you can use
the @+ and @- array, and with some map BLOCK functionality, you can
extract the submatches from the original string.

	while($str =~ /$pattern/g) {
	    my @matches = map { substr $str, $-[$_], $+[$_]-$-[$_] }
	       1 .. $#+;
	    # ... do stuff with @matches
	}

-- 
	Bart.


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

Date: Mon, 26 Mar 2001 20:12:18 GMT
From: ced@bcstec.ca.boeing.com (Charles DeRykus)
Subject: Re: Regex question
Message-Id: <GAtMsI.2vy@news.boeing.com>

In article <slrn9bmmqb.dqt.rgarciasuarez@rafael.kazibao.net>,
Rafael Garcia-Suarez <rgarciasuarez@free.fr> wrote:
>Ave Wrigley wrote in comp.lang.perl.misc:
>> Anyone know a neat regex for the follwing; capture from a string a
>> substing which contains any given 2 (3,4, ...) words, in any order.
>> Something along the lines of:
>> 
>> $string =~ /(?=.*foo)(?=.*bar)(.*?)(?<=foo.*)(?<=bar.*)/;
>> 
>> execpt that variable length lookbehind not implemented.
>
>A solution that does not use regexps :
>
>$searched = "There are a bar, a foo and a baz here."; 
>@words = qw/foo bar baz/;
>my $beg = (sort { $a <=> $b } map { index $searched, $_ } @words)[0];
>my $end = (sort { $b <=> $a } map { (length) + index $searched, $_ } @words)[0];
>print substr($searched,$beg,$end-$beg),"\n";
>
>Note that it's possible to enhance this solution by optimizing the way
>of calculating the max and the min of a list of integers.
>
>Also, handling cases where $searched does not match all words is left as
>an exercise the reader.
>

Here's one using a regex... uglier though: 

my $regex;
$regex .= "(?=^(.*?$_))" for @words;
my @sort = sort { length $a <=> length $b } $searched =~ /$regex/s; 
my ($trim) = $sort[0] =~/(.*?)(?:@{[join "|", @words]})/;
my $match = substr $sort[$#sort], length $trim;


--
Charles DeRykus


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

Date: Mon, 26 Mar 2001 21:50:18 +0200
From: Simon Stiefel <SiStie@nuclear-network.com>
Subject: Sockets
Message-Id: <Pine.LNX.4.31.0103262137250.2338-100000@server.stiefel.priv>

Hi,

I just tried out the sockets.
I have to add, that I am a newbie on this section.
I've written a client and a server-script (from the Perl Cookbook).

Now, the Client shall send a string to the Server.
How can I do that? ;-))

Here are the scripts:

--- server.pl ----

#!/usr/bin/perl -w

use diagnostics;

$server_port =3D 1234; # Server-Port


use IO::Socket;

if ($server =3D IO::Socket::INET->new(LocalPort   =3D> $server_port,
                                Type            =3D> SOCK_STREAM,
                                Reuse           =3D> 1,
                                Listen          =3D> 10 )
        or die "Couldn't run server on port $server_port: $@\n")
{
print "Server running at port $server_port...\n";
}

while ($client =3D $server->accept()) {

$server->recv($receive_data, 10);
print "Client connected...\n";

print $receive_data;
}


close($server);


----- client.pl -----

#!/usr/bin/perl -w

use IO::Socket;
use diagnostics;


$socket =3D IO::Socket::INET->new(PeerAddr        =3D> "server.stiefel.priv=
",
                                PeerPort        =3D> 1234,
                                Proto           =3D> "tcp",
                                Type            =3D> SOCK_STREAM,
                                Timeout         =3D> "1000")
        or die "Couldn't etablish connection: $@\n";

$socket->send("Hallo", 10) or die "Couldn't send data...$@\n";

$answer =3D <$socket>;

print $answer;

close($socket);


---- END ----

So, what have I to do exacly, that the string "Hallo" will be printed on
the server-script?

Thank you.



Mit freundlichen Gr=FC=DFen,        .~.     Open Minds.
with best regards               /V\             Open Sources.
                               // \\                    Open Future!
Simon Stiefel                 /(   )\_ I N U X
                               ^ ~ ^
--=20
|Simon Stiefel | Zwerbachstrasse 17 | 72555 Metzingen-Glems | Germany |
|SimonStiefel@nuclear-network.com   |  http://www.nuclear-network.com |
|ICQ#: 20196644 | phone: +49(0)7123/379070 | fax: +49(0)179/335990106 |
|Tux#: 114751 | PingoS - Linux-User helfen Schulen | Powered by LiNUX |



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

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


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