[22950] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5170 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jul 2 18:10:45 2003

Date: Wed, 2 Jul 2003 15:10:13 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Wed, 2 Jul 2003     Volume: 10 Number: 5170

Today's topics:
        LWP - Upload via HTTP POST (Mark Mykkanen)
    Re: LWP - Upload via HTTP POST <glex_nospam@qwest.net>
    Re: need assistance understanding multilevel hashes. <allen.wooden@educate.invalid>
    Re: Need Perl teacher/school: Network programming <irving_kimura@lycos.com>
    Re: Perl help needed <krahnj@acm.org>
    Re: pipe - non blocking read? (fork/Win32) (Greg Bacon)
    Re: pipe - non blocking read? (fork/Win32) <stjm2@hermes.cam.ac.uk>
    Re: pipe - non blocking read? (fork/Win32) (Greg Bacon)
        Please explain this .sig... [was: "Re: Is there a good  <bik.mido@tiscalinet.it>
    Re: Please explain this .sig... [was: "Re: Is there a g <abigail@abigail.nl>
    Re: Q: detect non-existent hash entries w/function? (Daniel Friedman)
    Re: Q: detect non-existent hash entries w/function? <uri@stemsystems.com>
        script for unrestricted permutation (weberh)
    Re: test, pls ignore <abigail@abigail.nl>
        trying to parse XML from an email... (rtl)
    Re: trying to parse XML from an email... <crazydj@chaostal.de>
        using 'DB_File' versus just plain tie() ? (dan baker)
    Re: using 'DB_File' versus just plain tie() ? <nanae@bill.perusion.com>
    Re: Using Win32::API To call CreateProcess - help! (Bryan Castillo)
    Re: Using Win32::API To call CreateProcess - help! <dont@want.spam>
        UTF16 and Control M's (Eileen)
    Re: UTF16 and Control M's <flavell@mail.cern.ch>
    Re: UTF16 and Control M's (Sam Holden)
    Re: XS question -- avoid memory leak <REMOVEsdnCAPS@comcast.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 2 Jul 2003 13:34:42 -0700
From: shamu112@hotmail.com (Mark Mykkanen)
Subject: LWP - Upload via HTTP POST
Message-Id: <db7dd15.0307021234.32df5558@posting.google.com>

Can someone tell me why the following code randomly produces a status
code of the following states?
	500 Can't read entity body: Unknown error
	405 Method not allowed
I'm trying to send a file via HTTP using a web form.  I can upload a
file without any problems using a browser, but I haven't been
successful with the code below.  Please help.

use strict;
use HTTP::Request::Common;
use LWP::UserAgent;

my $ua = new LWP::UserAgent;
$ua->agent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
my $req = new HTTP::Request 'GET' => 'http://localhost/';
$req->authorization_basic('user','pass');
my $content=$ua->request($req)->as_string;

=formOutput
<form 	ACTION="DataCollection.asp"
		METHOD="POST"
		ENCTYPE="multipart/form-data">

      <input TYPE="File" NAME="File">
      <input TYPE="Submit" VALUE="Submit">

    </form>
=cut

# Everything up to here succeeds

my $res = $ua->post(
	 'http://localhost/', 
	 Content_Type => 'form-data', 
	 Content      => [ 	
				file => ['data.txt'] 
			]
	  ); 
print $res->content() if($res->is_success);


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

Date: Wed, 02 Jul 2003 15:47:06 -0500
From: "J. Gleixner" <glex_nospam@qwest.net>
Subject: Re: LWP - Upload via HTTP POST
Message-Id: <HwHMa.910$yw3.48832@news.uswest.net>

Mark Mykkanen wrote:
> Can someone tell me why the following code randomly produces a status
> code of the following states?
> 	500 Can't read entity body: Unknown error
> 	405 Method not allowed
> I'm trying to send a file via HTTP using a web form.  I can upload a
> file without any problems using a browser, but I haven't been
> successful with the code below.  Please help.
> 
> use strict;
> use HTTP::Request::Common;
> use LWP::UserAgent;
> 
> my $ua = new LWP::UserAgent;
> $ua->agent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
> my $req = new HTTP::Request 'GET' => 'http://localhost/';
> $req->authorization_basic('user','pass');
> my $content=$ua->request($req)->as_string;
> 
> =formOutput
> <form 	ACTION="DataCollection.asp"
> 		METHOD="POST"
> 		ENCTYPE="multipart/form-data">
> 
>       <input TYPE="File" NAME="File">
>       <input TYPE="Submit" VALUE="Submit">
> 
>     </form>
> =cut
> 
> # Everything up to here succeeds
> 
> my $res = $ua->post(
> 	 'http://localhost/', 
> 	 Content_Type => 'form-data', 
> 	 Content      => [ 	
> 				file => ['data.txt'] 
> 			]
> 	  ); 
> print $res->content() if($res->is_success);

Take a look at your error log, and you should see what the request is 
trying to do.

The main issue is that the form has an action of "DataCollection.asp", 
however your post method doesn't send it to that action.

There are other problems with your post request, look at

perldoc lwpcook

for examples.



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

Date: Wed, 02 Jul 2003 15:25:48 GMT
From: Allen Wooden <allen.wooden@educate.invalid>
Subject: Re: need assistance understanding multilevel hashes.
Message-Id: <t8u5gvk3g46mdht0tiklhephkmujlgajcg@4ax.com>

On Fri, 27 Jun 2003 22:16:39 GMT, tiltonj@erols.com (Jay Tilton)
wrote:


>Posting entire programs is seldom necessary, and even less often
>desirable.  Reducing code to the barest minimum necessary to
>demonstrate the problem eases comprehension of the problem and its
>solution.

I'll try and remember that.. :)



>
>Slightly more intrusive but also more scalable is to create a hash of
>anonymous hashes directly.
>
>    our %customers = (
>        customer1 => { "ports" => 2, ... },
>        customer2 => { "ports" => 3, ... },
>        # etc.
>    );

So unwind the existing hashes in the config file and dump them into
another hash? I tried this and while the hashes exist and I can see
the data in it, I'm still having difficulty getting the customer
entered on the commandline to match up to the correct hash.

config: 
%foo = ( "ports" => "1", "rrd0" => 'some_path', "rate" => "0", "tag"
=> 'Foo Co.', "page_num" => "1" );

pull the contents of config into array.
then loop through the array and break out the key/value pairs and dump
them into the new hash.

$hash{$who}{$keys} = $values;

## $who is the hash name from the config file. 
## $keys are the keys from the hash in config file.
## $values are the values of the keys.

I can access all the data 
for $b ( keys %hash) {
        print "customers are: $b\n";
        for $c (keys %{ $hash{$b}}) {
                print "$c=$hash{$b}{$c}";
        }
        print "\n";


>
>For either case, instead of saying
>
>    if ( $custid =~ /^customer1$/ ) { %custid = %customer1; }
>    if ( $custid =~ /^customer2$/ ) { %custid = %customer2; }
>
>you would say
>
>    my %custid = %{ $customers{$custid} };
>
$cust = ARGV[0];
%hash1 = %{ $hash{$cust} };


Now this should copy the data in from $hash{<customer-id>} into a new
hash but instead I don't get jack. But I can loop through %hash and 
print out all the keys,values but if I try it with %hash1, I get 
"Use of unitialized value in hash dereference at line blah blah.. "
Which just so happens to be where the data should be assiged to the
new hash.  ie: %hash1 = %{ $hash{$cust} }; 


if I enter foo on commanline, I should be able to access
$hash1{foo}{"ports"} and get the value.  

I think I'm  either misunderstanding something or doing it the wrong
way.

I have a much smaller example prog if your interested.


Thanks btw for all your help.
Allen

Replace invalid with com to reply via email.
PGP key available @ keyserver.pgp.com


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

Date: Wed, 2 Jul 2003 17:04:50 +0000 (UTC)
From: Irving Kimura <irving_kimura@lycos.com>
Subject: Re: Need Perl teacher/school: Network programming
Message-Id: <bdv3bi$r7m$1@reader1.panix.com>

In <FmCMa.288$yw3.20063@news.uswest.net> "J. Gleixner" <glex_nospam@qwest.net> writes:

>The following should help you do what you want:

>http://www.stonehenge.com/merlyn/WebTechniques/col11.html

I appreciate the pointer, but I'm familiar with that script, and
I can tell you that 1) it can't handle anything other than the
simplest HTTP; most importantly, it doesn't listen to the HTTPS
port; and 2) some of the classes it uses have changed since 1997,
and as a result the script is broken.

	-Irv


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

Date: Wed, 02 Jul 2003 17:58:14 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Perl help needed
Message-Id: <3F031D3A.FBEC0D5B@acm.org>

Saya wrote:
> 
> I am a newbie to perl and regular expressions.
> I need help with extracting a substring.
> Presume:
> $test = "thisIsATest(12)";
> $subPar = should equal to (12)
> $num = should equal to 12
> 
> How do I achieve this in perl ? Do I use regular expression ?

$ perl -le'
$test = "thisIsATest(12)";
( $subPar, $num ) = $test =~ /(\((\d+)\))/;
print $subPar;
print $num;
'
(12)
12



John
-- 
use Perl;
program
fulfillment


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

Date: Wed, 02 Jul 2003 16:58:49 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: pipe - non blocking read? (fork/Win32)
Message-Id: <vg63q9djtpl72@corp.supernews.com>

In article <Pine.SOL.4.44.0307021543450.9290-100000@red.csi.cam.ac.uk>,
    Stuart Moore  <stjm2@hermes.cam.ac.uk> wrote:

: Hi, I'm using windows, I want to create a new process using fork (in
: Activestate's Perl 5.6.x for some x) and communicate between the parent
: and child using pipes. But I'd like the parent to be able to check if
: there was anything waiting for it in the pipe, and if not, come back and
: check later.
: 
: Is this possible? If not any suggestions on what I can do?

Is there a reason you'd prefer pipes to, say, sockets?

Greg
-- 
Today is already the tomorrow which the bad economist yesterday
urged us to ignore.
    -- Henry Hazlitt, Economics is One Lesson


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

Date: Wed, 2 Jul 2003 18:32:58 +0100
From: Stuart Moore <stjm2@hermes.cam.ac.uk>
Subject: Re: pipe - non blocking read? (fork/Win32)
Message-Id: <Pine.SOL.4.44.0307021829550.26955-100000@red.csi.cam.ac.uk>

On Wed, 2 Jul 2003, Greg Bacon wrote:
> In article <Pine.SOL.4.44.0307021543450.9290-100000@red.csi.cam.ac.uk>,
>     Stuart Moore  <stjm2@hermes.cam.ac.uk> wrote:
>
> : Hi, I'm using windows, I want to create a new process using fork (in
> : Activestate's Perl 5.6.x for some x) and communicate between the parent
> : and child using pipes. But I'd like the parent to be able to check if
> : there was anything waiting for it in the pipe, and if not, come back and
> : check later.
> :
> : Is this possible? If not any suggestions on what I can do?
>
> Is there a reason you'd prefer pipes to, say, sockets?

Um, no. Can Socekts do that? In which case how? A brief trawl through the
documentation gave me nothing.

Thank you very much
Stuart



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

Date: Wed, 02 Jul 2003 18:38:56 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: pipe - non blocking read? (fork/Win32)
Message-Id: <vg69m0j0o24q23@corp.supernews.com>

In article <Pine.SOL.4.44.0307021829550.26955-100000@red.csi.cam.ac.uk>,
    Stuart Moore  <stjm2@hermes.cam.ac.uk> wrote:

: On Wed, 2 Jul 2003, Greg Bacon wrote:
:
: > Is there a reason you'd prefer pipes to, say, sockets?
: 
: Um, no. Can Socekts do that? In which case how? A brief trawl through
: the documentation gave me nothing.

At the bottom of the IO::Select manpage is a socket server.  Here's
an adaptation of that:

    #! /usr/local/bin/perl

    use warnings;
    use strict;

    use IO::Select;
    use IO::Socket;

    sub child {
        my $port = shift;

        my $s = IO::Socket::INET->new(PeerAddr => "localhost:$port");
        die "$0: failed to create socket in child" unless $s;

        for (1 .. 5) {
            sleep rand 10;

            print $s "message #$_\n";
        }
    }

    sub do_something_else {
        print "parent: Doing something else...\n";
        sleep rand 5;
        print "parent: Done.\n";
    }

    sub check_sockets {
        my $lsn = shift;
        my $sel = shift;

        my @ready = $sel->can_read(0);
        foreach my $fh (@ready) {
            if ($fh == $lsn) {
                # Create a new socket
                my $new = $lsn->accept;
                $sel->add($new);
            }
            else {
                # Process socket
                my $fd = fileno $fh;
        
                if (my $input = <$fh>) {
                    chomp $input;
                    print "parent: fd $fd: [$input]\n";
                }
                else {
                    $sel->remove($fh);
                    $fh->close;

                    $lsn->close;
                }
            }
        }
    }

    ## main
    my $port = 8080;
    my $lsn  = new IO::Socket::INET(Listen => 1, LocalPort => $port);
    my $sel  = new IO::Select($lsn);

    my $pid = fork;
    die "$0: fork: $!" unless defined $pid;

    unless ($pid) {
        child $port;
        exit 0;
    }

    while ($lsn->opened) {
        check_sockets $lsn, $sel;

        do_something_else;
    }

Hope this helps,
Greg
-- 
And when I heard the baby-talk coming out of George W. Bush's mouth in front
of Congress, there's this Axis of Evil, Iran, Iraq and ... North Korea?  I
mean, he doesn't know where these places are, much less what evil is . . .
    -- Gore Vidal


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

Date: Wed, 02 Jul 2003 23:20:43 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Please explain this .sig... [was: "Re: Is there a good free/not so expensive Perl IDE for Linux"]
Message-Id: <smf6gv076cfs8r8uhv5tvmevckslkbtmd6@4ax.com>

On 30 Jun 2003 14:02:35 GMT, Abigail <abigail@abigail.nl> wrote:

> perl -wlne '}{print$.' file

I guess this has to do with "the code -n puts around" -e's argument...


Michele
-- 
$\=q.,.,$_=q.print'  ,\g,,( w,a'c'e'h,,map{$_-=qif/g/;chr
}107..q[..117,q)[map+hex,split//,join' ,2B,, w$ECDF078D3'
F9'5F3014$,$,];];$\.=$/,s,q,32,g,s,g,112,g,y,' , q,,eval;


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

Date: 02 Jul 2003 21:20:44 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Please explain this .sig... [was: "Re: Is there a good free/not so expensive Perl IDE for Linux"]
Message-Id: <slrnbg6j5c.mu3.abigail@alexandra.abigail.nl>

Michele Dondi (bik.mido@tiscalinet.it) wrote on MMMDXCII September
MCMXCIII in <URL:news:smf6gv076cfs8r8uhv5tvmevckslkbtmd6@4ax.com>:
$$  On 30 Jun 2003 14:02:35 GMT, Abigail <abigail@abigail.nl> wrote:
$$  
$$ > perl -wlne '}{print$.' file
$$  
$$  I guess this has to do with "the code -n puts around" -e's argument...


I share that guess.



Abigail
-- 
   my $qr =  qr/^.+?(;).+?\1|;Just another Perl Hacker;|;.+$/;
      $qr =~  s/$qr//g;
print $qr, "\n";


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

Date: 2 Jul 2003 08:32:48 -0700
From: danielf@erols.com (Daniel Friedman)
Subject: Re: Q: detect non-existent hash entries w/function?
Message-Id: <68cdce23.0307020732.4c6d5fbb@posting.google.com>

Tad,

Late yesterday, the same idea sprouted in my mind, and lo, you've
given me the code!  Bingo, this is just what I was looking for; thanks
very much.

--dan


tadmc@augustmail.com (Tad McClellan) wrote in message news:<slrnbg3t3r.2tf.tadmc@magna.augustmail.com>...

> Here is a function to get you started, it wants a hash-ref as
> the first arg, followed by a list of keys, eg:
> 
>    do_something() if exists_no_viv(\%myhash, $foo, $bar, $baz)
> 
> 
> ---------------------------
>    sub exists_no_viv {  # "walk" down a chain of hashrefs
>       my $h = shift;    # a ref to hash, start at the "top"
> 
>       while ( my $k = shift ) {
>          return 0 unless exists $h->{$k};  # fail w/o autoviv
>          if ( ref $h->{$k} eq 'HASH' )     # go one level "deeper"
>             { $h = $h->{$k} }
>       }
>       return 1;
>    }
> ---------------------------


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

Date: Wed, 02 Jul 2003 18:00:56 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Q: detect non-existent hash entries w/function?
Message-Id: <x7n0fw4xlz.fsf@mail.sysarch.com>

>>>>> "DF" == Daniel Friedman <danielf@erols.com> writes:

<don't top post>

  DF> Late yesterday, the same idea sprouted in my mind, and lo, you've
  DF> given me the code!  Bingo, this is just what I was looking for;
  DF> thanks very much.

again, i will point you to this article which has similar code.

	http://tlc.perlarchive.com/articles/perl/ug0002.shtml

uri

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


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

Date: 2 Jul 2003 11:35:01 -0700
From: weberh@zedat.fu-berlin.de (weberh)
Subject: script for unrestricted permutation
Message-Id: <7003ec4a.0307021035.43f21ccf@posting.google.com>

Hi !

All scripts for permutation I found yet, were restricted by depth
(loops have to be hardcoded) or memory (recursive approach).

Here I wrote a handy script that gives the user unrestricted 
choice of number of elements and depth of permutation.

The script is a bit special, because it dynamically generates code.
This code delivers the permutation and is evaluated within the script.

So, "perm 4 red green blue" gives

redredredred
redredredgreen
 .
 .
blueblueblueblue 

It's still an ad hoc script and has to be refined, of course.

Would you like to see it as part of a module ?
Maybe together with other flavors of permutation and combination ?

Here we go ...

#!/usr/bin/perl
#perm: permutation with user defined depth and number of elements
#usage: perm depth element1 element2 ...

use strict;

my ($code,$i);
my ($depth,@element) = @ARGV;


for ($i = 1; $i <= $depth; $i++)   #Code generation starts here
    {$code .= "for (my \$i$i = 0; \$i$i <= $#element; \$i$i++)\n{"}

$code .= 'print "';

for (my $c=1; $c <= $depth; $c++)
    {$code .= "\$element[\$i$c]"}

$code .= '\n"';

for (my $c=1; $c <= $i-1; $c++)
    {$code .= '}'}

$code .= "\n";                     #Code generation is finished here

eval($code);                       #Code evaluation gives the permutation




Regards,     Harald


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

Date: 02 Jul 2003 19:39:14 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: test, pls ignore
Message-Id: <slrnbg6d72.mu3.abigail@alexandra.abigail.nl>

Yuri Shtil (shtil@comcast.net) wrote on MMMDXCII September MCMXCIII in
<URL:news:3UBMa.18176$926.1334@sccrnsc03>:

[ Nothing ]


*PLOINK*



Abigail
-- 
@_=map{[$!++,$_^$/]}split$²,"\@\x7Fy~*kde~box*Zoxf*Bkiaox";$\="\r";
$|=++$*;do{($#,$=)=(rand@_,rand@_);@_[$#,$=]=@_[$=,$#]}for($*..@_);
for$:($|..@_-$|){for($|..@_-$:){@_[$_-$|,$_]=@_[$_=>$_-$*]if$_[$_][
$¼]<$_[$_-$*][$®];print+map{$_->[$|]}@_;select$·,$°,$½,0.1}}print$/


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

Date: 2 Jul 2003 08:49:25 -0700
From: rtl@trsolutions.net (rtl)
Subject: trying to parse XML from an email...
Message-Id: <cd8b6435.0307020749.6dc639bd@posting.google.com>

I am retrieving an email with XML content.  I strip off the email
headings and save only the XML portion to a file.  Subsequently I
parse through the XML using XML::Simple.

Most times the XML parses fine, but there are times when there is some
extra encoding, such that all ='s (equal signs) are followed by '3D'. 
This seems to happen when certain characters such as TM or (R) or (c)
are included in some of the data.

I do not know what modules may be helpful so I can successfully decode
the XML correctly.  I have tried mime::quotedprint which successfully
fixes most of the encoding, but there are times when characters such
as TM come in as =E2=84=A2 but is not translated as a single character
but as three separate characters.

Any help would be great.  I am running Perl 5.6.1 on Win2k.  I am
swimming in modules and terms including MIME, base64 and UTF-8 -- but
to no avail.

Thank you.


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

Date: Wed, 02 Jul 2003 19:43:19 +0200
From: Bastian Ballmann <crazydj@chaostal.de>
Subject: Re: trying to parse XML from an email...
Message-Id: <bdv5v1$112ftn$2@ID-31630.news.dfncis.de>

rtl wrote:

> Most times the XML parses fine, but there are times when there is some
> extra encoding, such that all ='s (equal signs) are followed by '3D'.

3D is the hexadecimal representation of the character '=' in ASCII.
Maybe there is some encoding stuff fooling your parser and you should
first decode hexadecimal values to ascii?
HTH & Greets

Basti


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

Date: 2 Jul 2003 11:17:32 -0700
From: botfood@yahoo.com (dan baker)
Subject: using 'DB_File' versus just plain tie() ?
Message-Id: <13685ef8.0307021017.5ca1596b@posting.google.com>

can anyone shed some light on the pros and cons of tie() ing to a hash
using 'DB_FILE' rather than just a plain tie? What I have been playing
with works great and seems very fast, but creates a binary file that I
can't move between a UNIX server and a PC test environment.... I am
wondering how I can get the tie(0 to create a plain ASCII file that
could be moved between platforms.

Also wondering about other tradeoffs such as speed, and at what point
in terms of number of records it would even be noticable. i have a
fairly simple value string, and can use delimiters to pack and unpack
multiple fields, so thats not a problem I don't mind dealing with.

my current code uses something lie this:

	use DB_File;
	tie %tempHash , 'DB_File' , $dbfile ; 
	$tempHash{$key} = $ValueString ;

and I am wondering about what I'd have to do to get the tie()ed file
to be written in plain ASCII text ?

thanx,

d


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

Date: Wed, 02 Jul 2003 19:46:49 GMT
From: Perusion Hostmaster <nanae@bill.perusion.com>
Subject: Re: using 'DB_File' versus just plain tie() ?
Message-Id: <slrnbg6dg5.j9u.nanae@bill.heins.net>

In article <13685ef8.0307021017.5ca1596b@posting.google.com>, dan baker wrote:
> can anyone shed some light on the pros and cons of tie() ing to a hash
> using 'DB_FILE' rather than just a plain tie? What I have been playing
> with works great and seems very fast, but creates a binary file that I
> can't move between a UNIX server and a PC test environment.... I am
> wondering how I can get the tie(0 to create a plain ASCII file that
> could be moved between platforms.

AFAIK, DB_File is portable between machines and even big-endian
and little-endian architectures (unlike GDBM or NDBM).

You may be running into version bifurcation -- many DB_File
modules still use 1.x, and a lot have moved to 2.x/3.x.

> Also wondering about other tradeoffs such as speed, and at what point
> in terms of number of records it would even be noticable. i have a
> fairly simple value string, and can use delimiters to pack and unpack
> multiple fields, so thats not a problem I don't mind dealing with.
> 
> my current code uses something lie this:
> 
> 	use DB_File;
> 	tie %tempHash , 'DB_File' , $dbfile ; 
> 	$tempHash{$key} = $ValueString ;
> 
> and I am wondering about what I'd have to do to get the tie()ed file
> to be written in plain ASCII text ?

Some of this depends on your keys and data. If you are are going to
have binary data, or data that can exceed a few hundred bytes, you
really want to use DB_File.

You also might try something like Tie::TextDir, which creates a directory
with files named for the keys. Performance will not be near as good,
of course, but it might suit. A sample script follows.


#!/usr/bin/perl -w

use Tie::TextDir;

my $DIR = '/tmp/testhash';

unless (-d $DIR) {
	mkdir $DIR, 0777
		or die "mkdir $DIR: $!\n";
}

my %hash;

tie %hash, 'Tie::TextDir',  $DIR, 'rw';  # Open in read/write mode

print "Current keys in hash:\n\n";

for(keys %hash) {
	print "\t$_\n";
}

print "\n";

## Create new file based on date

my $now = time();
my $date = scalar(localtime);

$hash{$now} = "File created by me on $date\n";

untie %hash;

chdir $DIR 
	or die "chdir $DIR: $!\n";

my $dir = `ls -l`;
print "Directory $DIR:\n$dir\n";

-- 

Light travels faster than sound. This is why some people appear bright
until you hear them speak. -- unknown


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

Date: 2 Jul 2003 11:59:48 -0700
From: rook_5150@yahoo.com (Bryan Castillo)
Subject: Re: Using Win32::API To call CreateProcess - help!
Message-Id: <1bff1830.0307021059.78c20b32@posting.google.com>

Chris Lowth <dont@want.spam> wrote in message news:<yvAMa.1017$ju6.8993@newsfep4-glfd.server.ntli.net>...
> I need to call the "real" CreateProcess of windows NT from an ActiveState 
> perl script. The code below always reports a return of 0 (FALSE) and fails 
> to start "notepad". Where as the matching C code (also listed) works fine.
> 
> I cant see what's wrong myself - has anyone got this to work, and if so - 
> can you see what is wrong with the script?
> 
> ---------- perl --------------
> [ This doesnt work ]
> 
> use Win32::API;
> 
> Win32::API::Struct->typedef( "STARTUPINFO", qw(
>    DWORD cb;
><snip some stuff>
>    HANDLE hStdError;
> ) );
> 
> Win32::API::Struct->typedef( "PROCESS_INFORMATION", qw(
>    HANDLE hProcess;
>    HANDLE hThread;
>    DWORD dwProcessId;
>    DWORD dwThreadId;
> ) );
> 
> my $CreateProcess = Win32::API->new( "kernel32", "
>  BOOL CreateProcess(
>    LPCTSTR lpApplicationName,
<snip some stuff>
> )");
> 
> $cmd = "c:\\winnt\\notepad.exe";
> $dir = ".";
> 
> my $si = Win32::API::Struct->new( "STARTUPINFO" );
> my $pi = Win32::API::Struct->new( "PROCESS_INFORMATION" );
> $rtn = $CreateProcess->Call(undef, $cmd, undef, undef, 0, 0, undef, $dir, 
> $si, $pi);
> 
> print "rtn = $rtn\n";
> 


There is a module Win32::Process you should use instead.
The documentation for it has an example running notepad.


<snip the C>


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

Date: Wed, 02 Jul 2003 21:24:36 +0100
From: Chris Lowth <dont@want.spam>
Subject: Re: Using Win32::API To call CreateProcess - help!
Message-Id: <8cHMa.2065$nP.1093@newsfep4-winn.server.ntli.net>

Bryan Castillo wrote:

> Chris Lowth <dont@want.spam> wrote in message
> news:<yvAMa.1017$ju6.8993@newsfep4-glfd.server.ntli.net>...
>> I need to call the "real" CreateProcess of windows NT from an ActiveState
>> perl script. The code below always reports a return of 0 (FALSE) and
>> fails to start "notepad". Where as the matching C code (also listed)
>> works fine.
>> 
>> I cant see what's wrong myself - has anyone got this to work, and if so -
>> can you see what is wrong with the script?
>> 
>> ---------- perl --------------
>> [ This doesnt work ]
>> 
>> use Win32::API;
>> 
>> Win32::API::Struct->typedef( "STARTUPINFO", qw(
>>    DWORD cb;
>><snip some stuff>
>>    HANDLE hStdError;
>> ) );
>> 
>> Win32::API::Struct->typedef( "PROCESS_INFORMATION", qw(
>>    HANDLE hProcess;
>>    HANDLE hThread;
>>    DWORD dwProcessId;
>>    DWORD dwThreadId;
>> ) );
>> 
>> my $CreateProcess = Win32::API->new( "kernel32", "
>>  BOOL CreateProcess(
>>    LPCTSTR lpApplicationName,
> <snip some stuff>
>> )");
>> 
>> $cmd = "c:\\winnt\\notepad.exe";
>> $dir = ".";
>> 
>> my $si = Win32::API::Struct->new( "STARTUPINFO" );
>> my $pi = Win32::API::Struct->new( "PROCESS_INFORMATION" );
>> $rtn = $CreateProcess->Call(undef, $cmd, undef, undef, 0, 0, undef, $dir,
>> $si, $pi);
>> 
>> print "rtn = $rtn\n";
>> 
> 
> 
> There is a module Win32::Process you should use instead.
> The documentation for it has an example running notepad.
> 
> 
> <snip the C>

Thanks for the reply. Yes I looked at Win32::Process, but it isnt rich 
enough for my needs. I want to be able to "play" with the Standard file 
handles passed in the STARTUPINFO structure - and Win32::Process does 
provide this facility - which is why I am trying to use Win32::API.

So I am still stuck - does anyone have a working example you'd be happy to 
share?

Chris
-- 
Real address: chris at lowth dot sea oh em.
World's first wrist-watch PDA with Palm OS, available June 30
from Amazon.com. Order now to beat the rush!
   http://www.lowth.com/shop/wrist_pda


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

Date: 2 Jul 2003 12:52:39 -0700
From: ellona@u.washington.edu (Eileen)
Subject: UTF16 and Control M's
Message-Id: <c630f334.0307021152.43e6d80b@posting.google.com>

Hi,

I have a text file with CTRL-M's. It is encoded as UTF16. When I try
to search for a string in this file, nothing is found. If I remove the
control-m's in vi, my search works. However, I cannot get the
control-m's to be removed using Perl. I've tried:

 my $file= "myfile.xml";
 while (<IN>) {
 	s/\cM//g;
 }

and

  my $file= "myfile.xml";
 while (<IN>) {
 	s/\x{0x0D00}//g;
 }

and 

 my $file= "myfile.xml";
 while (<IN>) {
 	s/\^M//g;
 }

and 

while (<IN>) {
 	s/\cM//g;
 }

all to no avail. I've tried it on Unix perl as well as Windows perl.
Again, I can remove the characters with vi (using s/^V^M//g).

Does anyone have any ideas on what to do? If I convert the file to
UTF8, the substitution and subsequent searches work. However, I have
several hundred files to deal with, and they are all encoded as UTF16.

Thanks,

Eileen


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

Date: Wed, 2 Jul 2003 22:51:40 +0200
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: UTF16 and Control M's
Message-Id: <Pine.LNX.4.53.0307022215570.19732@lxplus014.cern.ch>

On Wed, Jul 2, Eileen inscribed on the eternal scroll:

> I have a text file with CTRL-M's. It is encoded as UTF16.

Then you will need to tell Perl about that.  You _are_ using
at least Perl 5.8.0, yes?  Take a look at perldoc perluniintro on your
hard disk or at http://www.perldoc.com/perl5.8.0/pod/perluniintro.html
and follow a few relevant links.

Note that UTF-16 comes in two flavours, so don't omit to mention
whether it's LE or BE.

> When I try
> to search for a string in this file, nothing is found.

You've got to read the file first...

> If I remove the
> control-m's in vi, my search works. However, I cannot get the
> control-m's to be removed using Perl. I've tried:
>
>  my $file= "myfile.xml";
>  while (<IN>) {

There's something important missing there  ;-}

Anyhow, be sure to tell Perl what it's supposed to be reading, or else
it'll default to whatever it thinks the local I/O convention is (maybe
iso-8859-1), which isn't likely to be useful.

[...]

>  	s/\x{0x0D00}//g;

That's some weird Unicode character... well, actually no, it seems
0D00 and 0D01 are unassigned, the first assigned character in that
group is 0D02 MALAYALAM SIGN ANUSVARA

Anyway, joking apart, it looks as if you already know that your
external data is utf-16LE, just that you omitted to say so...

You've got to either read that stuff in the natural Perl 5.8 way, i.e
with the right encoding layer, and then process the characters _as
Unicode_; or you've got to read it in as binary and handle it as pairs
of bytes.  I don't recommend the latter approach, at least not with a
current version of Perl.

You seem to be stuck somewhere in the middle, trying to do partly one
and partly the other, and that's not going to work.

> Does anyone have any ideas on what to do?

With Perl 5.8, as I say, I reckon your best bet is to open the file
using ':encoding(utf-16LE)' and then you'll be set to handle the
characters naturally in Perl.  Assuming you want the results in
utf-16LE again, don't omit to say so on the output filehandle too.

> If I convert the file to
> UTF8, the substitution and subsequent searches work. However, I have
> several hundred files to deal with, and they are all encoded as UTF16.

No need to "convert the file", Perl will deal with this for you.

But I would recommend taking a moment to understand a little better
how Perl's Unicode support is designed to work, and its relationship
with external coding schemes.  Each step is basically straightforward,
but there's a semi-infinite number of ways of getting things wrong,
and only a relatively small number of ways of doing it right, so it
repays a bit of study.  The Perl documentation isn't perfect yet, but
it's at least as clear as I'd be able to explain it, usually better.

-- 

  "it is beyond the power of words to describe the way HTML browsers
   encode non-ASCII form data."  - lib/Encode documentation


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

Date: 2 Jul 2003 21:34:30 GMT
From: sholden@flexal.cs.usyd.edu.au (Sam Holden)
Subject: Re: UTF16 and Control M's
Message-Id: <slrnbg6jv5.d94.sholden@flexal.cs.usyd.edu.au>

On 2 Jul 2003 12:52:39 -0700, Eileen <ellona@u.washington.edu> wrote:
> Hi,
> 
> I have a text file with CTRL-M's. It is encoded as UTF16. When I try
> to search for a string in this file, nothing is found. If I remove the
> control-m's in vi, my search works. However, I cannot get the
> control-m's to be removed using Perl. I've tried:
> 
>  my $file= "myfile.xml";
>  while (<IN>) {
>  	s/\cM//g;
>  }

You didn't include the code for opening IN.

Are you telling perl that the file is UTF16? Or is perl just treating it
as a bunch of bytes and hence erasing 'half' a character? See the
numerous posts about various character codings that have turned up here
a week ago or so for details (I don't know them since I don't deal with
that stuff at the moment).

[snip a couple of other equivalent code sippets]

> 
> all to no avail. I've tried it on Unix perl as well as Windows perl.
> Again, I can remove the characters with vi (using s/^V^M//g).
> 
> Does anyone have any ideas on what to do? If I convert the file to
> UTF8, the substitution and subsequent searches work. However, I have
> several hundred files to deal with, and they are all encoded as UTF16.

UTF16->UTF8 and back is lossless and easily scripted. In fact vi is
easily scripted so dealing with hundreds of files should be as easy as
dealing with 10.

However, I suspect that telling perl what the text encoding actually is
will solve the problem (and if your perl is old and doesn't have all
that stuff then you can always replace the two byte sequence that codes
a \cM using s//g, something like s/^(..)*\0\cM/$1/g - that's completely
untested and probably wrong - depending on which UTF16 coding you
actually have).

-- 
Sam Holden



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

Date: Wed, 02 Jul 2003 16:44:35 -0500
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: XS question -- avoid memory leak
Message-Id: <Xns93ACB4787514Fsdn.comcast@206.127.4.25>

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

"Sisyphus" <kalinabears@hdc.com.au> wrote in
news:3f02c098$0$22125@echo-01.iinet.net.au: 

> 
> Have your function return an SV* instead of a char*.
> SV  * your_func () {
>    char * string;
>    SV * outsv;
> 
> Copy the contents of 'string' into 'outsv'.
> outsv = newSVpv(string, 0);
> 
> Then free() 'string' and return 'outsv':
> free(string);
> return outsv;

Great!  Thanks for the advice!

- -- 
Eric
$_ =  reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPwNSOGPeouIeTNHoEQLHqACg6hdjskaLfCfh4O7zSdlRxXG+6uoAnApC
CqPkPDljF82SfaWFeIQ6koYh
=lhhe
-----END PGP SIGNATURE-----


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

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


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