[22166] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4387 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jan 11 21:10:33 2003

Date: Sat, 11 Jan 2003 18:10:10 -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           Sat, 11 Jan 2003     Volume: 10 Number: 4387

Today's topics:
        perl pattern matching woes <foo@var.com>
    Re: perl pattern matching woes (Tony L. Svanstrom)
    Re: perl pattern matching woes <bongie@gmx.net>
    Re: perl pattern matching woes <admin@-NOSPAM-2host.com>
    Re: perl pattern matching woes (Daniel S. Lewart)
    Re: Picking out options in argv Andrew Lee
    Re: Picking out options in argv <starfury@cats.ucsc.edu>
    Re: Red Hat 8 breaking one-liner signature script <goldbb2@earthlink.net>
    Re: Server sockets <goldbb2@earthlink.net>
    Re: undef of large Hashes/Arrays took a very long time <goldbb2@earthlink.net>
    Re: Using tr/// - Am I barking up the wrong tree? <Jodyman@hotmail.com>
    Re: vbs from perl Andrew Lee
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sat, 11 Jan 2003 19:46:33 -0600
From: "n" <foo@var.com>
Subject: perl pattern matching woes
Message-Id: <v21i62hobabl23@corp.supernews.com>

I have been using perl for some years without problems, but I am stumped on
the problem I having with a simple pattern match.

All I want to do is check to see that $foo consists of exactly 4 digits.
Sounds simple but I am having problems.

My sample code fails to work properly:

$foo = "12347777";

  print "timeout is $foo \n";
  if ($foo =~ /(\d{4})/) {  print "Timeout value is 4 digits!\n";}

This should fail but instead it prints the value. If I set $foo to something
less than 4 digits it works. But 4 or more and the output improperly says it
is 4 digits. Even using {4,4} fails.

What am I doing wrong?

Win2k SP3 perl v5.6.1 build 633

Thanks for any help

Andrew





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

Date: Sun, 12 Jan 2003 01:54:09 GMT
From: tony@svanstrom.com (Tony L. Svanstrom)
Subject: Re: perl pattern matching woes
Message-Id: <1fonjyf.rkbtthslua02N%tony@svanstrom.com>

n <foo@var.com> wrote:

> $foo = "12347777";
> 
>   print "timeout is $foo \n";
>   if ($foo =~ /(\d{4})/) {  print "Timeout value is 4 digits!\n";}
> 
> This should fail but instead it prints the value. If I set $foo to something
> less than 4 digits it works. But 4 or more and the output improperly says it
> is 4 digits. Even using {4,4} fails.
> 
> What am I doing wrong?

 Your checking to see if there are four digits in $foo... which is true
also if there's five digits in four, right?! =)

 ^\d{4}$

-- 
# Per scientiam ad libertatem! // Through knowledge towards freedom! #
# Genom kunskap mot frihet! =*= (c) 1999-2002 tony@svanstrom.com =*= #

    perl -e'print$_{$_} for sort%_=`lynx -source svanstrom.com/t`'


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

Date: Sun, 12 Jan 2003 02:53:24 +0100
From: "Harald H.-J. Bongartz" <bongie@gmx.net>
Subject: Re: perl pattern matching woes
Message-Id: <3515532.u1oAY4uRxZ@nyoga.dubu.de>

n wrote:
> I have been using perl for some years without problems, but I am
> stumped on the problem I having with a simple pattern match.

Good,  I like simple things. ;-)

> All I want to do is check to see that $foo consists of exactly 4
> digits. Sounds simple but I am having problems.
> 
> My sample code fails to work properly:
> 
> $foo = "12347777";
> 
>   print "timeout is $foo \n";
>   if ($foo =~ /(\d{4})/) {  print "Timeout value is 4 digits!\n";}

This checks, if $foo *contains* four digits in a row.

        if ($foo =~ /^\d{4}$/) { ...

will do that you want.  (You don't need the grouping parens, do you?  If
the match succeeds, $1 will be equal to $foo, otherwise it will be
empty, so nothing to see here.)

Ciao,
        Harald
-- 
Harald H.-J. Bongartz <bongie@gmx.net>
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
I'm a great lover, I'll bet.    -- Emo Phillips


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

Date: Sat, 11 Jan 2003 17:56:06 -0800
From: "2Host.com - Robert" <admin@-NOSPAM-2host.com>
Subject: Re: perl pattern matching woes
Message-Id: <3E20CB36.4E4183C9@-NOSPAM-2host.com>



n wrote:
> 
> I have been using perl for some years without problems, but I am stumped on
> the problem I having with a simple pattern match.
> 
> All I want to do is check to see that $foo consists of exactly 4 digits.
> Sounds simple but I am having problems.
> 
> My sample code fails to work properly:
> 
> $foo = "12347777";
> 
>   print "timeout is $foo \n";
>   if ($foo =~ /(\d{4})/) {  print "Timeout value is 4 digits!\n";}

if ($foo =~ /^\d{4}$/) {}
-- 
Regards,
Robert McGregor - Email: admin@(remove)2host.com. Phone: 530-941-0690
Server admin, support & programing for shared & dedicated web servers
Secure, reliable hosting you expect and deserve! http://www.2host.com


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

Date: Sun, 12 Jan 2003 01:54:27 GMT
From: lewart@uiuc.edu (Daniel S. Lewart)
Subject: Re: perl pattern matching woes
Message-Id: <nV3U9.12229$Vf3.127646@vixen.cso.uiuc.edu>

Andrew <foo@var.com> writes:

> $foo = "12347777";
>   print "timeout is $foo \n";
>   if ($foo =~ /(\d{4})/) {  print "Timeout value is 4 digits!\n";}

print "Timeout value is 4 digits!\n" if $foo =~ /^\d{4}$/;

Cheers,
Daniel Lewart
lewart@uiuc.edu


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

Date: Sat, 11 Jan 2003 18:10:01 -0500
From: Andrew Lee
Subject: Re: Picking out options in argv
Message-Id: <bu812v8fu5peetjgsseo3em47kfr5m5k3s@4ax.com>

On Sat, 11 Jan 2003 21:59:05 +0100, "Harald H.-J. Bongartz"
<bongie@gmx.net> wrote:

>Andrew Lee wrote:
>> On Sat, 11 Jan 2003 10:35:41 -0800, "John Tuong"
>> <starfury@cats.ucsc.edu> wrote:
>>>This is a cool concept, I think I'll stick to this 1 because perldoc
>>>getopt doesn't yield any results.
>
>Yes, that's true, unfortunately.  Maybe we need an FAQ entry like "Does
>Perl have something like the getopt() function?" or "How do I parse
>command line options?"  But on the other hand, you can find it when
>looking at "perldoc perlmodlib" or searching on CPAN.
>
>> [snip]
>> 
>> try http://search.cpan.org ... perldoc won't find it if you didn't
>> install it  :-)
>
>Getopt::Long and Getopt::Std are part of the standard Perl distribution,
>Getopt::Long at least since 5.6.0.


Well I'll be a monkey's uncle ... yes you're correct -- I guess the
last time used the modules  was pre 5.6.0.  I remember having to
download and build them.

Thanks for the heads up

-- Andrew


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

Date: Sat, 11 Jan 2003 16:38:12 -0800
From: "John Tuong" <starfury@cats.ucsc.edu>
Subject: Re: Picking out options in argv
Message-Id: <3e20b8f2$1@news.ucsc.edu>

I've been playing with switch.  It's easy and all, but it doesn't feel lazy.
So I went and made another file that did the same thing using getopt::long.
It seems kinda fun, but I've hit a snag.  It would take in options and store
it nice and easy, but there's no checking what kind of options is stored in.

like..  -T  stuff
stuff gets stored but what if it's the wrong stuff typed in by the user.
I thought about having a subroutine that checks if the stuff is valid after
getoptions is done with the option leeching.. but that would make getopt
more cumbersome than a switch statement.  Is there a way to check the stuff
while I'm inside the GetOption function?






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

Date: Sat, 11 Jan 2003 19:24:11 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Red Hat 8 breaking one-liner signature script
Message-Id: <3E20B5AB.DFBA489C@earthlink.net>

Emmy wrote:
[snip problem identification and fix]
> > The problems that people have there occur because they think "unix
> > means you don't have to binmode() your binary files", and they get
> > bitten by the fact this is untrue on redhat8 with perl5.8.
> >
> > The historical "binary mode == text mode on unix" behavior has
> > induced a certain laziness in unix programmers with respect to
> > binmode, causing them to leave it out when they should have it in.
> 
> So, in general, if one has an old Perl script which was writing and
> reading ASCII files, he should binmode() them all to make it work now?

Nono.  If one has an old perl script which was writing and reading ASCII
files, it should work ok without change (since perl opens files in text
mode by default, and "utf8" mode is CORRECT for text files on redhat8).

If one has an old perl script which was writing and reading BINARY
files, he should binmode() all the handles referring to non-text files
to make it work now (since "utf8" mode is INCORRECT for binary files).

-- 
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r  coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);


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

Date: Sat, 11 Jan 2003 19:56:57 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Server sockets
Message-Id: <3E20BD59.C38A938E@earthlink.net>

luc wastiaux wrote:
> 
> I want to write a perl NNTP server (for a special application), but I
> have nver done any network programming with perl, where could I find a
> comparison of the different methods for using sockets in perl?
> (Socket, IO::Socket, IO::Select) ?

If you're used to writing sockets stuff in C, then you may prefer (or at
least, feel comfortable using) the Socket.pm perl module.  It's a
relatively thin wrapper around various socket constants and functions.

However, most folks prefer IO::Socket, as it lets you construct a socket
in a single step, instead of the multiple steps that Socket.pm requires.

Eg, to make a server socket with Socket.pm, you would have to do
socket(), bind(), and listen()... something like:

   my $proto = getprotobyname('tcp');
   socket(my($sock), PF_INET, SOCK_STREAM, $proto);
   bind($sock, pack_sockaddr_in($lport, inet_aton($laddr)));
   listen($sock, $n);

With IO::Socket, you just have to create it with appropriate parameters
to ->new().  Like:

   my $sock = IO::Socket::INET->new(
      LocalAddr => $laddr,
      LocalPort => $lport,
      Listen => $n,
   );

(The protocol used will be 'tcp' by default, unless something else (such
as udp) is specified.)

As to IO::Select -- this is just a wrapper around the select() function
call -- you don't *have* to use it, but it's much simpler to use.

A much more interesting problem is deciding whether you want to write a
forking server, or a multiplexing one.  A forking server is generally
easier to write, but uses more memory.  A multiplexing one will be more
complicated (especially since each client will be in a different state),
but (imho) more fun.

If you do choose to write a multiplexing server, I would seriously
advise that you use a dispatch table.  You would have a hash, keyed by
socket, and the value for each hash would be the state info (the data
currently being read in, what command is being executed, if any, what
data has yet to be sent to that socket, etc) and callbacks (coderefs for
what functions should be called when the socket becomes readable or
writable).

For examples of multiplexing servers, see:
   http://groups.google.com/groups?
      q=author:goldberg+group:comp.lang.perl.misc+do_accept

One side benefit of using a dispatch table is that if you eventually
decide to rewrite it in C, this will make it (relatively) easy.  Your
dispatch table would be an array of structs, indexed by filedescriptor
of the socket, and the callbacks would be pointers-to-functions, but it
would be otherwise about the same.

-- 
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r  coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);


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

Date: Sat, 11 Jan 2003 19:19:09 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: undef of large Hashes/Arrays took a very long time
Message-Id: <3E20B47D.56246AB6@earthlink.net>

Pierre Asselin wrote:
> Benjamin Goldberg writes:
> 
> >Jan Schubert wrote:
> >>
> >> Pls, may someone point me to some documentation about memory
> >> consumption of very large Arrays and/or Hashes!? It seems that
> >> undef/unfree of huge Arrays (2-3GB) takes very long
> 
> >It generally shouldn't.  If it does, there's a bug in your malloc.
> 
> Are you sure?  I had large undef times too and I interpreted that as a
> garbage collection expense:  free a chunk, decrement reference counts,
> recurse.

Why are you undefing, instead of declaring the array as a lexical
variable and merely letting it go out of scope?  Or assigning an empty
list to it?

When you undef() an array or hash, you're doing something very special,
which often has bad consequences on performance.

> My data structures were more than straight arrays, though.
> Still, a perl array is not allocated into a single buffer, is it?

A perl array *is* allocated as a single buffer -- the buffer is filled
with pointers to the elements of the array (which are seperate buffers,
more or less).

> >> and would even apply when exiting a perl-program.
> 
> Saw that too.  I even considered having the script kill itself -9
> instead of exiting.
> 
> >Not necessarily.  Under normal circumstances, many of the things in
> >perl's memory are freed simply by exiting -- that is, the operating
> >system magically cleans up all memory used by a program.  This
> >happens nearly instantaneously, no matter what kind of wierd malloc
> >bugs the program might have.
> 
> This doesn't help if garbage collection occurs before the exit.

Some things are garbage collected at exit, some are not.

See perldoc perlhack/PERL_DESTRUCT_LEVEL

-- 
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r  coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);


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

Date: Sun, 12 Jan 2003 00:38:07 GMT
From: "Jodyman" <Jodyman@hotmail.com>
Subject: Re: Using tr/// - Am I barking up the wrong tree?
Message-Id: <PN2U9.5552$Qr4.529510@newsread1.prod.itd.earthlink.net>


"R. Charles Henry" <trapforcannedmeatproduct@hotmail.com> wrote in message

> Trying to convert individual letters in a string, to gif image HTML tags:
> e.g.
> from:  jo@jo.com to  <img src="j.gif"><img src="o.gif"><img
src="at.gif"><img src="j.gif"> etc.
> is there an easier way of doing this?

You can do this:

#!c:\perl\bin\perl -w
use strict;

my $in = 'jo@jo.com';

my @in = split //, $in;
my $html;

foreach (@in) {
$html .= "<img src='$_.gif'>";
}

print $html;

BTW, ..gif, @.gif etc are valid filenames so you can use the easier way.

Jody




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

Date: Sat, 11 Jan 2003 18:29:06 -0500
From: Andrew Lee
Subject: Re: vbs from perl
Message-Id: <n4a12v4qkm7ad7816hpo666dhvel0shpun@4ax.com>

On Sun, 29 Dec 2002 12:14:06 -0500, Andrew Lee
<andrew_lee@earthlink.net> wrote:

>On Fri, 27 Dec 2002 22:42:42 +0200, "Janne Saarinen / HTV"
><janne.saarinen@disnet.fi> wrote:
>
>>is it possible to run allkind vbscript commands from perl scripts?
>>
>>i need to make vbscript eo exe and perl can. so if perl can run vbs
>>commands, could i do exe file from my perl script?
>>
>>vbs script cant build to exe, this is my major problem.
>
>
>
>See perldoc -f system and perldoc perlop (qw/STRING/)
>

make that perdoc perlop (qx/STRING/)


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

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


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