[22313] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4534 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Feb 10 18:26:52 2003

Date: Mon, 10 Feb 2003 15:26:30 -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           Mon, 10 Feb 2003     Volume: 10 Number: 4534

Today's topics:
        grab lines and format <xyf@nixnotes.org>
    Re: grab lines and format <krahnj@acm.org>
    Re: grab lines and format <bigj@kamelfreund.de>
    Re: grab lines and format (Anno Siegel)
    Re: grab lines and format <Jodyman@hotmail.com>
    Re: grab lines and format <bart.lateur@pandora.be>
    Re: grab lines and format <bigj@kamelfreund.de>
    Re: grab lines and format <bigj@kamelfreund.de>
        High-speed directory listings (Seth Brundle)
    Re: High-speed directory listings <goldbb2@earthlink.net>
        How I got mysql replication going <andrew.rich@bigpond.com>
    Re: How I got mysql replication going <abigail@abigail.nl>
        How many bytes is scalar integer? <redalert@wakproductions.com>
    Re: How many bytes is scalar integer? (Walter Roberson)
    Re: How many bytes is scalar integer? <dthomson@NOSPAMusers.sf.net>
    Re: How many bytes is scalar integer? <ubl@schaffhausen.de>
    Re: How many bytes is scalar integer? <redalert@wakproductions.com>
    Re: How many bytes is scalar integer? <abigail@abigail.nl>
    Re: How many bytes is scalar integer? (Walter Roberson)
    Re: How many bytes is scalar integer? <bart.lateur@pandora.be>
    Re: How many bytes is scalar integer? <redalert@wakproductions.com>
        how to check variable assigned in child process during  (duanrt)
    Re: how to check variable assigned in child process dur <tassilo.parseval@post.rwth-aachen.de>
    Re: how to check variable assigned in child process dur <goldbb2@earthlink.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sat, 08 Feb 2003 19:06:32 GMT
From: ktb <xyf@nixnotes.org>
Subject: grab lines and format
Message-Id: <slrnb4al97.s4.xyf@scab.nixnotes.org>

This is actually the second step in a problem I'm wrangling with.  The
first step was to write a regular expression that would grab the text I
wanted out of a large file resulting in:

10-K reports. 
19th century music. 
 0148-2076
30 minutes [microform] 
 0163-5654
48 hours [microform] /  produced by CBS News. 
60 minutes [microform] 
 0361-3216

Now what I'm trying to do is grab the line such as "0361-3216" and the
line directly above it which would be "60 minutes [microform]" and print
them out in the format of:

0361-3216		60 minutes [microform]

All other lines such as "10-K reports." and "48 hours [microform] /
produced by CBS News." should be ignored.

I'm just starting with perl so I may by looking at this all wrong but
this is kind of where my thinking has been so far regarding this
problem:

while(<FILE>) {
    if (/(?<=.*)\s(*\d-\d)/) {
        print "$2\t$1); 
    }
}

That doesn't work.  I don't even know if what I'm trying to do is possible.
Any help would be appreciated.
Thanks,
kent

-- 
To know the truth is to distort the Universe.
                      Alfred N. Whitehead (adaptation)


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

Date: Sat, 08 Feb 2003 20:02:48 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: grab lines and format
Message-Id: <3E456242.FA4FE367@acm.org>

ktb wrote:
> 
> This is actually the second step in a problem I'm wrangling with.  The
> first step was to write a regular expression that would grab the text I
> wanted out of a large file resulting in:
> 
> 10-K reports.
> 19th century music.
>  0148-2076
> 30 minutes [microform]
>  0163-5654
> 48 hours [microform] /  produced by CBS News.
> 60 minutes [microform]
>  0361-3216
> 
> Now what I'm trying to do is grab the line such as "0361-3216" and the
> line directly above it which would be "60 minutes [microform]" and print
> them out in the format of:
> 
> 0361-3216               60 minutes [microform]
> 
> All other lines such as "10-K reports." and "48 hours [microform] /
> produced by CBS News." should be ignored.
> 
> I'm just starting with perl so I may by looking at this all wrong but
> this is kind of where my thinking has been so far regarding this
> problem:
> 
> while(<FILE>) {
>     if (/(?<=.*)\s(*\d-\d)/) {
>         print "$2\t$1);
>     }
> }
> 
> That doesn't work.  I don't even know if what I'm trying to do is possible.

Something like this should work:

while ( <FILE> ) {
    if ( /\d+ minutes \[microform\]/ and <FILE> =~ /\s+(\d+-\d+)/ ) {
        print "$1\t$_";
    }
}



John
-- 
use Perl;
program
fulfillment


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

Date: Sat, 08 Feb 2003 20:11:13 +0100
From: "Janek Schleicher" <bigj@kamelfreund.de>
Subject: Re: grab lines and format
Message-Id: <pan.2003.02.08.18.57.44.466520@kamelfreund.de>

On Sat, 08 Feb 2003 19:06:32 +0000, ktb wrote:

> 10-K reports. 
> 19th century music. 
>  0148-2076
> 30 minutes [microform] 
>  0163-5654
> 48 hours [microform] /  produced by CBS News. 
> 60 minutes [microform] 
>  0361-3216
> 
> Now what I'm trying to do is grab the line such as "0361-3216" and the
> line directly above it which would be "60 minutes [microform]" and print
> them out in the format of:
> 
> 0361-3216		60 minutes [microform]
> 
> All other lines such as "10-K reports." and "48 hours [microform] /
> produced by CBS News." should be ignored.
> 
> I'm just starting with perl so I may by looking at this all wrong but
> this is kind of where my thinking has been so far regarding this
> problem:
> 
> while(<FILE>) {
>     if (/(?<=.*)\s(*\d-\d)/) {
           ^^^^^^^
                    ^^

I'm surprised to see a negative look behind assertion from a perl starter!
(?<=.*) means that before this point, there has to be a .* expression.
That means that there must be 0 till infinity characters (non-newlines),
what's of course always true. So (?<=.*) has no effect, but slowness. See
also perldoc perlre for details.

The second marked point is a lonely *, that's also very strange. A *
stands for a 0-infinity repeating of the previous part of the regexp, but
there is no previous one (only the brackets ()).

>         print "$2\t$1); 
>     }
> }
> 
> That doesn't work.  I don't even know if what I'm trying to do is possible.

Of course, it's possible.
Here's a solution (not working with regexps, quite simpler):

my $last_line = undef;
while (<FILE>) {
    chomp;
    if (/\d+ - \d+/x) {  # the x modifier let ignore the whitespaces
        print "$_\t$last_line\n";
    }
    $last_line = $_;
}



Best Wishes,
Janek


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

Date: 8 Feb 2003 21:00:40 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: grab lines and format
Message-Id: <b23r5o$ngk$1@mamenchi.zrz.TU-Berlin.DE>

ktb  <xyf@nixnotes.org> wrote in comp.lang.perl.misc:
> This is actually the second step in a problem I'm wrangling with.  The
> first step was to write a regular expression that would grab the text I
> wanted out of a large file resulting in:
> 
> 10-K reports. 
> 19th century music. 
>  0148-2076
> 30 minutes [microform] 
>  0163-5654
> 48 hours [microform] /  produced by CBS News. 
> 60 minutes [microform] 
>  0361-3216
> 
> Now what I'm trying to do is grab the line such as "0361-3216" and the
> line directly above it which would be "60 minutes [microform]" and print
> them out in the format of:
> 
> 0361-3216		60 minutes [microform]
> 
> All other lines such as "10-K reports." and "48 hours [microform] /
> produced by CBS News." should be ignored.

Should "19th century music." be reported?  Assuming it should:

    my $prev = '';
    while ( <DATA> ) {
        chomp;
        print "$_\t$prev\n" if /^\s*\d+-\d+$/;
        $prev = $_;
    }

> I'm just starting with perl so I may by looking at this all wrong but
> this is kind of where my thinking has been so far regarding this
> problem:
> 
> while(<FILE>) {
>     if (/(?<=.*)\s(*\d-\d)/) {

The regex doesn't compile.  This can't be code you ran.  Please use
copy/paste from working code.

>         print "$2\t$1); 
>     }
> }

Anno


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

Date: Mon, 10 Feb 2003 04:32:30 GMT
From: "Jodyman" <Jodyman@hotmail.com>
Subject: Re: grab lines and format
Message-Id: <yXF1a.8607$1q2.740747@newsread2.prod.itd.earthlink.net>

"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
> ktb  <xyf@nixnotes.org> wrote in comp.lang.perl.misc:
> > This is actually the second step in a problem I'm wrangling with.  The
> > first step was to write a regular expression that would grab the text I
> > wanted out of a large file resulting in:
> >
> > 10-K reports.
> > 19th century music.
> >  0148-2076
> > 30 minutes [microform]
> >  0163-5654
> > 48 hours [microform] /  produced by CBS News.
> > 60 minutes [microform]
> >  0361-3216
> >
> > Now what I'm trying to do is grab the line such as "0361-3216" and the
> > line directly above it which would be "60 minutes [microform]" and print
> > them out in the format of:
> >
> > 0361-3216 60 minutes [microform]
> >
> > All other lines such as "10-K reports." and "48 hours [microform] /
> > produced by CBS News." should be ignored.
>
> Should "19th century music." be reported?  Assuming it should:
>
>     my $prev = '';
>     while ( <DATA> ) {
>         chomp;
>         print "$_\t$prev\n" if /^\s*\d+-\d+$/;
>         $prev = $_;
>     }

    Nice job Anno!

Jody




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

Date: Mon, 10 Feb 2003 15:18:08 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: grab lines and format
Message-Id: <gigf4v4044m1v1onv66hep76r5plpfu9hj@4ax.com>

Janek Schleicher wrote:

>I'm surprised to see a negative look behind assertion from a perl starter!
>(?<=.*) means that before this point, there has to be a .* expression.
>That means that there must be 0 till infinity characters (non-newlines),
>what's of course always true. So (?<=.*) has no effect, but slowness. See
>also perldoc perlre for details.

One might add that variable length lookbehind doesn't work in Perl. Only
fixed length lookbehind, and variable length lookahead.

-- 
	Bart.


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

Date: Mon, 10 Feb 2003 17:58:07 +0100
From: "Janek Schleicher" <bigj@kamelfreund.de>
Subject: Re: grab lines and format
Message-Id: <pan.2003.02.10.16.57.47.227260@kamelfreund.de>

On Mon, 10 Feb 2003 15:18:08 +0000, Bart Lateur wrote:

> Janek Schleicher wrote:
> 
>>I'm surprised to see a negative look behind assertion from a perl starter!
>>(?<=.*) means that before this point, there has to be a .* expression.
>>That means that there must be 0 till infinity characters (non-newlines),
>>what's of course always true. So (?<=.*) has no effect, but slowness. See
>>also perldoc perlre for details.
> 
> One might add that variable length lookbehind doesn't work in Perl. Only
> fixed length lookbehind, and variable length lookahead.

Good catch.
Alltough, I've often used lookbehinds,
I've never tried something like the above, as it is too unlogical :-)


Greetings,
Janek


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

Date: Mon, 10 Feb 2003 17:59:33 +0100
From: "Janek Schleicher" <bigj@kamelfreund.de>
Subject: Re: grab lines and format
Message-Id: <pan.2003.02.10.16.57.58.711246@kamelfreund.de>

On Mon, 10 Feb 2003 15:18:08 +0000, Bart Lateur wrote:

> Janek Schleicher wrote:
> 
>>I'm surprised to see a negative look behind assertion from a perl starter!
>>(?<=.*) means that before this point, there has to be a .* expression.
>>That means that there must be 0 till infinity characters (non-newlines),
>>what's of course always true. So (?<=.*) has no effect, but slowness. See
>>also perldoc perlre for details.
> 
> One might add that variable length lookbehind doesn't work in Perl. Only
> fixed length lookbehind, and variable length lookahead.

Good catch.
Alltough, I've often used lookbehinds,
I've never tried something like the above, as it is too unlogical :-)


Greetings,
Janek


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

Date: 7 Feb 2003 17:52:10 -0800
From: brundlefly76@hotmail.com (Seth Brundle)
Subject: High-speed directory listings
Message-Id: <53e2ec95.0302071752.1df017e4@posting.google.com>

I have about a million files on NFS, with about 1.5 million symlnks
pointing to them.

I need to use symlinks rather then hardlinks because of NFS, but
unfortunately I really need daily reference accounting (not just
broken symlinks), so I find every link daily and count the references
to each file. Simple, but time consuming with so many files.

For example, I need to walk a hashed directory tree and print each
symlink to stdout:

/my/tree/33/4b/334b87474488932239888454.txt (which is a symlink)

The directory depth is fixed, and the only contents at that depth are
symlinks, so I dont need to test for file type - I just ignore '.' and
'..'

I had been using glob for this:

foreach(</my/tree/*/*/*>){
               print "$_\n";
}

But I found out that glob is really slow, I think it might be doing a
stat on everything it finds, which is unnecessary.

The fastest method I have found is to open and read each nested
directory -  very ugly code, but extremely fast:

if($master=new DirHandle('/my/tree')){
                while(defined($f1=$master->read)){
                        next if substr($f1,0,1) eq '.';
                        if($l1=new DirHandle('my/tree/'.$f1)){ # ex.:
/33
                                while(defined($f2=$l1->read)){
                                              blah...

So, I have two questions:

1. Is there any other perl function or module that will acheive this
kind of performance? I have lots of long file crawls to do, so
replacing all my globs would be nice.

2. I tried to make the code more legible by putting the directory
reads in a subroutine, but I took a performance hit in loading and
returning the array reference. Is there anyway to iterate over the
list output of a subroutine as the results become available without
loading and returning a whole data structure?

Thanks for any help.


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

Date: Sat, 08 Feb 2003 00:35:38 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: High-speed directory listings
Message-Id: <3E44972A.9F41845@earthlink.net>

Seth Brundle wrote:
[snip]
> I had been using glob for this:
> 
> foreach(</my/tree/*/*/*>){
>                print "$_\n";
> }
> 
> But I found out that glob is really slow, I think it might be doing a
> stat on everything it finds, which is unnecessary.
> 
> The fastest method I have found is to open and read each nested
> directory -  very ugly code, but extremely fast:
[snip]

I would suggest you use File::Find.

   use File::Find;
   find( sub {
      return unless -l;
      print "$_ is a ", (-e ? "good" : "bad"), " link\n";
   }, "/my/tree" );
   __END__

This will be slightly slower than 3 levels of nested opening and reading
of directories, but it's much clearer.

If you want to use roll-your-own code for opening and reading of dirs,
then a bit of recursion (or psuedo-recursion) would probably be clearer
than nested loops.

   my @stack = ("/my/tree");
   my $target = ($stack[-1] =~ tr[/][]) + 3;
   while( my $f = pop @stack ) {
      if( $f =~ tr[/][] < $target ) {
         opendir( my ($fh), $f ) or next;
         push @stack, map "$f/$_",
            grep !/^\.\.?\z/, readdir($fh);
         next;
      }
      next unless -l $f;
      print "$f is a ", (-e $f ? "good" : "bad"), " link\n";
   }
   __END__

Note that this doesn't even bother trying to stat filenames which are
going to be opened as directories -- it just tries, and goes to the next
name if it fails.  This is usually faster than first doing a stat, then
doing an opendir.

NB: Both bits of code are untested.

-- 
"So, who beat the clueless idiot today?"
"Well, we flipped for it, but when Kuno
 landed, he wasn't in any shape to fight."
"Next time, try flipping a *coin.*"


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

Date: Sat, 08 Feb 2003 10:13:06 +1000
From: "Andrew Rich" <andrew.rich@bigpond.com>
Subject: How I got mysql replication going
Message-Id: <rIX0a.42905$jM5.108675@newsfeeds.bigpond.com>

How to get MySQL Replication going.

Master

/etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/tmp/mysql.sock
server-id=1
log-bin
[client]
socket=/tmp/mysql.sock
[mysql.server]
user=mysql
basedir=/var/lib
[safe_mysqld]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

mysql> GRANT FILE ON *.* TO repl@"%" IDENTIFIED BY '<password>';
mysql > SHOW MASTER STATUS;

service mysqld restart



SLAVE

mysqld drop <database> (master will create it)

/etc/my.cnf
[mysqld]
socket=/tmp/mysql.sock
server-id=2
master-host=192.168.0.2
master-user=replicate
master-password=<pasword>

[client]
socket=/tmp/mysql.sock

service mysqld restart

mysql> show slave status;
+-------------+-------------+-------------+---------------+------------------+-----+---------------+-----------------+---------------------+------------+------------+--------------+
| Master_Host | Master_User | Master_Port | Connect_retry |
Log_File         | Pos | Slave_Running | Replicate_do_db |
Replicate_ignore_db | Last_errno | Last_error | Skip_counter |
+-------------+-------------+-------------+---------------+------------------+-----+---------------+-----------------+---------------------+------------+------------+--------------+
| 192.168.0.2 | replicate   | 3306        | 60            |
pentium4-bin.003 | 343 | Yes           |                
|                     | 0          |            | 0            |
+-------------+-------------+-------------+---------------+------------------+-----+---------------+-----------------+---------------------+------------+------------+--------------+
1 row in set (0.00 sec)

mysql> slave stop;
mysql> slave start;


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

Date: 09 Feb 2003 01:20:30 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: How I got mysql replication going
Message-Id: <slrnb4bb6u.sb.abigail@alexandra.abigail.nl>

Andrew Rich (andrew.rich@bigpond.com) wrote on MMMCDXLVIII September
MCMXCIII in <URL:news:rIX0a.42905$jM5.108675@newsfeeds.bigpond.com>:
&&  How to get MySQL Replication going.


I suggest you keep posting to random, non-mysql related groups.
After all, if a million monkeys need a million years to produce
a Shakespearian work, there's hope for you too!



Abigail
-- 
perl -wle 'print prototype sub "Just another Perl Hacker" {};'


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

Date: Sat, 08 Feb 2003 02:23:26 GMT
From: "Winston Kotzan" <redalert@wakproductions.com>
Subject: How many bytes is scalar integer?
Message-Id: <ySZ0a.2043$kV5.1234@newssvr19.news.prodigy.com>

Hello,

I am converting an encryption program from another language to run on a web
server in Perl.  The encryption algorithm does shift operations on 4-byte
integers.  Can I safely use a plain scalar (like $myvariable) and trust that
it is four bytes long and that bits shifted off the left will be removed?
If not, do you have a suggestion to ensure this happens?

Thanks!

--
Winston Kotzan
http://www.wakproductions.com/




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

Date: 8 Feb 2003 03:28:41 GMT
From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Subject: Re: How many bytes is scalar integer?
Message-Id: <b21th9$t8p$1@canopus.cc.umanitoba.ca>

In article <ySZ0a.2043$kV5.1234@newssvr19.news.prodigy.com>,
Winston Kotzan <redalert@wakproductions.com> wrote:
:I am converting an encryption program from another language to run on a web
:server in Perl.  The encryption algorithm does shift operations on 4-byte
:integers.  Can I safely use a plain scalar (like $myvariable) and trust that
:it is four bytes long and that bits shifted off the left will be removed?

No, perl as a language does not constrain integers to be any 
particular size.


:If not, do you have a suggestion to ensure this happens?

logical 'and' operators. 

It might be worth your time to write two versions of the code, one
with the 4-byte assumption and the other without, and then choose between
the two versions depending what you calculate as the size of an
integer on the fly (e.g., 'pack' the value of -1 and see if the result
is 4 bytes long.) 


If you have to do a lot of encryption, then you should probably use
a C extension.
-- 
   Tenser, said the Tensor.
   Tenser, said the Tensor.
   Tension, apprehension,
   And dissension have begun.               -- Alfred Bester (tDM)


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

Date: Sat, 08 Feb 2003 13:36:12 +1000
From: Derek Thomson <dthomson@NOSPAMusers.sf.net>
Subject: Re: How many bytes is scalar integer?
Message-Id: <3e447abe$0$7812$afc38c87@news.optusnet.com.au>

Winston Kotzan wrote:
> Hello,
> 
> I am converting an encryption program from another language to run on a web
> server in Perl.  

First question - why not just use one of the encryption libraries 
already available for Perl? All the popular encryption algorithms are 
available on CPAN.

> The encryption algorithm does shift operations on 4-byte
> integers.  Can I safely use a plain scalar (like $myvariable) and trust that
> it is four bytes long and that bits shifted off the left will be removed?
> If not, do you have a suggestion to ensure this happens?
> 

Short answer - no you can't be sure exactly how long a Perl integer is. 
Yes, you can look at the source, but there is no guarantee that in 
version 5.6.X that this won't change.

Long answer - why not perform your shift with the Perl '<<' operator, 
then mask off anything beyond the first four bytes with something like 
($n & 0xffffffff) ?

--
Derek.



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

Date: Sat, 08 Feb 2003 16:37:36 +0100
From: Malte Ubl <ubl@schaffhausen.de>
Subject: Re: How many bytes is scalar integer?
Message-Id: <b23bfe$58m$1@news.dtag.de>

Winston Kotzan wrote:
> Hello,
> 
> I am converting an encryption program from another language to run on a web
> server in Perl.  The encryption algorithm does shift operations on 4-byte
> integers.  Can I safely use a plain scalar (like $myvariable) and trust that
> it is four bytes long and that bits shifted off the left will be removed?
> If not, do you have a suggestion to ensure this happens?

This is decided at build time. If you build your Perl on on 64 Bit 
System you might want to use 8 Byte integers. There might be other 
caveats, though. You might want to look at existing CPAN modules or 
consider using a Perl object with an Inline::C inplementation.

->malte



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

Date: Sat, 08 Feb 2003 23:51:54 GMT
From: "Winston Kotzan" <redalert@wakproductions.com>
Subject: Re: How many bytes is scalar integer?
Message-Id: <uKg1a.2625$Fy1.1198@newssvr16.news.prodigy.com>

Thanks for the response.

At one part of the code, I will have to rotate the bits on a 32-bit integer
(Like shifting, but the bits that fall off appear on the other end of the
32-bits).  I just discovered the pack and unpack.  Do you think that would
be applicable?


"Malte Ubl" <ubl@schaffhausen.de> wrote in message
news:b23bfe$58m$1@news.dtag.de...
> Winston Kotzan wrote:
> > Hello,
> >
> > I am converting an encryption program from another language to run on a
web
> > server in Perl.  The encryption algorithm does shift operations on
4-byte
> > integers.  Can I safely use a plain scalar (like $myvariable) and trust
that
> > it is four bytes long and that bits shifted off the left will be
removed?
> > If not, do you have a suggestion to ensure this happens?
>
> This is decided at build time. If you build your Perl on on 64 Bit
> System you might want to use 8 Byte integers. There might be other
> caveats, though. You might want to look at existing CPAN modules or
> consider using a Perl object with an Inline::C inplementation.
>
> ->malte
>




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

Date: 09 Feb 2003 01:26:20 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: How many bytes is scalar integer?
Message-Id: <slrnb4bbhs.sb.abigail@alexandra.abigail.nl>

Winston Kotzan (redalert@wakproductions.com) wrote on MMMCDXLVIII
September MCMXCIII in <URL:news:ySZ0a.2043$kV5.1234@newssvr19.news.prodigy.com>:
@@  Hello,
@@  
@@  I am converting an encryption program from another language to run on a web
@@  server in Perl.  The encryption algorithm does shift operations on 4-byte
@@  integers.  Can I safely use a plain scalar (like $myvariable) and trust that
@@  it is four bytes long and that bits shifted off the left will be removed?

No:

    $ perl -wle 'print 1 << 42'
    4398046511104
    $ perl -MConfig -wle 'print $Config {use64bitint}'
    define
    $

@@  If not, do you have a suggestion to ensure this happens?

Are you sure you want to do encryption in Perl? Encryption usually 
means lots and lots of numeric and bit-twiddling operations. Not
exactly the areas where Perl excells in speed.

You should consider doing it in C. There are some math related 
modules out there that might help you (PDL, Pari), or you could
use Inline::C or XS.



Abigail
-- 
perl -we '$@="\145\143\150\157\040\042\112\165\163\164\040\141\156\157\164".
             "\150\145\162\040\120\145\162\154\040\110\141\143\153\145\162".
             "\042\040\076\040\057\144\145\166\057\164\164\171";`$@`'


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

Date: 9 Feb 2003 05:57:26 GMT
From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Subject: Re: How many bytes is scalar integer?
Message-Id: <b24qk6$bqo$1@canopus.cc.umanitoba.ca>

In article <uKg1a.2625$Fy1.1198@newssvr16.news.prodigy.com>,
Winston Kotzan <redalert@wakproductions.com> wrote:
:At one part of the code, I will have to rotate the bits on a 32-bit integer
:(Like shifting, but the bits that fall off appear on the other end of the
:32-bits).  I just discovered the pack and unpack.  Do you think that would
:be applicable?

You could certainly do it using techniques such as unpacking as bits
to a string of 1's and 0's, manipulating the string as a string,
packing it back together as an integer... but it wouldn't be very fast!

-- 
   I've been working on a kernel
   All the livelong night.
   I've been working on a kernel
   And it still won't work quite right.      -- J. Benson & J. Doll


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

Date: Sun, 09 Feb 2003 10:13:36 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: How many bytes is scalar integer?
Message-Id: <i7ac4vcq357s92danjctjisbouiqlc2ka0@4ax.com>

Winston Kotzan wrote:

>I am converting an encryption program from another language to run on a web
>server in Perl.  The encryption algorithm does shift operations on 4-byte
>integers.  Can I safely use a plain scalar (like $myvariable) and trust that
>it is four bytes long and that bits shifted off the left will be removed?
>If not, do you have a suggestion to ensure this happens?

Yes, I do believe that Perl integers are garanteed to be at  least 4
bytes long.

But I agree wit hthe other posters: why not use one of the existing
encryption modules, that other people have written, and which have
already been thoroughly tested?

	<http://search.cpan.org/search?query=crypt&mode=module>

-- 
	Bart.


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

Date: Mon, 10 Feb 2003 03:58:48 GMT
From: "Winston Kotzan" <redalert@wakproductions.com>
Subject: Re: How many bytes is scalar integer?
Message-Id: <YrF1a.1351$VU.49312815@newssvr15.news.prodigy.com>

I must use a custom encryption algorithm because this will be used to
generate the registration codes for shareware programs.  After some
experimentation, I found a solution that works by using the shift operators
and a bitwise and on the result.  Similar to this:

# rotates 5 places left
$result = (($mynumber << 5) | ($mynumber >> 27)) & 0xFFFFFFFF;

I would like to thank everyone who responded to my post.  The responses were
helpful in familiarizing myself with Perl (with which I am relatively new).

--
Winston Kotzan
http://www.wakproductions.com/

"Bart Lateur" <bart.lateur@pandora.be> wrote in message
news:i7ac4vcq357s92danjctjisbouiqlc2ka0@4ax.com...
> Winston Kotzan wrote:
>
> >I am converting an encryption program from another language to run on a
web
> >server in Perl.  The encryption algorithm does shift operations on 4-byte
> >integers.  Can I safely use a plain scalar (like $myvariable) and trust
that
> >it is four bytes long and that bits shifted off the left will be removed?
> >If not, do you have a suggestion to ensure this happens?
>
> Yes, I do believe that Perl integers are garanteed to be at  least 4
> bytes long.
>
> But I agree wit hthe other posters: why not use one of the existing
> encryption modules, that other people have written, and which have
> already been thoroughly tested?
>
> <http://search.cpan.org/search?query=crypt&mode=module>
>
> --
> Bart.




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

Date: 7 Feb 2003 10:58:54 -0800
From: duanrt@hotmail.com (duanrt)
Subject: how to check variable assigned in child process during fork in perl
Message-Id: <e6cc4b17.0302071058.63b9ee35@posting.google.com>

Hi,

I have two questiones in using fork. And I tried many ways but still
can not make it work.

Q1:
I have a varialbe called "$passed". Depending on the running in the
parent or child processes, that variable will set to 0 if the test is
failed. But after exit out of the fork, $passed change back to 1 again
even though I changed it to 0 in the child process.

Q2: 
In the child process, if there is an error, I will set $passed to 0,
and want exit from the for loop ($i) at once. I use "exit(1)", but it
only exit from $p loop, but not $i loop. And I want it exit if
$passed=0, but at this moment, $passed becomes 1 again.

Any suggests or help will be highly appreciated.

I attached my program here:



#!/opt/local/bin/perl

&run_test();

sub run_test{
    my $passed = 1;


    for ($i = 0; $i < 3; $i++) {
    if ($pid = fork) {
            # do something here......
            # $passed maybe changed to 0, but we do not care here

            # wait for any children to complete
            wait;
    }
    elsif (defined $pid) {

            for (my $p = 0; $p < 3; $p++)
            {
                my $temp = 1;

                if ($temp = 1) {
                    $passed = 0;    # Q1: it is 0 now, but once out of
                                    # the fork, it is 1 again.

                    print "passed in child process =$passed\n";
                    exit(1);       # Q2: i want exit from all the for
loops,
                                   # not only $p one, but $i loop.
                }
            }

            # exit out of the child
            exit (0);
        }
        else {
            print "could not fork...\n";
        }
        
        # Q: now $passed has changed to 1 
        print "passed after each fork=$passed\n";

        if ($passed = 0) {
            exit(1);
        }
    }

    # Q: now $passed has changed to 1 
    print "passed out of fork =$passed\n";

    if ( ! $passed ) {
        print "test is not passed\n";
    }
    else {
        print "test is passed\n";
    }
}

1;



I also attached the output:

777 localhost//> perl test.pl
passed in child process =0
passed after each fork=1
passed in child process =0
passed after each fork=0
passed in child process =0
passed after each fork=0
passed out of fork =0
test is not passed


Thanks a lot.

Duan


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

Date: 7 Feb 2003 19:16:05 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: how to check variable assigned in child process during fork in perl
Message-Id: <b210ll$apg$1@nets3.rz.RWTH-Aachen.DE>

Also sprach duanrt:

> I have two questiones in using fork. And I tried many ways but still
> can not make it work.
> 
> Q1:
> I have a varialbe called "$passed". Depending on the running in the
> parent or child processes, that variable will set to 0 if the test is
> failed. But after exit out of the fork, $passed change back to 1 again
> even though I changed it to 0 in the child process.

By definition, processes do not share any data. After the fork, the
child gets a copy of the variables but they are no longer connected to
the variables the parent-process has access to. So either you use
threads that allow sharing of data or you use shared memory. This can be
done with IPC::Shareable for instance.

> Q2: 
> In the child process, if there is an error, I will set $passed to 0,
> and want exit from the for loop ($i) at once. I use "exit(1)", but it
> only exit from $p loop, but not $i loop. And I want it exit if
> $passed=0, but at this moment, $passed becomes 1 again.

Sorry, I don't understand this at all, not even with your code. The
$p-loop (as you called it) is in your child so if your child exits the
parent process is not affected by that.

As for your code: There was something seriously wrong with your identing
which made it rather hard to follow.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: Fri, 07 Feb 2003 15:14:55 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: how to check variable assigned in child process during fork in perl
Message-Id: <3E4413BF.CC27C9AF@earthlink.net>

duanrt wrote:
[snip]
>     if ($pid = fork) {
[snip]
>         wait;
>     }
>     elsif (defined $pid) {
[snip stuff which assigns to $passed]
>         exit (0);
>     }
[snip]
>         # Q: now $passed has changed to 1
>      print "passed after each fork=$passed\n";

You've only assigned to $passed inside of the child process, but here
you are examining it in the parent process.  Once you fork, the parent
and child are independent -- no changes to variables in the child effect
variables in the parrent.

>        if ($passed = 0) {
>            exit(1);
>        }

This assigns zero to the $passed variable, and then checks if $passed is
true... which of course it never is.

What you really need is something like this:

   unless( defined(my $pid = open(my ($fh), "-|")) ) {
      print "Couldn't fork: $!";
      return;
   }
   if( $pid ) {
      # This is the parent.
      $passed = do { local $/; <$fh> };
      close $fh;
   } else {
      for my $p ( 0 .. 2 ) {
         if( ... ) {
            print $fh "0";
            exit;
         }
      }
      print $fh "1";
      exit;
   }

-- 
"So, who beat the clueless idiot today?"
"Well, we flipped for it, but when Kuno
 landed, he wasn't in any shape to fight."
"Next time, try flipping a *coin.*"


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

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


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