[19146] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1341 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jul 20 11:06:06 2001

Date: Fri, 20 Jul 2001 08:05:11 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <995641511-v10-i1341@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Fri, 20 Jul 2001     Volume: 10 Number: 1341

Today's topics:
    Re: ####  ADD 6 INCHES TO YOUR PENIS #####  2830 (Scott Porter)
    Re: (long) advice needed: how robust is this script ? a <will@cottay.net>
        CGI and open2 problem <rlaufer1@lucent.com>
    Re: dumb question <newspost@coppit.org>
        Extra bytes when writing file out (Bob Dubery)
    Re: Extra bytes when writing file out (Anno Siegel)
    Re: Extra bytes when writing file out <ubl@schaffhausen.de>
    Re: Extra bytes when writing file out <tward10@jaguar.com>
    Re: Extra bytes when writing file out (Bob Dubery)
        extracting string seperated multiline data... <jvs@india.ti.com>
    Re: extracting string seperated multiline data... (Tad McClellan)
        FAQ: Can I write useful Perl programs on the command li <faq@denver.pm.org>
    Re: Generic Language interpreter in Perl? <newspost@coppit.org>
    Re: Getting always corrupt files while uploading (cgi) <Georg.Vassilopulos@SoftwareAG.de>
        GPIB / Parrallel port <SPAM_lapenta_jm@yahoo.com>
        Hashes of arrays (David Anderson)
    Re: Hashes of arrays <bill.kemp@wire2.com>
    Re: Hashes of arrays <david.anderson@pern.co.uk>
    Re: Hashes of arrays (Tad McClellan)
    Re: Hashes of arrays <mjcarman@home.com>
    Re: ide for perl? <ubl@schaffhausen.de>
    Re: Including flock in code while developing in Windows <m.grimshaw@salford.ac.uk>
        Indirection (Camel book p. 58) <dg@sfs.nphil.uni-tuebingen.de>
    Re: Indirection (Camel book p. 58) (Ronald Blaschke)
    Re: Indirection (Camel book p. 58) (Tad McClellan)
    Re: Indirection (Camel book p. 58) <mjcarman@home.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 20 Jul 2001 10:17:32 GMT
From: scott@nospamthankx.javascript-games.org (Scott Porter)
Subject: Re: ####  ADD 6 INCHES TO YOUR PENIS #####  2830
Message-Id: <3b5804c8.1629500@news.freeserve.co.uk>

On Thu, 19 Jul 2001 14:42:53 -0400, Andras Malatinszky <andras@mortgagestats.com>
wrote:

>
>
>Craig Berry wrote:
>
>> Bernard El-Hagin (bernard.el-hagin@lido-tech.net) wrote:
>> : Who needs a module?
>> :
>> : $your_cock += 6;
>>
>> Yeah, if yours is scalar...
>>
>>   push @your_cock, ("inches") x 6;
>
>Ouch! That sounds painful!
>

:-) myCock+=(myCock*.8);


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

Date: Fri, 20 Jul 2001 08:01:47 -0400
From: Will Cottay <will@cottay.net>
Subject: Re: (long) advice needed: how robust is this script ? and other  questions :)
Message-Id: <3B581DAB.69FA8663@cottay.net>


But he's darn sure you won't ever get "blowjob" in your
mail as he checks for it three times. (Although he returns
after seeing it the first time.)

It's not Kev's mods that make it so bad BTW.  All of the "Master
Series" CGI scripts are crap.

-will


Jasper McCrea wrote:
> 
> Kev wrote:
> >
> > Hi All
> >
> > I am new to perl, and request the advice /
> > assistance of you seasoned pro's.
> >
> > Any assistance very gratefully appreciated.
> >
> 
> And I thought I wrote unmaintainable code. This is horrible.
> 
> But I did enjoy reading the 'offensive' words category. And I found it
> interesting that you wouldn't send mail including the words
> 'endorsement', 'advertisement', 'wristwatch', never mind 'suck'.
> 
> Jasper
> --
>       split//,'019617511192'.
>       '17011111610114101114'.
>       '21011141011840799901'.
>             '17101174';
>             foreach(0..
>             $#_){$_[$_
>             ++]^=$_[$_
>             --]^=$_[$_
> ]^=$_[++    $_]if!($_%
> 2)}$g.=$_  ,chr($g)=~
>  /(\w)/&&($o.=$1and
>    $g='')foreach@_;
>       print"$o\n"



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

Date: Fri, 20 Jul 2001 13:17:43 +0200
From: Robert Laufer <rlaufer1@lucent.com>
Subject: CGI and open2 problem
Message-Id: <3B581357.863FA4BA@lucent.com>

Hi,
I have a problem using CGI and IPC package together. Here is a short
example code that shows the problem. On an http server the output is
different from the command line output.
It seems, that open2 redirects some filehandles that CGI uses.
Does anyone have an idea?

Thanks in advance,
Robert.


#!/usr/bin/perl -w

use FileHandle;
use IPC::Open2;
use CGI qw/:standard/;

print header;
print start_html('simple test');

open2(*RDR, *WRTR, "/usr/bin/cat -u")
  or die "Could not pipe to cat: $!\n";
WRTR->autoflush();

print WRTR "Hello World!\n";
close WRTR;

while(defined($line=<RDR>))
{
  print $line;
}
close RDR;

print end_html;


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

Date: Fri, 20 Jul 2001 10:08:58 -0400
From: David Coppit <newspost@coppit.org>
Subject: Re: dumb question
Message-Id: <3B583B7A.9060605@coppit.org>

Godzilla! wrote:

> Another practicing internet mind reader! It is so
> amazing how you internet psychics can provide almost
> correct answers for even the most incoherent question!

No... You misunderstand. We're here to amuse ourselves, irrespective of 
the poster's (well-specified or poorly-specified) problem description. 
As you can see, we've been happily solving a more interesting (read: 
tractable) problem than the NP-complete one that the OP eventually 
described. :)

Regards,
David



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

Date: Fri, 20 Jul 2001 11:22:38 GMT
From: megapode@hotmail.com (Bob Dubery)
Subject: Extra bytes when writing file out
Message-Id: <3b581138.161511070@10.100.2.1>

Hi all,

I've been struggling with this for a while now. Maybe somebody can
shed some light for me.

I have a simple program that retrieves a file via HTTP and writes it
out to the local hard drive. It works fine for text files. There are
problems with binary files.

The code is required to run on a Win 32 box (NT4 or Win2k). I have
version 5. 00503 running on the windows boxes - the active state port.

During testing I am trying to retrieve a file from a unix box. The
file is 8200 bytes in size. My program reads in 8200 bytes, but the
file that is written away ends up 8246 bytes in size - ON WINDOWS. In
my test scenario the file is a zipped file, and will not uncompress on
the windows box - which would seem to be something to do with the
extra 46 bytes.

If I run the same program on a Unixware box (the version there is
5.00404) then the output file ends up correctly as 8200 bytes.

Any ideas.

Code follows. I have also tried reading $data in a loop that started
while($data = <$socket>){

That didn't make any difference

If I chomp $data then the file usually ends up too small - again by 46
bytes.

==============================================


use IO::Socket;
use Strict;
use Time::Local;

open(OFILE,">ak_ADCON000.Z") or die "no can do :-(";

# open a socket...
	$socket = IO::Socket::INET->new(PeerAddr => '138.216.1.1',
					PeerPort => 80,
					Proto    => 'tcp')

	or die "Couldn't open the socket!!!!";	

# form the HTTP request
	$sourcefile = '/ak_ADCON000.Z
	$request = "GET $sourcefile HTTP/1.0\n\n";

# request the file...
	print $socket $request;


# process the output and strip out the response headers
        	$readingHeaders = 1;
	$actual = 0;
	$readstuff = 0;
	      
# now read the socket and process the output
# ... first exhaust the headers...
       	while($readingHeaders){
		$line=<$socket>;
         		@stuff = split(/\015/,$line);
            		 if($stuff[0] eq ""){
            			$readingHeaders = 0;
            		}
         	}
			
	$ops = 0;
	#... now the actual data...
	do{
# try to read
		$bytesread = sysread($socket, $data, 1024);
		$ops ++;
		print OFILE $data;
		$actual += $bytesread;
		print "$ops ... $actual\n";		
# end reading if nothing was read
	} while ($bytesread > 0);
			
						
	print "done read $actual bytes\n";


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

Date: 20 Jul 2001 11:47:47 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Extra bytes when writing file out
Message-Id: <9j95p3$guq$1@mamenchi.zrz.TU-Berlin.DE>

According to Bob Dubery <megapode@hotmail.com>:
 
[...]

> During testing I am trying to retrieve a file from a unix box. The
> file is 8200 bytes in size. My program reads in 8200 bytes, but the
> file that is written away ends up 8246 bytes in size - ON WINDOWS. In
> my test scenario the file is a zipped file, and will not uncompress on
> the windows box - which would seem to be something to do with the
> extra 46 bytes.
> 
> If I run the same program on a Unixware box (the version there is
> 5.00404) then the output file ends up correctly as 8200 bytes.
> 
> Any ideas.

Yes, perldoc -f binmode.

Anno


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

Date: Fri, 20 Jul 2001 13:51:36 +0100
From: Malte Ubl <ubl@schaffhausen.de>
Subject: Re: Extra bytes when writing file out
Message-Id: <3B582957.9D674BDF@schaffhausen.de>

Bob Dubery schrieb:
> 
> Hi all,
> 
> I've been struggling with this for a while now. Maybe somebody can
> shed some light for me.


Uhmm, why dont you use LWP. The network stuff should be way way way
easier with that
 ...
Concerning your problem: you should look into binmode
specifically
open FILE...
binmode(FILE)

That should help.

Bye
->malte


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

Date: Fri, 20 Jul 2001 12:31:29 +0100
From: "Trevor Ward" <tward10@jaguar.com>
Subject: Re: Extra bytes when writing file out
Message-Id: <9j94qi$akp5@eccws12.dearborn.ford.com>

Answer think you need to use bin mode to stop the file having additional
crlf added.
unless of course somebody knows differently.

"Bob Dubery" <megapode@hotmail.com> wrote in message
news:3b581138.161511070@10.100.2.1...
> Hi all,
>
> I've been struggling with this for a while now. Maybe somebody can
> shed some light for me.
>
> I have a simple program that retrieves a file via HTTP and writes it
> out to the local hard drive. It works fine for text files. There are
> problems with binary files.
>
> The code is required to run on a Win 32 box (NT4 or Win2k). I have
> version 5. 00503 running on the windows boxes - the active state port.
>
> During testing I am trying to retrieve a file from a unix box. The
> file is 8200 bytes in size. My program reads in 8200 bytes, but the
> file that is written away ends up 8246 bytes in size - ON WINDOWS. In
> my test scenario the file is a zipped file, and will not uncompress on
> the windows box - which would seem to be something to do with the
> extra 46 bytes.
>
> If I run the same program on a Unixware box (the version there is
> 5.00404) then the output file ends up correctly as 8200 bytes.
>
> Any ideas.
>
> Code follows. I have also tried reading $data in a loop that started
> while($data = <$socket>){
>
> That didn't make any difference
>
> If I chomp $data then the file usually ends up too small - again by 46
> bytes.
>
> ==============================================
>
>
> use IO::Socket;
> use Strict;
> use Time::Local;
>
> open(OFILE,">ak_ADCON000.Z") or die "no can do :-(";
>

  binmode (OFILE);

> # open a socket...
> $socket = IO::Socket::INET->new(PeerAddr => '138.216.1.1',
> PeerPort => 80,
> Proto    => 'tcp')
>
> or die "Couldn't open the socket!!!!";
>
> # form the HTTP request
> $sourcefile = '/ak_ADCON000.Z
> $request = "GET $sourcefile HTTP/1.0\n\n";
>
> # request the file...
> print $socket $request;
>
>
> # process the output and strip out the response headers
>         $readingHeaders = 1;
> $actual = 0;
> $readstuff = 0;
>
> # now read the socket and process the output
> # ... first exhaust the headers...
>        while($readingHeaders){
> $line=<$socket>;
>          @stuff = split(/\015/,$line);
>             if($stuff[0] eq ""){
>             $readingHeaders = 0;
>             }
>          }
>
> $ops = 0;
> #... now the actual data...
> do{
> # try to read
> $bytesread = sysread($socket, $data, 1024);
> $ops ++;
> print OFILE $data;
> $actual += $bytesread;
> print "$ops ... $actual\n";
> # end reading if nothing was read
> } while ($bytesread > 0);
>
>
> print "done read $actual bytes\n";




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

Date: Fri, 20 Jul 2001 12:25:41 GMT
From: megapode@hotmail.com (Bob Dubery)
Subject: Re: Extra bytes when writing file out
Message-Id: <3b58231d.166092207@10.100.2.1>

On Fri, 20 Jul 2001 13:51:36 +0100, Malte Ubl <ubl@schaffhausen.de>
wrote:


>Concerning your problem: you should look into binmode
>specifically
>open FILE...
>binmode(FILE)

Thanks Malte, and also Anno

That did the trick :-)


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

Date: Fri, 20 Jul 2001 18:49:09 +0530
From: Jahagirdar V S <jvs@india.ti.com>
Subject: extracting string seperated multiline data...
Message-Id: <3B582FCD.ADDAC6FB@india.ti.com>

Hi,
I have a file which contains data seperated by a string which occurs on
a line by itselves now I want to move each data field in a file by
itselves I wrote a shell script to achive the same but think a better
solution exists in perl any suggestions??
for eg
if the seperator is: hello
then the file looks like
<start sample file>

ignore  data at start
 ...
the next line is the seperator we need to get everything in between the
two hellos
hello
first data entry starts
 ...
hello
second data field starts BTW thanks for reading this post
 ....
hello
3rd data field and thanks in advance for responding
hello
<end sample file>

<start shell script>
#!/usr/bin/sh
PREVIOUS=0
BOUNDARY=$1
FILE=hellofile
UNIQUE=$$
for line in `grep -n -- $BOUNDARY $FILE | cut -d : -f1`; do
 [ -r /tmp/"$UNIQUE"TMP ] &&  head -`expr $line - $PREVIOUS`
/tmp/"$UNIQUE"TMP>/tmp/$UNIQUE$PREVIOUS
less /tmp/$UNIQUE$PREVIOUS
tail +$line $FILE>/tmp/"$UNIQUE"TMP
PREVIOUS=$line
done

<end shell script>




--
Do not disturb. Already disturbed!
Jahagirdar .V.S
IC Design Engineer , Texas Instruments (India) Ltd.
Ph (91)080-5099129(O)





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

Date: Fri, 20 Jul 2001 09:39:39 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: extracting string seperated multiline data...
Message-Id: <slrn9lgd4r.26q.tadmc@tadmc26.august.net>

Jahagirdar V S <jvs@india.ti.com> wrote:

>I have a file which contains data seperated by a string which occurs on
>a line by itselves


See the $/ variable in perlvar.pod. Then use:

   $/ = "hello\n";

To read a multiline record for each <INPUT> operator.


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


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

Date: Fri, 20 Jul 2001 12:17:45 GMT
From: PerlFAQ Server <faq@denver.pm.org>
Subject: FAQ: Can I write useful Perl programs on the command line?
Message-Id: <JjV57.12$zH9.189282304@news.frii.net>

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with every Standard Distribution of
Perl.

+
  Can I write useful Perl programs on the command line?

    Yes. Read the perlrun manpage for more information. Some examples
    follow. (These assume standard Unix shell quoting rules.)

        # sum first and last fields
        perl -lane 'print $F[0] + $F[-1]' *

        # identify text files
        perl -le 'for(@ARGV) {print if -f && -T _}' *

        # remove (most) comments from C program
        perl -0777 -pe 's{/\*.*?\*/}{}gs' foo.c

        # make file a month younger than today, defeating reaper daemons
        perl -e '$X=24*60*60; utime(time(),time() + 30 * $X,@ARGV)' *

        # find first unused uid
        perl -le '$i++ while getpwuid($i); print $i'

        # display reasonable manpath
        echo $PATH | perl -nl -072 -e '
            s![^/+]*$!man!&&-d&&!$s{$_}++&&push@m,$_;END{print"@m"}'

    OK, the last one was actually an Obfuscated Perl Contest entry. :-)

- 

Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short.  They represent an important
part of the Usenet tradition.  They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.

If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile.  If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.

Answers to questions about LOTS of stuff, mostly not related to
Perl, can be found by pointing your news client to

    news:news.answers

or to the many thousands of other useful Usenet news groups.

Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release.  It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.

The perlfaq manual page contains the following copyright notice.

  AUTHOR AND COPYRIGHT

    Copyright (c) 1997-1999 Tom Christiansen and Nathan
    Torkington.  All rights reserved.

This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.

                                                           03.25
-- 
    This space intentionally left blank


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

Date: Fri, 20 Jul 2001 10:11:03 -0400
From: David Coppit <newspost@coppit.org>
Subject: Re: Generic Language interpreter in Perl?
Message-Id: <3B583BF7.2040104@coppit.org>

Bart Lateur wrote:

> XML is another candidate for file syntax.

Yeah, but it was designed for computer consumption. Plus it's probably 
no harder to understand from the user's perspective than using Perl 
syntax and doing a require.

David





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

Date: Fri, 20 Jul 2001 13:26:13 +0200
From: "Georg Vassilopulos" <Georg.Vassilopulos@SoftwareAG.de>
Subject: Re: Getting always corrupt files while uploading (cgi)
Message-Id: <9j94gl$elr$1@gamma.ecomp.net>

Thanks a lot for Your help.
It works now!!

Greetings
Georg
georg.vassilopulos@softwareAG.com


Andreas Schmitz wrote in message <3b55f5ed.12593735@news.btx.dtag.de>...
>On Wed, 18 Jul 2001 11:19:26 +0200, "Georg Vassilopulos"
><Georg.Vassilopulos@SoftwareAG.de> wrote:
>
>>use CGI qw(:standard);
>use CGI::Carp qw(fatalsToBrowser);
>$CGI::POST_MAX=1024 * 100;
>>my $file = $query->param('Datei');
>
>You can't do HTTP upload unless your from use multipart/form-data.
>Please check this.
>>open (UPFILE, ">$myFile"); || die"no such upfile,$!";
>>while (read($file, $buffer, 1024)) { print UPFILE $buffer;  }
>close (UPFILE) || die "no closing,$!";
>
>
>--
>#!C:\perl\bin\perl.exe -wT
>
>print map {chr(ord($_)-3)} split //, "Dqguhdv0Vfkplw}Cw0rqolqh1gh";
># Andreas Schmitz                     http://www.medialsoft.de
># medialsoft (perlprogramming...)     post@medialsoft.de




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

Date: Fri, 20 Jul 2001 09:07:44 -0400
From: Jason LaPenta <SPAM_lapenta_jm@yahoo.com>
Subject: GPIB / Parrallel port
Message-Id: <3B582D20.648C762B@yahoo.com>

Hello,

Has anyone used perl to communicate to GPIB or a PC parallel port under
ms windows or linux? How did you do it? Did it work well?

Thanks
Jason


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

Date: Fri, 20 Jul 2001 12:47:13 GMT
From: dma@pern.co.uk (David Anderson)
Subject: Hashes of arrays
Message-Id: <6BADEA3735E301A8.7CD782DDB20C1CAB.117C12CF97E29BB2@lp.airnews.net>

I am trying to create a hash of arrays using the following code:

open (IN, "c:\\bacodes.txt") or die "Cannot open bacodes\n";
while (<IN>) {
    chomp;
    ($code, @info) = split ("\t", $_);
    $ba{$code} = @info;
}

This puts the final value of @info into all the entries in the hash.

Is there any way to create the hash of arrays without having to set up
a loop to put each individual value in?


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

Date: Fri, 20 Jul 2001 13:56:13 +0100
From: "W K" <bill.kemp@wire2.com>
Subject: Re: Hashes of arrays
Message-Id: <iWV57.25$dm2.589@news.uk.colt.net>


David Anderson wrote in message
<6BADEA3735E301A8.7CD782DDB20C1CAB.117C12CF97E29BB2@lp.airnews.net>...
>I am trying to create a hash of arrays using the following code:
>
>open (IN, "c:\\bacodes.txt") or die "Cannot open bacodes\n";
>while (<IN>) {
>    chomp;
>    ($code, @info) = split ("\t", $_);
>    $ba{$code} = @info;
>}
>
>This puts the final value of @info into all the entries in the hash.
>
>Is there any way to create the hash of arrays without having to set up
>a loop to put each individual value in?

you probably want to create a hash of _references_ to arrays.
This would (mistakenly) look like this:
$ba{$code} = \@info;

For reasons explained better elsewhere, you need
$ba{$code}= [@info];

(and to get the array later @{$ba{key}} )






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

Date: Fri, 20 Jul 2001 14:20:10 +0100
From: David Anderson <david.anderson@pern.co.uk>
Subject: Re: Hashes of arrays
Message-Id: <C71DC5E8E911EF16.236647132A878B57.2B82E1AD903D12ED@lp.airnews.net>

Works beautifully. Thanks Bill.
One day I will get the hang of these hashes of arrays, hashes of
hashes, etc.



On Fri, 20 Jul 2001 13:56:13 +0100, "W K" <bill.kemp@wire2.com> wrote:

>
>David Anderson wrote in message
><6BADEA3735E301A8.7CD782DDB20C1CAB.117C12CF97E29BB2@lp.airnews.net>...
>>I am trying to create a hash of arrays using the following code:
>>
>>open (IN, "c:\\bacodes.txt") or die "Cannot open bacodes\n";
>>while (<IN>) {
>>    chomp;
>>    ($code, @info) = split ("\t", $_);
>>    $ba{$code} = @info;
>>}
>>
>>This puts the final value of @info into all the entries in the hash.
>>
>>Is there any way to create the hash of arrays without having to set up
>>a loop to put each individual value in?
>
>you probably want to create a hash of _references_ to arrays.
>This would (mistakenly) look like this:
>$ba{$code} = \@info;
>
>For reasons explained better elsewhere, you need
>$ba{$code}= [@info];
>
>(and to get the array later @{$ba{key}} )
>
>
>



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

Date: Fri, 20 Jul 2001 08:44:57 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Hashes of arrays
Message-Id: <slrn9lg9u9.205.tadmc@tadmc26.august.net>

David Anderson <dma@pern.co.uk> wrote:
>I am trying to create a hash of arrays using the following code:
>
>open (IN, "c:\\bacodes.txt") or die "Cannot open bacodes\n";
>while (<IN>) {
>    chomp;
>    ($code, @info) = split ("\t", $_);
                             ^^^^

A pattern should *look like* a pattern...


>    $ba{$code} = @info;
>}
>
>This puts the final value of @info into all the entries in the hash.
>
>Is there any way to create the hash of arrays without having to set up
>a loop to put each individual value in?


   my($code, @info) = split (/\t/, $_);  # must be my()d
   $ba{$code} = \@info;

or

   $ba{$code} = [@info];   # no my() required, we make a copy


see perlreftut.pod.


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


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

Date: Fri, 20 Jul 2001 09:01:50 -0500
From: Michael Carman <mjcarman@home.com>
Subject: Re: Hashes of arrays
Message-Id: <3B5839CE.95998403@home.com>

W K wrote:
> 
> David Anderson wrote:
>
>> I am trying to create a hash of arrays using the following code:
>>
>> open (IN, "c:\\bacodes.txt") or die "Cannot open bacodes\n";
>> while (<IN>) {
>>     chomp;
>>     ($code, @info) = split ("\t", $_);
>>     $ba{$code} = @info;
>> }
>>
>> This puts the final value of @info into all the entries in the
>> hash.

No, it interprets @info in scalar context and puts its *length* into the
hash.

>> Is there any way to create the hash of arrays without having to
>> set up a loop to put each individual value in?
> 
> you probably want to create a hash of _references_ to arrays.
> This would (mistakenly) look like this:
> $ba{$code} = \@info;

Not if you properly scope your variables:

while (<IN>) {
    chomp;
    my ($code, @info) = split(/\t/, $_);
    $ba{$code} = \@info;
}
 
> For reasons explained better elsewhere, you need
> $ba{$code}= [@info];

Also good, and preferred by some because they feel the former method
holds a trap for novices.

-mjc


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

Date: Fri, 20 Jul 2001 13:54:08 +0100
From: Malte Ubl <ubl@schaffhausen.de>
Subject: Re: ide for perl?
Message-Id: <3B5829F0.B0997A9A@schaffhausen.de>

Robert Sherman schrieb:
> 
> Jeff D. Hamann wrote:
> 
> komodo from www.activestate.com is a possibility...haven't tried it
> though...

VisualPerl from www.activestate.com is a possibility...haven't tried it
though...

->malte


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

Date: Fri, 20 Jul 2001 12:58:42 +0100
From: Mark Grimshaw <m.grimshaw@salford.ac.uk>
Subject: Re: Including flock in code while developing in Windows for Unix??
Message-Id: <3B581CF2.E8E7C9C9@salford.ac.uk>

Bart Lateur wrote:
> 
> Mark Grimshaw wrote:
> 
> >
> >
> >Bart Lateur wrote:
> >>
> >> Mark Grimshaw wrote:
> >>
> >> >               if(eval "use Fcntl qw(:flock)") # has flock()
> >> ...
> >> ><snip>
> >> >
> >> >on WinNT ActiveState, this prints:
> >> >no flock:
> >> >
> >> >with nothing in $@ yet I can lock a filehandle with LOCK_EX...  Any idea
> >> >what I'm doing wrong?
> >>
> >> The code you eval doesn't fail, yet it doesn't return a value. Two
> >> solutions:
> >>
> >>  - use $@ as the definitive test.
> >>
> >>                 eval "use Fcntl qw(:flock)";
> >>                 unless($@)      # has flock()
> >>
> >
> >I'm not sure I understand that.  I thought $@ would contain something if
> >the eval failed in which case, it does NOT have flock() (if I understand
> >eval correctly).
> 
> That's why I use "unless", which is the inverse of "if". So if the eval
> succeeds, "unless($@) { ... }" will execute the block.
> 
> However, your test is no good. "use Fcntl ':flock'" only imports the
> constatnts, it does NOT barf if flock isn't supported. For that, you
> need to try and execute a flock(), which will give a fatal error if
> flock() is not supported. Jeff Zucker's attempt to "flock" STDOUT is
> such a test.
> 
> >>  - add a true statement to your eval:
> >>
> >>                 if(eval "use Fcntl qw(:flock); 1")      # has flock()
> >>
> >
> >Is this simply to force the issue?
> 
> Look, if eval() fails, it will return undef. However, it can succeed and
> still return undef:
> 
>         eval "undef"
> 
> So a test if eval returns undef, or false, is not good enough.
> 
> A "use" statement is such a statement, that in itself doesn't return
> anything. So you need an extra statement that returns true. "1" will do.
> 
>         eval "undef; 1"
> 
> will return 1 (and $@ will be empty).
> 
> --
>         Bart.

Understood - thanks.


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

Date: Fri, 20 Jul 2001 14:05:46 +0200
From: Dale Gerdemann <dg@sfs.nphil.uni-tuebingen.de>
Subject: Indirection (Camel book p. 58)
Message-Id: <3B581E9A.F503C271@sfs.nphil.uni-tuebingen.de>

On p. 58 of the Camel book (3rd ed), it says:

   ... instead of saying:

      $bert

   you might say:

      ${ some expression() }

   and if the some_expression() function returns a reference to a
   variable $bert (or even the string "bert"), it will work just as if
   you'd said $bert in the first place. ...


I tried this out and discovered that it is not true:

  my $bert = "ernie";

  sub some_expression {
    return "bert";
  }

  print "${ some_expression() }\n";  # Syntax error!

According to the passage cited above (more precisely: the part that
says 'or even the string "bert"'), this should print "ernie". So
what's wrong here?

-- Dale Gerdemann





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

Date: 20 Jul 2001 13:24:43 GMT
From: TGVCDPVNTLMI@spammotel.com (Ronald Blaschke)
Subject: Re: Indirection (Camel book p. 58)
Message-Id: <9j9bep$n59er$1@ID-57488.news.dfncis.de>

There are two answers to your question.

1) What you said you do works only with real references,
thus
   sub some_expression {
     return \"bert";
   }      --^--
But in this case, "bert" will be printed.

2) What you tried to do works, as far as I know, only with 
global variables, because you are using a _symbolic_ reference.
For that you need to say
   $bert = "ernie";
, i.e. remove the "my".


Hope it helps,
Ron




On Fri, 20 Jul 2001 14:05:46 +0200, 
Dale Gerdemann <dg@sfs.nphil.uni-tuebingen.de> wrote:
> On p. 58 of the Camel book (3rd ed), it says:
> 
>    ... instead of saying:
> 
>       $bert
> 
>    you might say:
> 
>       ${ some expression() }
> 
>    and if the some_expression() function returns a reference to a
>    variable $bert (or even the string "bert"), it will work just as if
>    you'd said $bert in the first place. ...
> 
> 
> I tried this out and discovered that it is not true:
> 
>   my $bert = "ernie";
> 
>   sub some_expression {
>     return "bert";
>   }
> 
>   print "${ some_expression() }\n";  # Syntax error!
> 
> According to the passage cited above (more precisely: the part that
> says 'or even the string "bert"'), this should print "ernie". So
> what's wrong here?
> 
> -- Dale Gerdemann
> 
> 
> 


-- 


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

Date: Fri, 20 Jul 2001 08:40:18 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Indirection (Camel book p. 58)
Message-Id: <slrn9lg9li.205.tadmc@tadmc26.august.net>

Dale Gerdemann <dg@sfs.nphil.uni-tuebingen.de> wrote:
>On p. 58 of the Camel book (3rd ed), it says:
>
>   ... instead of saying:
>
>      $bert
>
>   you might say:
>
>      ${ some expression() }
>
>   and if the some_expression() function returns a reference to a
>   variable $bert (or even the string "bert"), it will work just as if
>   you'd said $bert in the first place. ...
>
>I tried this out and discovered that it is not true:
>
>  my $bert = "ernie";
>
>  sub some_expression {
>    return "bert";
>  }
>
>  print "${ some_expression() }\n";  # Syntax error!
>
>According to the passage cited above (more precisely: the part that
>says 'or even the string "bert"'), this should print "ernie". 


You should disregard the Camel's parenthetical if you have "use strict"
enabled, as it requires the use of symbolic references.


>So
>what's wrong here?


1) use strict is enabled (but that is "right" in Real Code, just
                          not in the context of that Camel passage)

2) $bert is my()d. Can't do symbolic references on lexical variables.


Remove "use strict" and the my(), and it will work as the Camel describes.

But don't do that in a real program. Symrefs are bad:

   http://www.plover.com/~mjd/perl/varvarname.html
   http://www.plover.com/~mjd/perl/varvarname2.html
   http://www.plover.com/~mjd/perl/varvarname3.html


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


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

Date: Fri, 20 Jul 2001 09:11:24 -0500
From: Michael Carman <mjcarman@home.com>
Subject: Re: Indirection (Camel book p. 58)
Message-Id: <3B583C0C.74C4C4C5@home.com>

[Jeapordectomy performed. *Please* don't top post.]

Ronald Blaschke wrote:
>
> Dale Gerdemann <dg@sfs.nphil.uni-tuebingen.de> wrote:
>
>> On p. 58 of the Camel book (3rd ed), it says:
>>
>>    ... instead of saying:
>>
>>       $bert
>>
>>    you might say:
>>
>>       ${ some expression() }
>>
>>    and if the some_expression() function returns a reference to
>>    a variable $bert (or even the string "bert"), it will work
>>    just as if you'd said $bert in the first place. ...
>>
>> I tried this out and discovered that it is not true:
>>
>>   my $bert = "ernie";
>>
>>   sub some_expression {
>>     return "bert";
>>   }
>>
>>   print "${ some_expression() }\n";  # Syntax error!

It's not a syntax error, it's either a violation of use strict "refs" or
an uninitialized error without strict.

> 1) What you said you do works only with real references,
> thus
>    sub some_expression {
>      return \"bert";
>    }      --^--
> But in this case, "bert" will be printed.

sub some_expression {
    return \$bert;
}

> 2) What you tried to do works, as far as I know, only with
> global variables, because you are using a _symbolic_ reference.

Correct.

-mjc


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

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


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