[7423] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1048 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Sep 19 11:17:19 1997

Date: Fri, 19 Sep 97 08:00:38 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 19 Sep 1997     Volume: 8 Number: 1048

Today's topics:
     Re: Bad syntax check with perl -c <merlyn@stonehenge.com>
     Re: Capitalizing the first letter via regex <seay@absyss.fr>
     Re: Capitalizing the first letter via regex (Bart Lateur)
     Confusion with chop/chomp (Steve Lamb)
     Re: Confusion with chop/chomp (Greg Bacon)
     dbmopen w/ undef problem <net0018@edwardjones.com>
     dereferencing code references (was Re: Nested subs and  <seay@absyss.fr>
     Re: dereferencing code references (was Re: Nested subs  (Greg Bacon)
     Re: Does Perl have a "blank" regex character class code <merlyn@stonehenge.com>
     Re: echo $status <merlyn@stonehenge.com>
     Re: echo $status <seay@absyss.fr>
     Re: GNU sort & Perl (Jason Gloudon)
     Re: Help with indirect file handles <day@unicc.org>
     How to clear the remark of lang C? (CherngJyh Lin)
     Re: How to clear the remark of lang C? (Mike Heins)
     Re: How to clear the remark of lang C? (Tad McClellan)
     Re: Impossible dbm size (Jim Dennis)
     Re: Linux Perl Bug ? <seay@absyss.fr>
     Re: Open or die is mostly fatal (Oops, forgot the code, <rootbeer@teleport.com>
     perl 5.004_03 build error <marek.grinberg@mailbox.swipnet.se>
     Re: Perl equivalent to #ifdef in C? <seay@absyss.fr>
     Re: Ret Value from Opened Pipe <seay@absyss.fr>
     Re: Rounding numbers problem <seay@absyss.fr>
     Re: Sequential replace -- big problem? (Tad McClellan)
     Re: Sockets <seay@absyss.fr>
     Re: Something of the day script <seay@absyss.fr>
     Re: ssh and perl <seay@absyss.fr>
     Re: What does this error message mean? <seay@absyss.fr>
     Re: What does this error message mean? <rootbeer@teleport.com>
     Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)

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

Date: 19 Sep 1997 05:50:10 -0700
From: Randal Schwartz <merlyn@stonehenge.com>
To: Brett Denner <Brett.W.Denner@lmtas.lmco.com>
Subject: Re: Bad syntax check with perl -c
Message-Id: <8cd8m5o4cd.fsf@gadget.cscaper.com>

>>>>> "Brett" == Brett Denner <Brett.W.Denner@lmtas.lmco.com> writes:

Brett> I checked the syntax (with perl -c) of the following script:
Brett>     $DIGITS = '\d+';
Brett>     / $DIGITS (?!a/;   

Brett> and the syntax was okay.  The following script

Brett>     / \d+ (?!a/;      

Brett> however, produces this error: 'unmatched () in regexp'.

Brett> It looks like there should be unmatched parentheses errors in both
Brett> cases, because the (?! is not balanced by a ).  Is this a bug in perl
Brett> or have I overlooked something?

When there's a variable in a regex, Perl cannot compile the regex
until runtime.  "perl -c" does only the compile-time checks.  Without
compiling the regex, there's no way for Perl to know if the syntax is
good or not.

Consider this:

	if (time % 2) {
		$RIGHT = ")";
	} else {
		$RIGHT = "";
	}
	while (<>) {
		print if /(.*$RIGHT/;
	}

Now, half the time the program runs, it'd be a valid regex, and the
other half, it isn't!

You could argue that there is no sequence in your $REGEX that would
possibly make that regex valid, but I don't really want Perl to have
that much AI at compile time. It's scary enough as it is.:-)

print "Just another Perl hacker," # but not what the media calls "hacker!" :-)
## legal fund: $20,990.69 collected, $186,159.85 spent; just 347 more days
## before I go to *prison* for 90 days; email fund@stonehenge.com for details

-- 
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <merlyn@stonehenge.com> Snail: (Call) PGP-Key: (finger merlyn@ora.com)
Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me


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

Date: Fri, 19 Sep 1997 12:09:40 +0200
From: Doug Seay <seay@absyss.fr>
Subject: Re: Capitalizing the first letter via regex
Message-Id: <34224F64.56935553@absyss.fr>

Christopher Zukowski wrote:
> 
> To all,
> 
>     I am trying to figure out how to capitalize the first character of
> an otherwise all lower case string.
> 
> IE:  random
> want to be
>       Random
> 
> Any suggestions?

Yes.  RTFM.  What you want is in "perlfunc".

- doug


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

Date: Fri, 19 Sep 1997 11:25:47 GMT
From: bart.mediamind@tornado.be (Bart Lateur)
Subject: Re: Capitalizing the first letter via regex
Message-Id: <34275de2.14411880@news.tornado.be>

Christopher Zukowski <czukowski@qualcomm.com> wrote:

>    I am trying to figure out how to capitalize the first character of
>an otherwise all lower case string.

	s/(\w)/\u$1/;

To capitalize every word, try this:

	s/(\w+)/\u\L$1/g;

This will change
	NEW YORK
to
	New York

	Bart.


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

Date: 19 Sep 97 10:26:00 GMT
From: morpheus@no-spam.calweb.com (Steve Lamb)
Subject: Confusion with chop/chomp
Message-Id: <slrn624kpp.bbh.morpheus@web1.calweb.com>


    OK, confusion abound here.  The relevant portion of code that is causing
me problems is below.  It works as it stands but here are the problems I am
having.  When chomp($remainder) is at #1 or #2 it does not get pas the 
if($dest eq $channel) line.  I know that it is true because of the print
before the if statement spews out all the data as it is at tha time.  I even
went so far as to isolate the $dest on its own line with a print.  It
matches $channel.  No matter what, if chomp($remainder) is before that line
it will not go past it.  Why?  What am I missing here?  I'd much rather have
to do the chomp once this this is going to be a huge series of if and elsif.

  ($from, $type, $remainder) = split(/ /,$line,3);
  $from =~ /^:(.*)!(.*)$/;
  $from = $1;
  $host = $2;
  #1 chomp($remainder);
  print("$from $host $type\n");
  # Basic autop
  if (($type eq "JOIN") && ($from eq "GreydMiyu"))
  {
    &sl("MODE $channel +o $from")
  }
  elsif ($type eq "PRIVMSG") # Standard messages
  {
    ($dest, $remainder) = split(/ /,$remainder,2);
    #2 chomp($remainder);
    print("inmsg: $from $type $dest $remainder\n");  # in case of errors
    if ($dest eq $channel)
    {
      print("-$from$remainder");
      if ($remainder =~ /:m (.*)/)
      {
        chomp($chompme = $1);
        if ($chompme eq "version")
        {
          &version;
        }

-- 
             Steve C. Lamb             | Opinions expressed by me are not my
    http://www.calweb.com/~morpheus    | employer's.  They hired me for my
CC: from news not wanted or appreciated| skills and labor, not my opinions!
---------------------------------------+-------------------------------------


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

Date: 19 Sep 1997 13:59:52 GMT
From: gbacon@adtran.com (Greg Bacon)
Subject: Re: Confusion with chop/chomp
Message-Id: <5vu0go$d8i$2@info.uah.edu>

In article <slrn624kpp.bbh.morpheus@web1.calweb.com>,
	morpheus@no-spam.calweb.com (Steve Lamb) writes:
:     OK, confusion abound here.  The relevant portion of code that is causing
: me problems is below.  It works as it stands but here are the problems I am
: having.  When chomp($remainder) is at #1 or #2 it does not get pas the 
: if($dest eq $channel) line.

IRC channel names are case insensitive, but you don't seem to be taking
this into account (from the code snippet shown).  Try something like

    if (lc($dest) eq lc($channel)) {
        ...;
    }

You'll want to cache the lowercase versions if you use them much.  Still
I'm not sure why chomping or not chomping would affect this (unless you
have a bizarre value of $/).

FWIW, IRC messages are supposed to be terminated by a \r\n pair.  You
should only have to strip that once with something like

    $line =~ s/(?:\r?\n)+$//;

by the policy of being liberal in what you accept.

Hope this helps,
Greg
-- 
open(G,"|gzip -dc");$_=<<EOF;s/[0-9a-f]+/print G pack("h*",$&)/eg
f1b88000b620f22320303fa2d2e21584ccbcf29c84d2258084
d2ac158c84c4ece4d22d1000118a8d5491000000
EOF


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

Date: Fri, 19 Sep 1997 09:10:26 -0500
From: Paul Bardo <net0018@edwardjones.com>
Subject: dbmopen w/ undef problem
Message-Id: <342287D2.66DA@edwardjones.com>

While I try to do the following command with perl -w,

   dbmopen (%Prefix, "Prefixes", undef)
      || die "I cannot open the Prefix database";

I get the following error messages, although the program runs correctly.

   Use of uninitialized value at perltest line 7.
   Use of uninitialized value at perltest line 7.

If I try this,

   dbmopen (%Prefix, "Prefixes", defined)
      || die "I cannot open the EtherPrefix database";

the program still runs correctly, but I get no error messages.

The Camel book suggest the first way with the "undef".  Will I have
problems using "defined"?  Why didn't the "undef" work?

Thanks for any insight.

paul.bardo@edwardjones.com


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

Date: Fri, 19 Sep 1997 14:03:32 +0200
From: Doug Seay <seay@absyss.fr>
Subject: dereferencing code references (was Re: Nested subs and local vs my_
Message-Id: <34226A14.265CE505@absyss.fr>

Bart Lateur wrote:
> 
> gbacon@adtran.com (Greg Bacon) wrote:
>
> >        $inref->();
>
> Are the next two statements functionally equivalent? If not, what's the
> difference?
> 
>         $inref->();
>         &$inref();

I've never seen Greg's way before for code references.  I've always done
it with your second approach.  The first would make me thing that $inref
was an object.  My little test case seems to imply that they are
equivalent.

- doug

~/bin> cat fp
#!/usr/local/bin/perl -w

use strict;

my $ref = sub { print "ARGS: @_\n"; };

$ref->(1,2);
&$ref( qw(fred wilma) );
~/bin> fp
ARGS: 1 2
ARGS: fred wilma


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

Date: 19 Sep 1997 13:44:33 GMT
From: gbacon@adtran.com (Greg Bacon)
To: Doug Seay <seay@absyss.fr>
Subject: Re: dereferencing code references (was Re: Nested subs and local vs my_
Message-Id: <5vtvk1$d8i$1@info.uah.edu>

[Posted and mailed]

In article <34226A14.265CE505@absyss.fr>,
	Doug Seay <seay@absyss.fr> writes:
: I've never seen Greg's way before for code references.  I've always done
: it with your second approach.  The first would make me thing that $inref
: was an object.  My little test case seems to imply that they are
: equivalent.

The $subref->(args, ...) syntax for calling a sub through a reference to
it is new to 5.004.  From perldelta(1):

    New and changed syntax

    $coderef->(PARAMS)
        A subroutine reference may now be suffixed with an
        arrow and a (possibly empty) parameter list.  This
        syntax denotes a call of the referenced subroutine,
        with the given parameters (if any).

        This new syntax follows the pattern of
        $hashref->{FOO} and $aryref->[$foo]: You may now
        write &$subref($foo) as $subref->($foo).  All of
        these arrow terms may be chained; thus,
        &{$table->{FOO}}($bar) may now be written
        $table->{FOO}->($bar).

You can (?:thank|curse) Randal for suggesting it and Chip for creating
the patch for it. :-)

Greg
-- 
open(G,"|gzip -dc");$_=<<EOF;s/[0-9a-f]+/print G pack("h*",$&)/eg
f1b88000b620f22320303fa2d2e21584ccbcf29c84d2258084
d2ac158c84c4ece4d22d1000118a8d5491000000
EOF


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

Date: 19 Sep 1997 05:52:30 -0700
From: Randal Schwartz <merlyn@stonehenge.com>
To: Brett Denner <Brett.W.Denner@lmtas.lmco.com>
Subject: Re: Does Perl have a "blank" regex character class code
Message-Id: <8cafh9o48h.fsf@gadget.cscaper.com>

>>>>> "Brett" == Brett Denner <Brett.W.Denner@lmtas.lmco.com> writes:

Brett> I know that in a regex \s stands for the whitespace character class 
Brett> [ \t\n\r\f\v].  Is there something similar for a "blankspace" character
Brett> class that only contains [ \t], for example?  Typing [ \t]* in a regex
Brett> is so much uglier than \s*, but occasionally \s* grabs the newline
Brett> as well, which I don't always want.

You can subtract from the predefined character classes by a little
bit of clever sorcery:

	[^\S\n]

Read this as "not either non-whitespace or newline", which turns
out to be something like "\s minus \n".  Pretty handy when you want
all the letters and digits except the vowels:

	[^\Waeiou]

and so on.

print "Just another Perl hacker," # but not what the media calls "hacker!" :-)
## legal fund: $20,990.69 collected, $186,159.85 spent; just 347 more days
## before I go to *prison* for 90 days; email fund@stonehenge.com for details

-- 
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <merlyn@stonehenge.com> Snail: (Call) PGP-Key: (finger merlyn@ora.com)
Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me


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

Date: 19 Sep 1997 05:58:00 -0700
From: Randal Schwartz <merlyn@stonehenge.com>
To: dizzy@soda.CSUA.Berkeley.EDU (David Zeng)
Subject: Re: echo $status
Message-Id: <8c7mcdo3zb.fsf@gadget.cscaper.com>

>>>>> "David" == David Zeng <dizzy@soda.CSUA.Berkeley.EDU> writes:

David> Uh... dumb me.  Perl runs Bourne Shell, and "echo $status" should be
David> "echo $?" in Borune Shell.

But even that's not quite right.  You don't launch another child
process to find the status of the last child process.  Perl already
knows the value... it's in $?.

	@who_there = `who`;
	$who_status = $?;

print "Just another Perl hacker," # but not what the media calls "hacker!" :-)
## legal fund: $20,990.69 collected, $186,159.85 spent; just 347 more days
## before I go to *prison* for 90 days; email fund@stonehenge.com for details

-- 
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <merlyn@stonehenge.com> Snail: (Call) PGP-Key: (finger merlyn@ora.com)
Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me


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

Date: Fri, 19 Sep 1997 15:32:51 +0200
From: Doug Seay <seay@absyss.fr>
Subject: Re: echo $status
Message-Id: <34227F03.727EF327@absyss.fr>

David Zeng wrote:
> 
> Uh... dumb me.  Perl runs Bourne Shell, and "echo $status" should be
> "echo $?" in Borune Shell.
> 
> Sorry for the trouble and wasted bandwidth.

Look up $? in perlvars because you might be able to do away with that
echo completely.

- doug


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

Date: 19 Sep 1997 14:51:18 GMT
From: jgloudon@bbn.com (Jason Gloudon)
Subject: Re: GNU sort & Perl
Message-Id: <5vu3h6$go6$1@daily.bbnplanet.com>

In article <34216AD0.74EC3C04@ic.ac.uk>,
Tony Curzon Price  <j.curzon-price@ic.ac.uk> wrote:
>Thanks, Tom. You're certainly right that it wasn't a problem with my
>perl expression.
>
>I got to the bottom of the problem, and it might be a slightly salutary
>lesson for other perl beginners:
>
>I eventually realised that the sort was writing the sorted file when
>doing it from the shell, but it wasn't even writing the file from perl
>(a permission problem). But I had just assumed that if it was getting to
>the end of my script without crashing/squealing, it was actually
>executing the instructions. I hadn't made it verbose enough, and wasn't
>trapping errors...
>
>Tony
>
>-- 
>Tony Curzon Price    Imperial College   
>http://is.eunet.ch/Customers/curzon/index.html
>(During GMT office hours, or so):
>http://epgphd.et.ic.ac.uk/www/

Just a general comment on using sort this way. If you are sorting really big
"temporary" files (in my case often 100Mb+) that you are going to read back 
into perl you can speed up this process by reading the sorted file
from sort's standard output. This avoids reduces writing the file to disk
at least once, which is a big plus if you are going to modify that sorted 
output. I knocked 25% off the run time of some processing that I was doing this
way. 

Jason Gloudon


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

Date: Fri, 19 Sep 1997 14:09:09 +0200
From: Ryan Day <day@unicc.org>
Subject: Re: Help with indirect file handles
Message-Id: <34226B65.5985CBBE@unicc.org>

Ronald Sercely (emd) {xxxx} wrote:

> I have a perl script:
> <snip>
> foreach $directory (grep(!/^\.\.?$/,readdir(GLOBALS) ) ) {
>     foreach $string (@ARGV) {   # open up one file per input string
>         print $OUT{$string};    # works
>         print $OUT{$string} "xx"; # syntax error
>     }
> }

> What does the first print the inderect file work
> but the second give a syntax error?

You say that the first print statement works, but I don't think so.
I agree that it may not cause a a syntax error during compile, but
at run-time it complains.  I commented out your second print statement
to test this theory.  With v4.036, it segfaults.  With v5.003, I get the

error "Bad symbol for filehandle."  This was my suspicion.  What
you think is your filehandle, is not really a filehandle.  Maybe, try an

array of filehandles instead of a hash.

Ryan Day



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

Date: Fri, 19 Sep 1997 09:15:43 GMT
From: a00lcj00@mail.dt.nchc.gov.tw (CherngJyh Lin)
Subject: How to clear the remark of lang C?
Message-Id: <34223d9c.7383393@netnews.nchc.gov.tw>

Hi,
  Is there more efficient way to clear all the /* ... */ remark
in lang C by using perl language?? Thanks!

I had wrote one as below, but I think there must be another 
better answer!

######################
$file = "file.cpp";
open(FILE,"$file");
@data = <FILE>;
close(FILE);

$data = join("",@data);
$data =~ s/\/\*(.*?)\*\/\n?//sg;
open(FILE, ">$file");
print FILE $data;
close(FILE);


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

Date: 19 Sep 1997 10:05:49 GMT
From: mheins@prairienet.org (Mike Heins)
Subject: Re: How to clear the remark of lang C?
Message-Id: <5vtipt$n79$1@vixen.cso.uiuc.edu>

CherngJyh Lin (a00lcj00@mail.dt.nchc.gov.tw) wrote:
: Hi,
:   Is there more efficient way to clear all the /* ... */ remark
: in lang C by using perl language?? Thanks!
: 
: I had wrote one as below, but I think there must be another 
: better answer!
: 
: ######################
: $file = "file.cpp";
: open(FILE,"$file");
: @data = <FILE>;
: close(FILE);
: 
: $data = join("",@data);
: $data =~ s/\/\*(.*?)\*\/\n?//sg;
: open(FILE, ">$file");
: print FILE $data;
: close(FILE);

Why do you want to clear comments?  Obfuscation?  I am sure
you wouldn't do that -- there must be a good reason. Perhaps
distribution space, but it is a bad bargain. On my test, it
removed 350K of comments out of a meg of code -- that would
compress to less than 120K.

I also suggest as a matter of good practice that you check your 
open() calls.

This would be the best I could do:

#!/usr/bin/perl -w

undef $/;
foreach $file (@ARGV) {
    rename $file, "$file.orig";
    open(FILE, "$file.orig") or die "open $file.orig: $!\n";
    $data = <FILE>;
    close FILE;

    open (FILE, ">$file") or die "creat $file: $!\n";
    print FILE split m:/\*[\000-\377]*?\*/:, $data;
    close FILE;
}

That processed a meg of C source split over 40 files
in under a second on my machine. It even compiled
once I added [^"] to the regex, as this method will get
blown away by random "/*" literals in the input source.
This is not too uncommon a thing to see in UNIX.

It would be called with args of the file names you wished
to process. It prevents at least two variable copies compared
to your code -- and I removed the "\n?" which is dubious
at best.  Better an extra blank line then multiple #include
statements on the same line.

-- 
Regards,
Mike Heins

This post reflects the
opinion of my employer.


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

Date: Fri, 19 Sep 1997 06:22:47 -0500
From: tadmc@flash.net (Tad McClellan)
Subject: Re: How to clear the remark of lang C?
Message-Id: <7antv5.s6d.ln@localhost>

CherngJyh Lin (a00lcj00@mail.dt.nchc.gov.tw) wrote:
: Hi,
:   Is there more efficient way to clear all the /* ... */ remark
: in lang C by using perl language?? Thanks!

: I had wrote one as below, but I think there must be another 
: better answer!


Perl FAQ, part 6:

  "How do I use a regular expression to strip C style comments from a file?"


--
    Tad McClellan                          SGML Consulting
    tadmc@flash.net                        Perl programming
    Fort Worth, Texas


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

Date: 19 Sep 1997 01:32:14 -0700
From: jimd@antares.starshine.org (Jim Dennis)
Subject: Re: Impossible dbm size
Message-Id: <m390wtbt69.fsf@antares.starshine.org>


> [cc'd automagically to original author]

On Wed, 17 Sep 1997 15:28:12 -0400, Charles Jaimet <cjaimet@ican.net>
wrote:

>> My server says my dbm database file takes up 4,800 GB!

> Way cool... :-)

>> The server doesn't have nearly that much storage space and when I dump
>> the file to text it's only 239 kB.

> Ok.

>> The database and all the scripts I use to add/display/modify records
>> work fine. There are 1067 records and no duplicates. 
>>
>> What's wrong? I've racked my brain and pored through books and online
>> guides but I can't find the error.

> I believe DBM files (like core files) can contain lots of large
> "holes" that are still reported as used space. I think it's documented
> in the man pages for one of the DBM implementations, but I don't have
> a Unix box handy at the moment to check.

> Either that, or someone on this group explained it a year ago, and
> I've just remembered... :-)

> Jeremy

	Any regular Unix file can have "holes" in it.  I guess it 
	is somewhat more common for these to occur in dbm and 
	database files (dbm are hashed/index files created and
	manipulated with the various dbm, ndbm, and gdbm libraries).
	
	If I understand it correctly the way that holes are 
	represented has to do with the inode's structure.  Inodes
	have a number of fields with point to the filesystem blocks
	in which the file's data is stored.  

	When/if a normal (i.e. contiguous data) file grows large 
	enough to fill all the blocks that the inode can directly 
	point to -- a couple of "extents" are use to point at a set 
	of "indirect" blocks. These "indirect" blocks  then point to 
	more data blocks.  I think there are at least two levels of 
	inode block allocation indirection supported by most Unix 
	filesystems.

	Now, when a file has hole in it -- I guess this is done by
	skipping some of these block allocation fields and allocating
	blocks that are logically beyond the contiguously allocated
	blocks (which are filled with characters of the file's contents).

	It thus makes sense that the size reported by the filesystem
	driver (reading the inode information) could be much larger than
	the actual contents of the file.

	It also makes sense that dbm files would have holes in them.
	I take key value pairs and use a hash (searching/indexing)
	function to obtain an approximate offset at which it I want to
	store my key/value pair.  By using this hash value directly
	as a logical offset I don't have to have a separate file
	or part of the file for an index, and I don't have to load
	an index into core or constantly do linear searches through
	the index.

	(Knuth's "Art of Programming" series has quite a bit to 
	say about hashing and searching algorithms.  I glanced 
	over it back in high school -- and highly recommend it to 
	anyone that was confused by that last paragraph).


-- 
Jim Dennis  (800) 938-4078		consulting@starshine.org
Proprietor, Starshine Technical Services:  http://www.starshine.org
        PGP  1024/2ABF03B1 Jim Dennis <jim@starshine.org>
        Key fingerprint =  2524E3FEF0922A84  A27BDEDB38EBB95A 


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

Date: Fri, 19 Sep 1997 12:08:11 +0200
From: Doug Seay <seay@absyss.fr>
Subject: Re: Linux Perl Bug ?
Message-Id: <34224F0B.4B80F871@absyss.fr>

[posted and mailed]

Brahma Desta wrote:
> 
> The following piece of code, in Linux Perl 5.004, only
> works if the first two print statements are either
> removed or are of the form print "num1 = ", $num1;
> 
> In the current form, the xor produces a 0 whereas the
> real answer should be 55217.
> 
> I guess it has something to do with the conversion
> between strings and numbers ?
> 
> $num1 = 11230;
> $num2 = 64623;
> 
> print "num1 = $num1 \n"
> print "num2 = $num2 \n"
> 
> print int($num1 ^ $num2);
> 
> System Specs: Slackware 3.3 : kernel version 2.0.30 : Pentium 200 MMX.


I am running perl5.004 on RedHat 4.0 (linux 2.0.18) and it works well
for me.  Here is my code and the results.  I get what you expect for the
XOR.  If the same code doesn't work for you, I'd say it was time for you
to upgrade something or another.

- doug

--- code ---

#!/usr/local/bin/perl -w

use strict;

my $num1 = 11230;
my $num2 = 64623;
my $num3 = int($num1 ^ $num2);

printf "num1=%d(%x) num2=%d(%x) num3=%d(%x)\n", $num1, $num1,
		$num2, $num2, $num3, $num3;

--- results ---

~/bin> le
num1=11230(2bde) num2=64623(fc6f) num3=55217(d7b1)


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

Date: Fri, 19 Sep 1997 07:53:22 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: Guy Doucet <gdoucet@ait.acl.ca>
Subject: Re: Open or die is mostly fatal (Oops, forgot the code, here it is:)
Message-Id: <Pine.GSO.3.96.970919075238.11506P-100000@julie.teleport.com>

On Thu, 18 Sep 1997, Guy Doucet wrote:

> > The following code is giving me a headache. It always opens the file the
> > first time, but more often than not, it dies the second time.

What error message does it give when it dies?

-- 
Tom Phoenix           http://www.teleport.com/~rootbeer/
rootbeer@teleport.com  PGP   Skribu al mi per Esperanto!
Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/
              Ask me about Perl trainings!



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

Date: Fri, 19 Sep 1997 16:13:21 +0200
From: Marek Grinberg <marek.grinberg@mailbox.swipnet.se>
Subject: perl 5.004_03 build error
Message-Id: <34228880.4348BD13@mailbox.swipnet.se>

Hello,

Running SunOS 5.5 in a Sparc 20 box, I managed to build perl 5.004_03
without any problems.
Then I fetched db-1.85 from www.sleepycat.com, built it, and installed
it.
A following build of perl errors off:
	Making DB_File (dynamic)
LD_RUN_PATH="/usr/local/lib" gcc -o ../../lib/auto/DB_File/DB_File.so
-R/usr/local/lib -shared -L/usr/local/lib DB_File.o    -L/usr/local/lib
-ldb 
Text relocation remains			      referenced
    against symbol		    offset	in file
free                                0x894     
/usr/local/lib/libdb.a(hash_bigkey.o)
free                                0x310     
/usr/local/lib/libdb.a(hash.o)
	....
<unknown>                           0x1040    
/usr/local/lib/libdb.a(hash_page.o)
<unknown>                           0x1360    
/usr/local/lib/libdb.a(hash_page.o)
<unknown>                           0x137c    
/usr/local/lib/libdb.a(hash_page.o)
<unknown>                           0x1368    
/usr/local/lib/libdb.a(hash_page.o)
ld: fatal: relocations remain against allocatable but non-writable
sections
*** Error code 1
make: Fatal error: Command failed for target
`../../lib/auto/DB_File/DB_File.so'
Current working directory /usr/GNU/perl/perl5.004_03/ext/DB_File
*** Error code 1
make: Fatal error: Command failed for target
`lib/auto/DB_File/DB_File.so'

Any hints ?
-- 
Regards,

Marek Grinberg

|------------------------------------------------|
| home: mailto:marek.grinberg@mailbox.swipnet.se |
| work: mailto:magri@msc.se                      |
| web:  http://www.msc.se                        |
|------------------------------------------------|


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

Date: Fri, 19 Sep 1997 14:00:25 +0200
From: Doug Seay <seay@absyss.fr>
Subject: Re: Perl equivalent to #ifdef in C?
Message-Id: <34226959.7354A4BE@absyss.fr>

[cross-posted to Eli's group to ensure that he'll see it, but trimmed it
from the follow ups]

Eli the Bearded wrote:
> 
> In article <341FB593.4C7848C@absyss.fr>, Doug Seay  <seay@absyss.fr> wrote:
> > Yes, there is an quivalent structure in Perl.  It is "if".  It would be
> > used like
> 
> Not equivalent.
> 
> #define EFFICIENT_SHORTCUT 0
> #ifdef EFFICIENT_SHORTCUT
> 
> At some point I will implement this function, but not yet.
> 
>         if (can_do_efficiently())
>                 do_something_efficiently();
>         else
> 
> #else
> 
>                 do_something();
> #endif

You are correct that it is possible to do funny things with #ifdef, but
since Perl uses constant folding (IIRC) as part of the compilation phase

	use constant SHORTCUT => 0;
	if ( SHORTCUT )
		{ fast(); }
	else
		{ standard(); }

will be optimized down to the same bytecode as

	use constant SHORTCUT => 0;
	standard();

Nothing was gained by using the preprocessor.  If you looked for
odd-ball cases, you could find things where cpp would be a win, but I
think that they would be few and far between.


> > It is almost identical to the mechanism found in C and is functionally
> > identical to what you had described.
> 
> Even barring that I don't bother using some sort of comment delimitor
> in the ifdef'ed out code, note how the preprocessor cuts across the
> c syntax.

True, but who cares?  It isn't done all that much and is rarely
required.  Much of #ifdefs in C can be removed by constant folding
(assuming you use a modern C compiler).  The big win for #ifdefs are
include files/specific macros, but they are done at preprocessing time
so they cannot be done with C.  As a note, if you want to do something
like

	#ifdef	_WIN32
	#include <bill_gates.h>
	#endif

in Perl, you would need an eval() to do the 'use BillGates;' bit as
use() is done immediately (like a BEGIN block).  But I prefer eval() to
-P because that is staying within the Perl language, not using a foreign
tool to get the job done.  Of course, this is a personal opinion and
there are always other ways.

BTW - Your comment delimiter bit was a valid point.  That cannot be
easily reproduced with "if".  I'll group that in there with my "odd-badd
cases" if you don't mind.


> How about if you have a source file and a "compiled" file, with a makefile
> that uses cpp as the compiler to produce "compiled" perl script?

I'm not sure what you are asking here.  I have had a perl script launch
byacc to compile a specs file, then slurp in that output .pl file with
require() and then use the functions just defined.  Is that kinda like
what you were talking about?  It didn't use cpp nor did it use make (but
should have or cached the results as it recompiled the specs every
time).

- doug

PS - If perl5 doesn't do constant folding, my whole arguement falls to
pieces and I'll agree that -P does have a valid role.  I still won't use
it, but I won't get up on my soap box to preach against it.  I read the
function fold_constants() in op.c, but I have no idea what it is doing. 
Could someone who understands this gibberish give a summary.  Thanks.


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

Date: Fri, 19 Sep 1997 14:57:36 +0200
From: Doug Seay <seay@absyss.fr>
Subject: Re: Ret Value from Opened Pipe
Message-Id: <342276C0.50E5E86D@absyss.fr>

Jim Dennis wrote:
> 
>         How do I get the value returned by my open()'d process?
> 
>         Given code like:
> 
>                 open (PROG, "|$myprog");
>                         print PROG "some input";
>                         print PROG "some more input";
>                 #???
>                 close(PROG);
>                 print $?;  #???
> 
>         My program is supposed to take to lines of input --
>         and return one of several possible values.  I want
>         my perl code to be able to distinguish between them,
>         and act accordingly.

Do you mean that the program named in $myprog is supposed to exchange
input and output with this code snippet?  If so, never forget about the
deadlocks.  Look at perlipc and IPC::Open2() for something that might do
what you want.  

>         What I currently get is either 0 or 256 -- where I program
>         is returning 0, 1, 255, 254, or 253 for various test
>         cases.
>
>         Why doesn't this work as one would expect?

If you just want return codes, I don't see any problems.  Here is my
test case and the little exiting program ($myprog in your example).  Try
playing with this and see what results you get.

- doug

--- results ---

~/bin> e
result=0 (0) (0)
result=4352 (0) (17)
~/bin> 

--- main code ---  "e" ---

#!/usr/local/bin/perl -w
use strict;

sub exiter(@)
{
open(P, "| exiter") || die "open() failed: $!\n";
print P @_;
close(P);
my $rsig = $? % 256;
my $rexit = ($?/256) % 256;
printf "result=%d (%d) (%d)\n", $?, $rsig, $rexit;
}

exiter("0\n");
exiter("17\n");

--- child process --- "exiter" ---
 
#!/usr/local/bin/perl -w
use strict;
my $num = <STDIN>;
exit int($num);


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

Date: Fri, 19 Sep 1997 13:04:08 +0200
From: Doug Seay <seay@absyss.fr>
Subject: Re: Rounding numbers problem
Message-Id: <34225C28.322975B4@absyss.fr>

David R. Nurse wrote:
> 
> I need to round some numbers to different numbers of decimal places and
> then to the nearest half, e.g.
> 
> to 2 decimal places:
> 1.34456 becomes 1.35
> 2.57876 becomes 2.60
> 
> to 1 decimal place:
> 
> 1.34456 becomes 1.5
> 2.57876 becomes 2.5
> 
> any of you Perl experts think of an easy way of doing this ?

Easy?  As in posting instead of reading the FAQ?

After putting my guru-beanie on and giving the propeller a good spin, I
looked into the FAQ.  I just used good ol' grep in the .../lib/perl5/pod
directory for "round" and I found

  perlfaq4.pod:=head2 Does perl have a round function?  What about
ceil() and floor()?

This means to "perldoc perlfaq4" and look for "Does perl have a round
function?  What about ceil() and floor()?".  Read the next few lines and
you'll find what you want, plus you'll know where to look for FAQs next
time.

- doug


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

Date: Fri, 19 Sep 1997 07:40:31 -0500
From: tadmc@flash.net (Tad McClellan)
Subject: Re: Sequential replace -- big problem?
Message-Id: <vrrtv5.3jd.ln@localhost>

CherngJyh Lin (a00lcj00@mail.dt.nchc.gov.tw) wrote:
: Hello:

:    I got a problem in processing a string replace. Could any
: body know how to write the "replace" subprogram as below?
: Thanks!

: ========================
: $before = "abcdefgabcd";
: @a = qw/ab efg cd/;
: @b = qw/AB EFG CD/;
: $after = &replace($before);
: ========================

:    $after must become: "ABcdEFGabCD"

: (SEQUENTIAL search @a in $before and replace with @b. NOTE that
:  first "cd" doesn't be replaced because of sequential search!)



sub replace {  # only works for constant strings....
   my($str) =  @_;
   my $pos=0;          # how far into the string we have gotten so far
   my $i;              # index into @a and @b

   for ($i=0; $i<@a; $i++) {
      if ( ($pos = index($str, $a[$i], $pos)) >= 0) { # matched it
         substr($str, $pos, length($a[$i])) = $b[$i];
      }
      else
         {print "Uh oh. Could not match '$a[$i]' in $str\n"}
   }

   return $str;
}


--
    Tad McClellan                          SGML Consulting
    tadmc@flash.net                        Perl programming
    Fort Worth, Texas


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

Date: Fri, 19 Sep 1997 15:06:59 +0200
From: Doug Seay <seay@absyss.fr>
Subject: Re: Sockets
Message-Id: <342278F3.C683756@absyss.fr>

Mike Heins wrote:
> 
> Paul Livi (plivi@toronto.cbc.ca) wrote:
> : Hi there,
> :
> : Has anyone had experience with opening _and_ using sockets with PERL?
> : I'm able to open a socket to my destination and keep it open but am
> : unable to transmit data across it. I've tried using the send() and
> : recv() commands but have had no success. Any ideas or references?
> :
> 
> I use them all of the time. The awsome thing about Perl sockets
> is that you need not send() and recv(), you can do things like
> 'print SOCKET $var' and 'while (<SOCKET>) { do_something($_); }'.
> 
> 'perldoc perlipc' will give you everything you need
> to know.

Let me throw in my $0.02 worth: use IO::Socket.  Your life will be
simplified.  "perldoc IO::Socket" will tell you what you need to know. 
You should still read perlipc as it has lots of good stuff, but use the
object.

- doug


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

Date: Fri, 19 Sep 1997 12:56:42 +0200
From: Doug Seay <seay@absyss.fr>
To: John Thomas Mosey <mosey@alpha3.csd.uwm.edu>
Subject: Re: Something of the day script
Message-Id: <34225A6A.56EB6BC6@absyss.fr>

[posted and mailed]

John Thomas Mosey wrote:
> 
> Does anyone out there know of a daily rotation script program. For
> example, quote of the day or picture of the day?
> 
> I'm not looking for a random roation, I'd like everyone to see the same
> thing for one 24 hour period. I'd just like to program the quotes and
> pictures for a month period at a time.

You could use localtime() and take the yday field which will change
every day.  Use this as an index into whatever list you are keeping.  Of
course, Jan 1 for every year will have the same value, so you'll need to
update your list once a year.

- doug


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

Date: Fri, 19 Sep 1997 12:53:20 +0200
From: Doug Seay <seay@absyss.fr>
To: "Sjofaasj!" <sjofaasj@geocities.com>
Subject: Re: ssh and perl
Message-Id: <342259A0.5EBE0733@absyss.fr>


Sjofaasj! wrote:
> 
> When I execute a command remotely by ssh (secure shell) on the
> command-line (eg ssh -l root machineX commandY) no problem, when I try
> to do the same in a Perl-script (something like system("ssh", "-l root",
> "machineX")), ssh returns "Server refused our key. Permission denied".

System has two possible parameter solutions: a single scalar expression
or a list.  The scalar form will call /bin/sh to do the work, so all the
funny metacharacters work.  It also parses parameters the way you are
used to.  You chose the list form, so nothing is done for you.  This
includes parsing parameters.  What you did was start ssh with (in C
code)

	argv[0] = "ssh";
	argv[1] = "-l root";

which is most likely not what you wanted.  You would have prefered

	argv[0] = "ssh";
	argv[1] = "-l";
	argv[2] = "root";

Because that is what the shell would have done (split on spaces).  You 
need to either merge everything into a single string or break that
second arg into two.  I'd go with the second because the process model
would be

	system("ssh -l root");		perl5 - /bin/sh - ssh
	system(qw(ssh -l root));	perl5 - ssh

This is all written up in the docs.  "perldoc -f system" should do it.

- doug

PS - The bits about /bin/sh could be different for non-Unix platforms. 
But the idea of one string is passed to the local interpreter and a
multi-element list is done manually should be the same.


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

Date: Fri, 19 Sep 1997 13:11:28 +0200
From: Doug Seay <seay@absyss.fr>
Subject: Re: What does this error message mean?
Message-Id: <34225DE0.27F0C171@absyss.fr>

Joseph Scott Stuart wrote:

	<snip>

> and I get the following error message at the end of my program:
> 
> Not a GLOB reference during global destruction (#1)

Try adding "use diagnostic;" at the top of your code.  This give a bit
more verbose error messages.  As for your glob() problem, I can't help. 
I don't like glob() as it uses csh, which is evil.  I stick with
'opendir;grep(readdir);closedir' .

- doug


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

Date: Fri, 19 Sep 1997 07:32:33 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: Joseph Scott Stuart <nospam@ll.mit.edu>
Subject: Re: What does this error message mean?
Message-Id: <Pine.GSO.3.96.970919072738.11506J-100000@julie.teleport.com>

On 18 Sep 1997, Joseph Scott Stuart wrote:

> I get the following error message at the end of my program:
> 
> Not a GLOB reference during global destruction (#1)
> 
> I do use the glob routine, 

 ...which has nothing to do with the GLOB referred to in the message. :-)
The GLOB is a typeglob, while the glob() routine is a filename glob -
totally different animals, even though they have very similar names.

That error should happen only if something internal to Perl (or a compiled
module) has a bug in it. If you can boil this down to a small program
(say, ten lines or so) which still shows this error with the most recent
version of Perl and of any compiled modules you're using, you should
report the error to the Perl developers or to the module's author. Thanks! 

-- 
Tom Phoenix           http://www.teleport.com/~rootbeer/
rootbeer@teleport.com  PGP   Skribu al mi per Esperanto!
Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/
              Ask me about Perl trainings!



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

Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 8 Mar 97)
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.misc (and this Digest), send your
article to perl-users@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.

The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.

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 V8 Issue 1048
**************************************

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