[25242] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 7487 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Dec 6 09:05:35 2004

Date: Mon, 6 Dec 2004 06:05:09 -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, 6 Dec 2004     Volume: 10 Number: 7487

Today's topics:
    Re: FAQ 5.5: How do I make a temporary file name? <bik.mido@tiscalinet.it>
        FAQ 9.0: What is socket.ph and where do I get it? <comdog@panix.com>
    Re: file handle problem (Anno Siegel)
    Re: file handle problem <someone@example.com>
    Re: file handle problem (Anno Siegel)
    Re: How to handle a HTTP::Request with gzip, deflate he <leifwessman@hotmail.com>
    Re: How to handle a HTTP::Request with gzip, deflate he <gisle@activestate.com>
    Re: How to handle a HTTP::Request with gzip, deflate he <leifwessman@hotmail.com>
    Re: Need to locate beginner/intermediate training cours <sarah_mould@hotmail.com>
    Re: Obtaining length of binary string <flavell@ph.gla.ac.uk>
        Perl some time hank with system command. (Lim kiang Leng)
    Re: Perl some time hank with system command. <bik.mido@tiscalinet.it>
    Re: Perl some time hank with system command. <kalinaubears@iinet.net.au>
    Re: Perl some time hank with system command. <mritty@gmail.com>
    Re: Solaris taking over Perl ownership <tadmc@augustmail.com>
    Re: Solaris taking over Perl ownership <tadmc@augustmail.com>
    Re: suid-perl deprecated... why? chris-usenet@roaima.co.uk
    Re: suid-perl deprecated... why? chris-usenet@roaima.co.uk
        timezone conversions <zebee@zip.com.au>
        Trouble Passing Array Containing Hash to Subroutine (Scott)
    Re: Trouble Passing Array Containing Hash to Subroutine <noreply@gunnar.cc>
    Re: Trouble Passing Array Containing Hash to Subroutine <jkeen_via_google@yahoo.com>
    Re: Trouble Passing Array Containing Hash to Subroutine <1usa@llenroc.ude.invalid>
    Re: Trouble Passing Array Containing Hash to Subroutine <noreply@gunnar.cc>
    Re: Trouble Passing Array Containing Hash to Subroutine <1usa@llenroc.ude.invalid>
    Re: Trouble Passing Array Containing Hash to Subroutine <msemtd@yahoo.co.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 06 Dec 2004 10:59:15 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: FAQ 5.5: How do I make a temporary file name?
Message-Id: <dba8r0tps8g6u47pqme6ulignb2se1qtri@4ax.com>

On Sun, 5 Dec 2004 23:03:01 +0000 (UTC), PerlFAQ Server
<comdog@panix.com> wrote:

>5.5: How do I make a temporary file name?

Could it be worth to mention

  open my $tmp, '>', undef or die $!

too?


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Mon, 6 Dec 2004 11:03:01 +0000 (UTC)
From: PerlFAQ Server <comdog@panix.com>
Subject: FAQ 9.0: What is socket.ph and where do I get it?
Message-Id: <cp1e95$h3r$1@reader1.panix.com>

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 Perl.

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

9.0: What is socket.ph and where do I get it?

    It's a perl4-style file defining values for system networking constants.
    Sometimes it is built using h2ph when Perl is installed, but other times
    it is not. Modern programs "use Socket;" instead.



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

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.

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-2002 Tom Christiansen and Nathan
    Torkington, and other contributors as noted. 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.


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

Date: 6 Dec 2004 11:08:56 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: file handle problem
Message-Id: <cp1ek8$khl$1@mamenchi.zrz.TU-Berlin.DE>

John W. Krahn <krahnj@telus.net> wrote in comp.lang.perl.misc:
> newbie wrote:
> > Thanks guys, I get the point of not putting large file in memory. I got a
> > new question. Say I have a bunch of large files, and I want to just get rid
> > of the last line of each of these large files. Is there a simpler way
> > without reading in and outputing the file?
> 
> perldoc -f truncate

 ...probably in combination with File::ReadBackwards.

Anno


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

Date: Mon, 06 Dec 2004 12:36:08 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: file handle problem
Message-Id: <YmYsd.239193$df2.216676@edtnps89>

Anno Siegel wrote:
> John W. Krahn <krahnj@telus.net> wrote in comp.lang.perl.misc:
> 
>>newbie wrote:
>>
>>>Thanks guys, I get the point of not putting large file in memory. I got a
>>>new question. Say I have a bunch of large files, and I want to just get rid
>>>of the last line of each of these large files. Is there a simpler way
>>>without reading in and outputing the file?
>>
>>perldoc -f truncate
> 
> 
> ...probably in combination with File::ReadBackwards.

The OP said that he didn't want to read in the file so presumably he already 
knows the length of the last line?


John
-- 
use Perl;
program
fulfillment


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

Date: 6 Dec 2004 13:41:29 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: file handle problem
Message-Id: <cp1ni9$rni$1@mamenchi.zrz.TU-Berlin.DE>

John W. Krahn <krahnj@telus.net> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> > John W. Krahn <krahnj@telus.net> wrote in comp.lang.perl.misc:
> > 
> >>newbie wrote:
> >>
> >>>Thanks guys, I get the point of not putting large file in memory. I got a
> >>>new question. Say I have a bunch of large files, and I want to just get rid
> >>>of the last line of each of these large files. Is there a simpler way
> >>>without reading in and outputing the file?
> >>
> >>perldoc -f truncate
> > 
> > 
> > ...probably in combination with File::ReadBackwards.
> 
> The OP said that he didn't want to read in the file so presumably he already 
> knows the length of the last line?

What are you doing?  Applying logic to Usenet postings? :)

Anno


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

Date: 6 Dec 2004 02:33:21 -0800
From: "Leif Wessman" <leifwessman@hotmail.com>
Subject: Re: How to handle a HTTP::Request with gzip, deflate headers
Message-Id: <1102329201.631888.148430@f14g2000cwb.googlegroups.com>


Gisle Aas wrote:
> Gisle Aas <gisle@activestate.com> writes:
>
> > "Leif Wessman" <leifwessman@hotmail.com> writes:
> >
> > > If I send the following to in my request
> > >
> > > $req->header('Accept-encoding', 'gzip, deflate');
> > >
> > > And then the Content-Encoding header in the response is 'gzip' or
> > > 'deflate'. How can I uncompress the content?
> >
> > In libwww-perl-5.802 the response object have a decoded_content()
> > method that will undo any Content-Encoding for you.
>
> Also note that you can set up:
>
>   $ua->default_header("Accept-Encoding" => "gzip, deflate");
>
> and then all requests you send out will automatically get this
header.
> No need to do it yourself for each request.
>
> -- 
> Gisle Aas


Thanks. But how do I handle the HTTP Response?

Leif



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

Date: 06 Dec 2004 12:14:09 +0100
From: Gisle Aas <gisle@activestate.com>
Subject: Re: How to handle a HTTP::Request with gzip, deflate headers
Message-Id: <871xe3he66.fsf@ask.g.aas.no>

"Leif Wessman" <leifwessman@hotmail.com> writes:

> Gisle Aas wrote:
> > Gisle Aas <gisle@activestate.com> writes:
> >
> > > "Leif Wessman" <leifwessman@hotmail.com> writes:
> > >
> > > > If I send the following to in my request
> > > >
> > > > $req->header('Accept-encoding', 'gzip, deflate');
> > > >
> > > > And then the Content-Encoding header in the response is 'gzip' or
> > > > 'deflate'. How can I uncompress the content?
> > >
> > > In libwww-perl-5.802 the response object have a decoded_content()
> > > method that will undo any Content-Encoding for you.
> >
> > Also note that you can set up:
> >
> >   $ua->default_header("Accept-Encoding" => "gzip, deflate");
> >
> > and then all requests you send out will automatically get this header.
> > No need to do it yourself for each request.
>
> Thanks. But how do I handle the HTTP Response?

Just like normal, but use $res->decoded_content instead of $res->content.

-- 
Gisle Aas


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

Date: 6 Dec 2004 04:28:04 -0800
From: "Leif Wessman" <leifwessman@hotmail.com>
Subject: Re: How to handle a HTTP::Request with gzip, deflate headers
Message-Id: <1102336083.995508.294930@f14g2000cwb.googlegroups.com>


Gisle Aas wrote:
> "Leif Wessman" <leifwessman@hotmail.com> writes:
>
> > Gisle Aas wrote:
> > > Gisle Aas <gisle@activestate.com> writes:
> > >
> > > > "Leif Wessman" <leifwessman@hotmail.com> writes:
> > > >
> > > > > If I send the following to in my request
> > > > >
> > > > > $req->header('Accept-encoding', 'gzip, deflate');
> > > > >
> > > > > And then the Content-Encoding header in the response is
'gzip' or
> > > > > 'deflate'. How can I uncompress the content?
> > > >
> > > > In libwww-perl-5.802 the response object have a
decoded_content()
> > > > method that will undo any Content-Encoding for you.
> > >
> > > Also note that you can set up:
> > >
> > >   $ua->default_header("Accept-Encoding" => "gzip, deflate");
> > >
> > > and then all requests you send out will automatically get this
header.
> > > No need to do it yourself for each request.
> >
> > Thanks. But how do I handle the HTTP Response?
>
> Just like normal, but use $res->decoded_content instead of
$res->content.
>
> --
> Gisle Aas


I get the following error:
Can't locate object method "decoded_content" via package
"HTTP::Headers" at (eval 15) line 1.
I'm using LWP::Parallel::UserAgent. Does that matter?

Leif



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

Date: 6 Dec 2004 00:44:32 -0800
From: "SarahEmmm" <sarah_mould@hotmail.com>
Subject: Re: Need to locate beginner/intermediate training course in the UK
Message-Id: <1102322672.921395.145510@c13g2000cwb.googlegroups.com>

Tad - yes, that was it! Sadly, I don't think the boss will spring for a
course outside the UK, so I can't suggest Stonehenge ;) The UK list
contains a number of companies who sound reasonable: I would be most
grateful for feedback on
http://www.wellho.co.uk/
http://www.netspinner.co.uk/Training.html
http://www.mag-sol.com/
http://www.perlcourses.co.uk/
if you have any knowledge of them.

Many thanks!
---
SarahEmmm



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

Date: Sun, 5 Dec 2004 23:10:11 +0000
From: "Alan J. Flavell" <flavell@ph.gla.ac.uk>
Subject: Re: Obtaining length of binary string
Message-Id: <Pine.LNX.4.61.0412052304490.13973@ppepc56.ph.gla.ac.uk>

On Sat, 4 Dec 2004, Jürgen Exner wrote:

> However it misses the point. Joe wrote:
> >>If you're not using Unicode, the number of bytes is the same as
> >>the number of characters.

As I read it, Joe is using the term "characters" from Perl's point of 
view.

> And this is the statement I still don't agree with.

Well, if "characters" are defined externally to Perl, and Perl is only 
given the binary bytes and not told how to interpret them, we can 
hardly expect Perl to count characters for us.  Seems reasonable to 
me.

I think we're all saying the same thing - just in different terms.


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

Date: 6 Dec 2004 00:53:24 -0800
From: kiangleng@hotmail.com (Lim kiang Leng)
Subject: Perl some time hank with system command.
Message-Id: <4282e185.0412060053.619d6841@posting.google.com>

I have a problem by using the perl command. Some time it hank with the
system command. such as below.

system ("command") or `command` . 


The command that make me hank was ,
`hostname` 
system("clear")
system ("mv a b")

can any one tell me, what is the possible reason that  cause it hank ?


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

Date: Mon, 06 Dec 2004 10:59:16 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Perl some time hank with system command.
Message-Id: <rma8r0p2h96c91k5vkk4u426l6bea4jrp0@4ax.com>

On 6 Dec 2004 00:53:24 -0800, kiangleng@hotmail.com (Lim kiang Leng)
wrote:

>I have a problem by using the perl command. Some time it hank with the
>system command. such as below.

Define "to hank". This is not a joke: I'm not a native English speaker
and I couldn't find it in my dictionary. May well be slang, but I'm
not familiar with it.

More generally, the description of your problem is too vague: give
more (but reasonably not too many) details.

Have you considered reading the posting guidelines?

>The command that make me hank was ,
>`hostname` 

As a general rule, do not *ab*use system() and/or qx//. Nothing wrong
IMHO with this one, but maybe you could be interested in Sys::Hostname
anyway...


HTH,
Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Mon, 06 Dec 2004 09:18:05 +0000
From: Sisyphus <kalinaubears@iinet.net.au>
Subject: Re: Perl some time hank with system command.
Message-Id: <41b4333d$0$25780$5a62ac22@per-qv1-newsreader-01.iinet.net.au>

Michele Dondi wrote:

> 
> Define "to hank". 

I think the OP means "hang" - where the process just sits there doing 
nothing, and fails to terminate.

Cheers,
Rob

-- 
To reply by email u have to take out the u in kalinaubears.



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

Date: Mon, 06 Dec 2004 13:17:14 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Perl some time hank with system command.
Message-Id: <uZYsd.1622$Wy.1546@trndny06>

"Michele Dondi" <bik.mido@tiscalinet.it> wrote in message
news:rma8r0p2h96c91k5vkk4u426l6bea4jrp0@4ax.com...
> On 6 Dec 2004 00:53:24 -0800, kiangleng@hotmail.com (Lim kiang Leng)
> wrote:
>
> >I have a problem by using the perl command. Some time it hank with
the
> >system command. such as below.
>
> Define "to hank". This is not a joke: I'm not a native English speaker
> and I couldn't find it in my dictionary. May well be slang, but I'm
> not familiar with it.

As a native (U.S.) English speaker, I can assure you that 'hank' is
neither proper nor any slang that I've ever heard.

Paul Lalli



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

Date: Mon, 6 Dec 2004 06:52:41 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Solaris taking over Perl ownership
Message-Id: <slrncr8lgp.2s3.tadmc@magna.augustmail.com>

Andrew Hamm <ahamm@mail.com> wrote:
> krakle wrote:


>> perl? You mean Perl.

> but what the fuck? 


This should explain this particular instance:

   http://www.catb.org/~esr/jargon/html/T/troll.html


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


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

Date: Mon, 6 Dec 2004 06:53:18 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Solaris taking over Perl ownership
Message-Id: <slrncr8lhu.2s3.tadmc@magna.augustmail.com>

Andrew Hamm <ahamm@mail.com> wrote:

> Why is this ng so full
> of pedantic fools? 


We like you too.


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


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

Date: Mon, 6 Dec 2004 09:05:28 +0000
From: chris-usenet@roaima.co.uk
Subject: Re: suid-perl deprecated... why?
Message-Id: <ovjd82-p2b.ln1@moldev.cmagroup.co.uk>

Brian McCauley <nobull@mail.com> wrote:
> suid scripts are the subject of religious wars.

Yes, but mostly it seems to be around the issue of whether the writer
really knows what they're doing when they (think they) want a suid script.

> There are two ways to do suid scripts.  Both are to some extent a kluge.
> One is a user-space kluge the other a kernel-space kludge.  I'm of the
> school of thought that says a the user-space kludge is more elegant.

Yes, true... but as far as user space goes I see two kinds of solutions:

    *	the suidperl "hidden" kind of kludge (where the perl interpreter
	"knows" what to do)

    *	the "I've got to write another wrapper, sigh" kind of kludge
	(where it's possible that someone somewhere will introduce too
	much complexity and therefore a security hole).

Thanks,
Chris


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

Date: Mon, 6 Dec 2004 09:09:26 +0000
From: chris-usenet@roaima.co.uk
Subject: Re: suid-perl deprecated... why?
Message-Id: <67kd82-p2b.ln1@moldev.cmagroup.co.uk>

Darin McBride <dmcbride@naboo.to.org.no.spam.for.me> wrote:
> unless ($< == $apache->uid())
> {
>     if ($ARGV[0])
>     {
>         print "Gotta run this as the apache user, $apache_user\n";
>         exit 1;
>     }
>     exec(qw(sudo -u), $apache_user, $0, qw(1));
> }

Why've you got the "if" round the error message? I would have thought
that it should only constrain the "exec...sudo" clause?


> And then, in /etc/sudoers, I have:
> ALL ALL=(wwwrun) NOPASSWD: /path/to/my/script


I quite like this kind of solution. It pushes the configuration back to
a single contained point, and leaves (most of) the security issues to
be dealt with by sudo.

Thanks,
Chris


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

Date: Mon, 06 Dec 2004 00:38:12 GMT
From: Zebee Johnstone <zebee@zip.com.au>
Subject: timezone conversions
Message-Id: <slrncr7aa3.b1e.zebee@zeus.zipworld.com.au>

I have machines in 3 different timezones.

I have a monitoring program that sends me email, and I use GMT in the
subject line and in the log recordings so it's easy to spot when 
things are happening at the same physical time without having to 
convert time differences in my head. 

However, I'd like to add a note of current time in my home timezone in
Australia to the emails.

Date::Manip can do it, but would have to be told whether or not to use
daylight saving time for my home timezone.

Is there a perl way to tell Date::Manip whether it's daylight saving time
in Sydney or not so it knows whether to use AEST or AEDT?  Or a shell
command on linux to parse?


Zebee


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

Date: 5 Dec 2004 15:20:52 -0800
From: slhecht@attglobal.net (Scott)
Subject: Trouble Passing Array Containing Hash to Subroutine
Message-Id: <a018a20c.0412051520.7a1ab3bc@posting.google.com>

All:

I am using Perl 5.8.0 on Red Hat Linux release 9 (Shrike).  
I am attempting to pass a reference to an array whose elements are a
hash.  When I go to generalize my code and pass a reference to this
array to a subroutine, the results I get back are unexpected.

Here is my annotated test program:

#!/usr/bin/perl -w

#Create empty array arySTUFF.
my @arySTUFF=();

#Create hash to map 0,1,2 to data element names 
# corresponding to database column names.
my %hSTUFF_VARLIST=(0=>'DATE_AUTH',
                    1=>'FILENAME',
                    2=>'CLAIMS');

#Push an anonymous hash into the array arySTUFF 
# making it the first (zeroth) element of the array.  
# Note that I am initializing each of the hash elements 
# in case there is no data coming in from my data source 
# I will have some value.
push(@arySTUFF,{DATE_AUTH => 'XXXXXXXX', 
                FILENAME  => 'No FileName', 
                CLAIMS    => -1}); 

#Initialize the three hash elements for this first array element 
# and then print out the results to see if it worked.
$$arySTUFF[0]->{$hSTUFF_VARLIST{0}}="20041116";
$$arySTUFF[0]->{$hSTUFF_VARLIST{1}}="BIGDATAFILE.TXT";
$$arySTUFF[0]->{$hSTUFF_VARLIST{2}}=12345;
print "DATE_AUTH=",$$arySTUFF[0]{$hSTUFF_VARLIST{0}},"\n";
print "FILENAME=",$$arySTUFF[0]{$hSTUFF_VARLIST{1}},"\n";
print "CLAIMS=",$$arySTUFF[0]{$hSTUFF_VARLIST{2}},"\n";

#The next line shows that $$arySTUFF[0] is a HASH reference 
# at some memory location.
print "ARYSTUFF[0]=",$$arySTUFF[0],"\n";
#The next line shows the actual value in the hash reference 
# for FILENAME.  This works.
print "ARYSTUFF[0]=>",$$arySTUFF[0]->{FILENAME},"\n";

#Pass a reference to the array arySTUFF and hash hSTUFF_VARLIST.  
# Note that I have no problem with hash ref at all, just the array
ref.
print "===TRYSUB===\n";
TrySub(\@arySTUFF,\%hSTUFF_VARLIST);

#Use a subroutine to generalize the arrays and the hashes.
sub TrySub {
 my @aryData = @{$_[0]};
 my $hData = $_[1];
 print "Num Rows==>",$#aryData+1,"\n";
 #The following line show a difference HASH memory location 
 # than the one above.
 print "ARYDATA[0]=",$aryData[0],"\n";
 #The following prints out the initialization value "No Filename" 
 # rather than the expected BIGDATAFILE.TXT.
 print "ARYDATA[0]=>",$aryData[0]->{FILENAME},"\n";

 #Just in case, let's check if arySTUFF is messed up...it isn't.
 print "\nCheck: Original arySTUFF is okay\n";
 print "ARYSTUFF[0]=",$$arySTUFF[0],"\n";
 print "ARYSTUFF[0]=>",$$arySTUFF[0]->{FILENAME},"\n";
 
}

exit;


Here are the results of this test program:

DATE_AUTH=20041116
FILENAME=BIGDATAFILE.TXT
CLAIMS=12345
ARYSTUFF[0]=HASH(0x80660b0)
ARYSTUFF[0]=>BIGDATAFILE.TXT
===TRYSUB===
Num Rows==>1
ARYDATA[0]=HASH(0x804ca88)
ARYDATA[0]=>No FileName

Check: Original arySTUFF is okay
ARYSTUFF[0]=HASH(0x80660b0)
ARYSTUFF[0]=>BIGDATAFILE.TXT


When the subroutine prints out "No Filename", I am confused as to why
this happens.  I am expecting to see BIGDATAFILE.TXT.  Any help with
this would be very greatly appreciated. (I'd hate to hard code my
program when a nice subroutine would be better!)

Thanks!
Scott


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

Date: Mon, 06 Dec 2004 01:15:12 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Trouble Passing Array Containing Hash to Subroutine
Message-Id: <31hmo3F3bmqubU1@individual.net>

Scott wrote:
> I am attempting to pass a reference to an array whose elements are a
> hash.  When I go to generalize my code and pass a reference to this
> array to a subroutine, the results I get back are unexpected.
> 
> Here is my annotated test program:
> 
> #!/usr/bin/perl -w

Why not

     use strict;

?? If you had had strictures enabled, you would have been able to figure
out what mistake you made.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Mon, 06 Dec 2004 00:27:07 GMT
From: Jim Keenan <jkeen_via_google@yahoo.com>
Subject: Re: Trouble Passing Array Containing Hash to Subroutine
Message-Id: <vHNsd.42$sp.26@trndny08>

Scott wrote:

> All:
> 
> I am using Perl 5.8.0 on Red Hat Linux release 9 (Shrike).  
> I am attempting to pass a reference to an array whose elements are a
> hash.  When I go to generalize my code and pass a reference to this
> array to a subroutine, the results I get back are unexpected.
> 
> Here is my annotated test program:
> 
> #!/usr/bin/perl -w

Why haven't you used this?

     use strict;

> 
> #Create empty array arySTUFF.
> my @arySTUFF=();

Your choice of variable names creates unnecessary line noise.  Why is 
everything named some variant of 'STUFF'?
> 
> #Create hash to map 0,1,2 to data element names 
> # corresponding to database column names.
> my %hSTUFF_VARLIST=(0=>'DATE_AUTH',
>                     1=>'FILENAME',
>                     2=>'CLAIMS');

If you're using non-negative integers for the keys of a hash, you might 
as well use an array instead, since arrays are automatically indexed by 
non-negative integers.

I copied-and-pasted your code, ran it (it did compile), and got no 
output whatsoever.  So I'm not able to directly comment on your problem. 
  However, I *strongly* recommend that before proceeding further you get 
familiar with the Data::Dumper module, as it is the most widely used way 
to dump and examine the contents of variables, particularly multi-level 
arrays and hashes such as you are using.

     % perldoc Data::Dumper

Jim Keenan


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

Date: 6 Dec 2004 00:33:40 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Trouble Passing Array Containing Hash to Subroutine
Message-Id: <Xns95B6C6FC57938asu1cornelledu@132.236.56.8>

slhecht@attglobal.net (Scott) wrote in 
news:a018a20c.0412051520.7a1ab3bc@posting.google.com:

> I am attempting to pass a reference to an array whose 
> elements are a hash.  

I have no idea what this sentence means.

> When I go to generalize my code and pass a reference to this
> array to a subroutine, the results I get back are unexpected.

Ditto.
 
> Here is my annotated test program:
> 
> #!/usr/bin/perl -w

use warnings;

Incidentally, you really ought to make sure you fix everything perl warns 
you about before posting here. Have you read the posting guidelines for 
this group?

D:\Home>perl t7.pl
Warning: Use of "ref" without parentheses is ambiguous at t7.pl line 39.
Useless use of concatenation (.) or string in void context at t7.pl line 
40.

And, why do you not have

use strict;

D:\Home>perl t7.pl
Warning: Use of "ref" without parentheses is ambiguous at t7.pl line 42.
Global symbol "$arySTUFF" requires explicit package name at t7.pl line 26.
Global symbol "$arySTUFF" requires explicit package name at t7.pl line 27.
Global symbol "$arySTUFF" requires explicit package name at t7.pl line 28.
Global symbol "$arySTUFF" requires explicit package name at t7.pl line 29.
Global symbol "$arySTUFF" requires explicit package name at t7.pl line 30.
Global symbol "$arySTUFF" requires explicit package name at t7.pl line 31.
Global symbol "$arySTUFF" requires explicit package name at t7.pl line 35.
Global symbol "$arySTUFF" requires explicit package name at t7.pl line 38.
Global symbol "$arySTUFF" requires explicit package name at t7.pl line 60.
Global symbol "$arySTUFF" requires explicit package name at t7.pl line 61.
Execution of t7.pl aborted due to compilation errors.

What can I say? Isn't that obvious enough?

> #Create empty array arySTUFF.

Useless comments are worse than no comments.

> my @arySTUFF=();

Declare your variables in the smallest applicable scope.
 
> #Create hash to map 0,1,2 to data element names 
> # corresponding to database column names.

Why?

> my %hSTUFF_VARLIST=(0=>'DATE_AUTH',
>                     1=>'FILENAME',
>                     2=>'CLAIMS');

Choosing the correct data structure for your problem is essential. In this 
case, why use a hash when you are indexing using nonnegative integers?

> #Push an anonymous hash into the array arySTUFF 
> # making it the first (zeroth) element of the array.  

This amount of noise is really uncalled for and coupled with the really bad 
variable names makes your code very hard to read. In case you haven't 
noticed, we know that @arySTUFF is an array not because of the prefix 'ary' 
but because of the @ sign in front of it. Variable names should be 
descriptive of the information variables hold.

> # Note that I am initializing each of the hash elements 
> # in case there is no data coming in from my data source 
> # I will have some value.

Sigh! What data source?

> push(@arySTUFF,{DATE_AUTH => 'XXXXXXXX', 
>                 FILENAME  => 'No FileName', 
>                 CLAIMS    => -1}); 
> 
> #Initialize the three hash elements for this first array element 
> # and then print out the results to see if it worked.
> $$arySTUFF[0]->{$hSTUFF_VARLIST{0}}="20041116";
> $$arySTUFF[0]->{$hSTUFF_VARLIST{1}}="BIGDATAFILE.TXT";
> $$arySTUFF[0]->{$hSTUFF_VARLIST{2}}=12345;

Why $$?

> print "DATE_AUTH=",$$arySTUFF[0]{$hSTUFF_VARLIST{0}},"\n";
> print "FILENAME=",$$arySTUFF[0]{$hSTUFF_VARLIST{1}},"\n";
> print "CLAIMS=",$$arySTUFF[0]{$hSTUFF_VARLIST{2}},"\n";

This makes me think, what's up with %hSTUFF_VARLIST?

Also, please use

Data::Dumper

for this purpose.
 
> #The next line shows that $$arySTUFF[0] is a HASH reference 
> # at some memory location.
> print "ARYSTUFF[0]=",$$arySTUFF[0],"\n";
> #The next line shows the actual value in the hash reference 
> # for FILENAME.  This works.
> print "ARYSTUFF[0]=>",$$arySTUFF[0]->{FILENAME},"\n";
> 
> #Pass a reference to the array arySTUFF and hash hSTUFF_VARLIST.  
> # Note that I have no problem with hash ref at all, just the array
> ref.
> print "===TRYSUB===\n";
> TrySub(\@arySTUFF,\%hSTUFF_VARLIST);
> 
> #Use a subroutine to generalize the arrays and the hashes.
> sub TrySub {
>  my @aryData = @{$_[0]};
>  my $hData = $_[1];
>  print "Num Rows==>",$#aryData+1,"\n";

There is no need for $#aryData+1. Impose scalar context on @aryData to get 
the number of elements in @aryData.

>  #The following line show a difference HASH memory location 
>  # than the one above.
>  print "ARYDATA[0]=",$aryData[0],"\n";
>  #The following prints out the initialization value "No Filename" 
>  # rather than the expected BIGDATAFILE.TXT.
>  print "ARYDATA[0]=>",$aryData[0]->{FILENAME},"\n";
> 
>  #Just in case, let's check if arySTUFF is messed up...it isn't.
>  print "\nCheck: Original arySTUFF is okay\n";
>  print "ARYSTUFF[0]=",$$arySTUFF[0],"\n";
>  print "ARYSTUFF[0]=>",$$arySTUFF[0]->{FILENAME},"\n";
>  
> }

My head hurts!

> When the subroutine prints out "No Filename", I am confused as to why
> this happens.  I am expecting to see BIGDATAFILE.TXT.  Any help with
> this would be very greatly appreciated. (I'd hate to hard code my
> program when a nice subroutine would be better!)

Please read the posting guidelines for this group. This document is posted 
here regularly or you can find it on the WWW at 
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.text

I have a very hard time understanding why you are doing what you are doing.

#!/usr/bin/perl

use strict;
use warnings;


my @columns = qw(DATE_ATUH FILENAME CLAIMS);

my @claims;

# I think the line below would be a maintainence nightmare

push @claims, {
    $columns[0] => '20041116',
    $columns[1] => 'BIGDATAFILE.TXT',
    $columns[2] => 12345,
};

print TrySub(claims => \@claims, columns => \@columns);

sub TrySub {
    my %args = @_;
    for my $claim (@{ $args{claims} }) {
        for my $column (@{ $args{columns} }) {
            print "$column = $claim->{$column}\n";
        }
    }
}


Sinan.


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

Date: Mon, 06 Dec 2004 01:36:11 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Trouble Passing Array Containing Hash to Subroutine
Message-Id: <31hnvoF3cikctU1@individual.net>

Jim Keenan wrote:
> I copied-and-pasted your code, ran it (it did compile), and got no 
> output whatsoever.

Funny. It sure doesn't compile for me with strictures enabled. This is 
the first line where Perl complains:

     $$arySTUFF[0]->{$hSTUFF_VARLIST{0}}="20041116";

The first '$' character should not be there, since the declared variable 
is the array @arySTUFF.

Etc., etc.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: 6 Dec 2004 00:38:23 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Trouble Passing Array Containing Hash to Subroutine
Message-Id: <Xns95B6C7C8D39E6asu1cornelledu@132.236.56.8>

"A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote in 
news:Xns95B6C6FC57938asu1cornelledu@132.236.56.8:

> D:\Home>perl t7.pl
> Warning: Use of "ref" without parentheses is ambiguous at t7.pl line 39.
> Useless use of concatenation (.) or string in void context at t7.pl line 
> 40.

The warnings above are due to comments wrapping in my editor.

Sinan 



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

Date: Mon, 06 Dec 2004 13:03:37 +0000
From: Michael Erskine <msemtd@yahoo.co.uk>
Subject: Re: Trouble Passing Array Containing Hash to Subroutine
Message-Id: <1102338397.76870.0@iris.uk.clara.net>

Jim Keenan wrote:

> If you're using non-negative integers for the keys of a hash, you might
> as well use an array instead, since arrays are automatically indexed by
> non-negative integers.

Not if your non-negative integer keys are non-contiguous -- you fall foul of
deriving too much specific information from this limited context in your
tirade of poor-programming accusations.

Love,
Bunky


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

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.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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


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