[17786] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5206 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Dec 27 11:10:31 2000

Date: Wed, 27 Dec 2000 08:10:11 -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: <977933411-v9-i5206@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Wed, 27 Dec 2000     Volume: 9 Number: 5206

Today's topics:
    Re: Perl's BigInts/BigFloats <harasty@penvision.com>
    Re: Problems with Archive::Tar (BUCK NAKED1)
        random problem <wo@mtvwebdesign.hypermart.net>
    Re: regex question: putting back references in another  (Tad McClellan)
    Re: regexp with NULLs <bart.lateur@skynet.be>
        Replacing text <s.r.patterson@earthling.net>
    Re: Replacing text (Greg Bacon)
    Re: Replacing text nobull@mail.com
    Re: Replacing text (Tad McClellan)
    Re: Reverse "append to file" <cyner.mail@sweden.com>
    Re: Text Conversion in Perl <JD@Tallorno.net>
        using perl to connect to a pop3 server dionysus39@hotmail.com
    Re: using perl to connect to a pop3 server dionysus39@hotmail.com
    Re: using perl to connect to a pop3 server (Martien Verbruggen)
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Wed, 27 Dec 2000 15:24:20 GMT
From: Dan Harasty <harasty@penvision.com>
Subject: Re: Perl's BigInts/BigFloats
Message-Id: <3A4A09C2.F9F87BF6@penvision.com>


Raymond WAN wrote:

>         I did something else which is probably wrong, but worked for some
> reason.  Instead of explicitly creating a BigFloat with the new operator,
> I did something like this:
> 
>         $a = "4000000000000";
> 
>         And it knew $a was a BigFloat as opposed to doing the same without
> quotes....

You are observing two things, and coming to the wrong conclusion:

	1) Perl converts strings to numbers when it needs to
	2) Perl (somehow) implements integers > 2**32

In the above code, $a is just a regular Perl scalar which Perl converts to a string or number depending on context.  To get a true "BigFloat", you MUST do this:

	use Math::BigFloat;
	$a = new Math::BigFloat "3.45e+1000";

$a is now a special type of scalar: technically "a blessed reference", or, more colloquially, a Perl "object reference".  The reference is of type "Math::BigFloat".  To verify this, use the ref() function:

	print ref($a);

Prints:

	Math::BigFloat

Then you can do normal arithmetic on $a; Perl will "detect" you are using a "BigFloat" object, and use that module's version of "+", "*", "/", etc.

To my chagrin, some operations are not implemented, like log(), exp().  Does anyone reading this know of any effort to implement these for "BigFloats"?

- Dan Harasty


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

Date: Wed, 27 Dec 2000 02:16:07 -0600 (CST)
From: dennis100@webtv.net (BUCK NAKED1)
Subject: Re: Problems with Archive::Tar
Message-Id: <22475-3A49A547-20@storefull-246.iap.bryant.webtv.net>

I think you forgot a line. "extract_archive" extracts and writes. 

Try this coding for the Archive/Tar part of your script.

use Archive::Tar;
$tar = Archive::Tar->new(); 
$tar->extract_archive("blah.tar.gz",1);

Tested and works for me.

I hope this helps,
--Dennis



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

Date: Wed, 27 Dec 2000 15:51:55 +0100
From: "Maarten" <wo@mtvwebdesign.hypermart.net>
Subject: random problem
Message-Id: <92d0s0$8gt$4@news.tudelft.nl>

Please have a look at the following:
############BEGIN##############
#!c:/perl/bin/perl

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

print &Rand(7);
print "<br>";
print &Rand(7);

sub Rand {
my($n) = @_;
my ($string,$i);
my(@array);

srand;

 for($i=0; $i<10;$i++) {
  push @array, $i;
 }
 for($i=a; $i ne "aa";$i++) {
  push @array, $i;
  push @array, uc $i;
 }

 for($i=0; $i < $n; $i++) {

  my $num = rand(@array);
  $string .= $array[$num];

 }

 return $string;
}
##########END###############

When I use srand, the script will generate a random number each time the
script is run, but the second call to the &Rand subroutine will produce the
exact same number.
When I stop using srand, the script will not have that problem, but will
display the same random number every time!

What am I doing wrong here? I need a script wich can produce more than 1
random numbers in 1 run.

Maarten

--

passme







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

Date: Wed, 27 Dec 2000 06:39:27 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: regex question: putting back references in another variable
Message-Id: <slrn94jl7f.e1u.tadmc@magna.metronet.com>

Rafael Garcia-Suarez <rgarciasuarez@free.fr> wrote:
>Michael Stearns wrote in comp.lang.perl.misc:
>> Say I have the following:
>> 
>> $text= "foobar";
>> $find = "(foo)(bar)";

>You probably mean
>  $replace='$2$1';
>
>> I want to have a regular expression that will allow me to end up with $text
>> set to "barfoo". I was hoping I could do something like the following:
>> 
>> $text=~s^$find^$replace^s;
>> 
>
>One way is to use eval() to force evaluation at runtime:


But you can spell "eval" as "e" if you like.


>$text =~ s/$find/eval(qq("$repl"))/e;

   $text =~ s/$find/qq("$repl")/ee;


But I think I like it better this way:

   $repl = '$2.$1';
   $text =~ s/$find/$repl/ee;


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


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

Date: Wed, 27 Dec 2000 10:08:28 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: regexp with NULLs
Message-Id: <2tfj4tcf8e048o6pmj1mn4da13a93sttv2@4ax.com>

Steven Fletcher wrote:

>I attempted to write a function that would
>strip anything following a \00 from the data supplied

	s/\000.*//s;

-- 
	Bart.


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

Date: Wed, 27 Dec 2000 10:33:49 GMT
From: Stephen Patterson <s.r.patterson@earthling.net>
Subject: Replacing text
Message-Id: <92cgid$p9$1@nnrp1.deja.com>

I'd like to replace some text within a word document. This is the code
I'm using (which doesn't work).

open (FILE, "+<c:\\steve\\perl-progs\\name.doc") ||
    die "Couldn't open file: $!";

while (<FILE>) {
    if ($_ =~m /:name:/i) {
	$_ =~s /:name:/:test:/i;
    }
}

The file is being opened OK, and the while loop is being entered, but
the text is not being replaced (I'm trying to replace :name:
with :test:). As far as I can tell, the 2 regular expressions are OK,
but the if block is not being entered.

Does anyone know what I'm doing wrong?



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


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

Date: Wed, 27 Dec 2000 14:35:37 -0000
From: gbacon@HiWAAY.net (Greg Bacon)
Subject: Re: Replacing text
Message-Id: <t4jvhpbmjp4m71@corp.supernews.com>

In article <92cgid$p9$1@nnrp1.deja.com>,
    Stephen Patterson  <s.r.patterson@earthling.net> wrote:

: open (FILE, "+<c:\\steve\\perl-progs\\name.doc") ||
:     die "Couldn't open file: $!";
: 
: while (<FILE>) {
:     if ($_ =~m /:name:/i) {
: 	$_ =~s /:name:/:test:/i;
:     }
: }
: 
: [...]
:
: Does anyone know what I'm doing wrong?

You're not outputting your changes.  Instead, you're making changes
and then throwing them away.  You're also making changes inside a
file with a proprietary format.  Your operation may give unexpected
results.

Greg
-- 
People often forget that every problem they solve is a special case of
some recursively unsolvable problem!
    -- Knuth


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

Date: 27 Dec 2000 14:27:12 +0000
From: nobull@mail.com
Subject: Re: Replacing text
Message-Id: <u9g0jandmn.fsf@wcl-l.bham.ac.uk>

Stephen Patterson <s.r.patterson@earthling.net> writes:

> As far as I can tell, the 2 regular expressions are OK,
> but the if block is not being entered.

As far as I can tell there's a lion in the next room.  I'll only know
when I make an observation that would be influenced by the presence of
the lion.

Your program produces no standard output and writes to no files so how
can you tell what it is doing?

See FAQ "How do I change one line..."

BTW: testing the presence of a pattern with m// before replacing it
with s/// is a waste of time - s/// will already do nothing if there's
no match.

BTW writing $_ =~ is affected.  =~ means "use the specified variable
instead of $_ in the following operatation".

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: Wed, 27 Dec 2000 07:48:49 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Replacing text
Message-Id: <slrn94jp9h.e1u.tadmc@magna.metronet.com>

Stephen Patterson <s.r.patterson@earthling.net> wrote:

>I'd like to replace some text within a word document. 


I figure you meant to say "Word document" there instead?

If not, then what is a "word document"?


>Subject: Replacing text
                    ^^^^

A "Word document" is not a text file. It is a binary file.


>open (FILE, "+<c:\\steve\\perl-progs\\name.doc") ||
>    die "Couldn't open file: $!";


Using single quotes would make it easier to read, because then
you can use one backslash per backslash instead of two.

   open (FILE, '+<c:\steve\perl-progs\name.doc') ||


Using sane path separators will make it easier still.

   open (FILE, '+<c:/steve/perl-progs/name.doc') ||

or

   open (FILE, "+<c:/steve/perl-progs/name.doc") ||


>while (<FILE>) {
>    if ($_ =~m /:name:/i) {
>	$_ =~s /:name:/:test:/i;


Since you are processing a binary file, you can't really know
how big a "line" is going to end up being. There may be more
than one ':name:' in a "chunk".

Better put a s///g option on there too...


>    }
>}
>
>The file is being opened OK, and the while loop is being entered, but
>the text is not being replaced 


Your program does not make any output (hint), so how do you 
know that nothing is being replaced?


>(I'm trying to replace :name:
>with :test:). As far as I can tell, the 2 regular expressions are OK,


You do not need 2 pattern matches. s/// alone will do, as it only
substitutes when it matches anyway.


>but the if block is not being entered.


You have put a print statement or something in the block to
test that hypothesis?


>Does anyone know what I'm doing wrong?


Missing a Perl FAQ is what you are doing wrong. part 5:

   "How do I change one line in a file/
    delete a line in a file/
    insert a line in the middle of a file/
    append to the beginning of a file?"


You probably want to type this too:

   perldoc -f binmode


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


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

Date: Wed, 27 Dec 2000 13:40:29 GMT
From: chr1st1an <cyner.mail@sweden.com>
Subject: Re: Reverse "append to file"
Message-Id: <3A49F0B5.3000403@sweden.com>

Tad McClellan wrote:

>    perldoc -q append
> 
>       "How do I change one line in a file/
>        delete a line in a file/
>        insert a line in the middle of a file/
>        append to the beginning of a file?"
> 
> You can modify the code given there for your situation by print()ing
> to NEW before you start copying from OLD.

Erm... Ok. I had some trouble finding what you were referring to (I 
didn't understand "perldoc -q", thought it was another Linux command. 
Maybe it is? I'm still new here.) But now that Joe Schaefer showed me 
the URL I understand what OLD and NEW is. Thank you.

-- 
|
|      chr1st1an
|      cyner.mail@sweden.com
|



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

Date: Wed, 27 Dec 2000 12:55:39 +0000
From: John Delacour <JD@Tallorno.net>
To: "Mario Thomas" <mario@alamar.net>
Subject: Re: Text Conversion in Perl
Message-Id: <v03130300b66f949d3bea@news>

This routine seems to work well.  I've not yet tried it on huge files but I
guess its performance would not be bad.  The script print a sample string
and then reads a mac file and writes the equivalent Unicode html character
entities to another file.  I have written the script in such a way that if
the user is working on a Mac the file "temp.in" will first be written with
a list of all the Mac characters 0x80 and greater.  On other systems, just
paste the Mac stuff into your fin file and save it.

The text stream is read one character at a time.  If the character is in
the extended ascii range, it is converted to html code.

As I'm only a beginner in perl, I am sure there is a neater way of doing
this, but I haven't seen it.  I hope someone more experienced will supply
it.

JD

##################
@html = split /\s/,
'&#xC4; &#xC5; &#xC7; &#xC9; &#xD1; &#xD6; &#xDC; &#xE1;
&#xE0; &#xE2; &#xE4; &#xE3; &#xE5; &#xE7; &#xE9; &#xE8;
&#xEA; &#xEB; &#xED; &#xEC; &#xEE; &#xEF; &#xF1; &#xF3;
&#xF2; &#xF4; &#xF6; &#xF5; &#xFA; &#xF9; &#xFB; &#xFC;
&#x2020; &#xB0; &#xA2; &#xA3; &#xA7; &#x2022; &#xB6; &#xDF;
&#xAE; &#xA9; &#x2122; &#xB4; &#xA8; &#x2260; &#xC6; &#xD8;
&#x221E; &#xB1; &#x2264; &#x2265; &#xA5; &#xB5; &#x2202;
&#x2211; &#x220F; &#x3C0; &#x222B; &#xAA; &#xBA; &#x3A9;
&#xE6; &#xF8; &#xBF; &#xA1; &#xAC; &#x221A; &#x192;
&#x2248; &#x2206; &#xAB; &#xBB; &#x2026; &#xA0; &#xC0;
&#xC3; &#xD5; &#x152; &#x153; &#x2013; &#x2014; &#x201C;
&#x201D; &#x2018; &#x2019; &#xF7; &#x25CA; &#xFF; &#x178;
&#x2044; &#x20AC; &#x2039; &#x203A; &#xFB01; &#xFB02;
&#x2021; &#xB7; &#x201A; &#x201E; &#x2030; &#xC2; &#xCA;
&#xC1; &#xCB; &#xC8; &#xCD; &#xCE; &#xCF; &#xCC; &#xD3;
&#xD4; &#xF8FF; &#xD2; &#xDA; &#xDB; &#xD9; &#x131;
&#x2C6; &#x2DC; &#xAF; &#x2D8; &#x2D9; &#x2DA; &#xB8;
&#x2DD; &#x2DB; &#x2C7;';

############# Convert a string Mac to Unicode;
$macstring = 'ŽtŽ Ÿbel ÒhelloÓ Stuffitª';
@list = split //, $macstring;
foreach (@list) {
	$ord = ord();
	if ($ord > 127) {
		$n = $ord - 128;
		print $html[$n];
	 } else {
	print
	}
}
print $/

############# Convert a Mac file to Unicode;
$dir = 'd:documents'; # folder
$fin = "$dir:temp.in"; # file to read
$fout = "$dir:temp.out"; #file to write
$_ = $ENV{'TMPDIR'};
if (m/:/) { # if OS is Mac
	$OS = 'mac';
	open FIN, ">$fin"; # Write to fin only if running mac...;
	#...otherwise work with existing data;
	for ($i = 128; $i < 256; $i++) {
		print FIN chr($i).' ';
	}
	close FIN;
}
open FIN, "<$fin";
open FOUT, ">$fout";
print FOUT "<html>\n";
while (read FIN, $_, 1) {
	$ord = ord();
	if ($ord > 127) {
		$n = $ord - 128;
		print FOUT $html[$n];
	 } else {
	print FOUT
	}
}
print "\a\n", time - $^T, ' seconds.';
close FIN; close FOUT;

############# View results in editor -- Mac only
if ($OS eq 'mac') {
	MacPerl::DoAppleScript(<<END_SCRIPT);
	tell app "Finder" to Â
		open {item "$fout",item "$fin"} usingÂ
		 application file id "R*ch"
END_SCRIPT
}





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

Date: Wed, 27 Dec 2000 22:42:37 +1100
From: dionysus39@hotmail.com
Subject: using perl to connect to a pop3 server
Message-Id: <3A49D5AD.2613D1C0@hotmail.com>

I am attempting to put together a basic script to provide my site's
members with webmail functionality. A pop3 mail server is already set
up, with the accounts created on it, and it works fine. However, I now
need to be able to connect to that server through the perl script. I
know what commands to send to the server, and assume I would just set up
a
open(OUT, "> $server") file object and use the command
print OUT "Stuff to send to server" to give the server commands,
however, I have no idea what
value the variable $server should hold. I also do not know how I would
be able to get data from the server - would the command
@email = print OUT "Retrieve email command" work?

Any help much appreciated,

-d



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

Date: Wed, 27 Dec 2000 22:46:32 +1100
From: dionysus39@hotmail.com
Subject: Re: using perl to connect to a pop3 server
Message-Id: <3A49D697.7DAE2319@hotmail.com>

p.s. I am attempting to build a cross-platform script, so using
platform-specific programs is not really an option - I have to use perl
commands to do the connection

-d

dionysus39@hotmail.com wrote:

> I am attempting to put together a basic script to provide my site's
> members with webmail functionality. A pop3 mail server is already set
> up, with the accounts created on it, and it works fine. However, I now
> need to be able to connect to that server through the perl script. I
> know what commands to send to the server, and assume I would just set up
> a
> open(OUT, "> $server") file object and use the command
> print OUT "Stuff to send to server" to give the server commands,
> however, I have no idea what
> value the variable $server should hold. I also do not know how I would
> be able to get data from the server - would the command
> @email = print OUT "Retrieve email command" work?
>
> Any help much appreciated,
>
> -d



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

Date: Wed, 27 Dec 2000 23:18:24 +1100
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: using perl to connect to a pop3 server
Message-Id: <slrn94jngg.2tg.mgjv@martien.heliotrope.home>

On Wed, 27 Dec 2000 22:42:37 +1100,
	dionysus39@hotmail.com <dionysus39@hotmail.com> wrote:
> I am attempting to put together a basic script to provide my site's
> members with webmail functionality. A pop3 mail server is already set
> up, with the accounts created on it, and it works fine. However, I now
> need to be able to connect to that server through the perl script. I
> know what commands to send to the server, and assume I would just set up
> a
> open(OUT, "> $server") file object and use the command

No. You'd need to open a TCP connection to the correct port. You could
use the Socket module or IO::Socket::INET, or one of the other TCP
modules.

> print OUT "Stuff to send to server" to give the server commands,
> however, I have no idea what
> value the variable $server should hold. I also do not know how I would
> be able to get data from the server - would the command
> @email = print OUT "Retrieve email command" work?

The perlipc documentation has some info on this, butt...

You're going through too much trouble. CPAN has several modules that
work with email and mail servers. Get a few of those, and save yourself
loads and loads of work and bugs. besides that, it doesn't look like you
really know how client-server communication normally works, so getting
yourself a module that does all that work for you might be a good thing.

http://search.cpan.org/

search for POP, Mail, and some other interesting things.

BTW. For this sort of thing, IMAP is a much, much, much better protocol.
Using POP for this is not really what POP was meant for. IMAP will do
it, or directly working with the mailboxes on disk will do it. POP is
limited.

Martien
-- 
Martien Verbruggen              | 
Interactive Media Division      | Freudian slip: when you say one thing
Commercial Dynamics Pty. Ltd.   | but mean your mother.
NSW, Australia                  | 


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

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 V9 Issue 5206
**************************************


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