[13737] in Perl-Users-Digest
Perl-Users Digest, Issue: 1147 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 22 00:05:29 1999
Date: Thu, 21 Oct 1999 21:05:08 -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: <940565108-v9-i1147@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Thu, 21 Oct 1999 Volume: 9 Number: 1147
Today's topics:
Re: define global variables <ltl@rgsun40.viasystems.com>
Re: define global variables (Eric Bohlman)
Having very strange results using the if statement [ ca <snapperhead_420NOsnSPAM@hotmail.com.invalid>
Re: Having very strange results using the if statement <ltl@rgsun40.viasystems.com>
Re: Having very strange results using the if statement (Sam Holden)
Re: How do you split a string into fixed sized pieces? (Neko)
Re: in need of example... (Brett W. McCoy)
Re: in need of example... <ltl@rgsun40.viasystems.com>
Re: Mastering Algorithms With Perl-- Reviews (Sergej Zoubok)
Re: mv file routine in perl for NT & Unix? (Brett W. McCoy)
Re: NEED URGENT HELP <kperrier@blkbox.com>
Re: Obtaining a users login username (David Efflandt)
Perl and NT IIS4 <tomd@istar.ca>
Re: Persistent objects (Eric Bohlman)
Re: Problem enabling Cookies when using LWP::UserAgent <famouswizardNOfaSPAM@yahoo.com.invalid>
Re: Reference challenge <rick.delaney@home.com>
Re: Reference challenge <david@gigawatt.com>
Re: Reference challenge <david@gigawatt.com>
Re: Reference challenge <rick.delaney@home.com>
Re: Reference challenge <jeffp@crusoe.net>
Re: Reference challenge <jeffp@crusoe.net>
Re: Reference challenge <rick.delaney@home.com>
sorting hashes in an array of hashes <daniella@cs.umass.edu>
Re: sorting hashes in an array of hashes <rick.delaney@home.com>
Re: sorting hashes in an array of hashes (Martien Verbruggen)
Re: subroutine (Peter J. Kernan)
Re: Unlink (Sam Holden)
Re: Using Modules (Eric Bohlman)
Re: Win32: Communicate w/ printer <dchristensen@california.com>
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 22 Oct 1999 01:17:54 GMT
From: lt lindley <ltl@rgsun40.viasystems.com>
Subject: Re: define global variables
Message-Id: <7uoe02$298$1@rguxd.viasystems.com>
Tim & Barbara Aldrich <aldricht@onslowonline.net> wrote:
:>I am aware of my and local for defining local variables. If I don't use
:>either of these ... does that make it a global variable. Specifically if I
:>don't use either my or local inside a sub does that make a global variable
:>e.g..
Yes. You are correct. More specifically, it adds an entry to the
symbol table for the current package (in this case the package is
probably main:: ) for that variable name and datatype.
See `perldoc perldata` for more than you ever wanted to know
about this subject (as much because it references almost
every other document in the distribution as because it is chock
full of information).
But you will be happier if you just don't do that unless/until you
know exactly why you are doing it. That is why you see the
frequent advice in this newsgroup to invoke perl with the
"-w" switch and to put
use strict;
at the top of your programs. c.l.p.m regulars are almost rabid
in their insistence that you avoid package global variables
except where they are Perl builtins or File Handles (and in
those cases the hyrdophobic c.l.p.m'ers want you to use "local"
to isolate the potential side affects).
Perl may have been designed to allow you to do things the way you
want to do them and to do them easily without worrying about those
details, but in the long run the discipline imposed by "use strict"
will help you avoid problems.
Nowadays even in my quick and dirty programs I am in the habit of
following the "use strict" discipline. It pays off in the long
run.
--
// Lee.Lindley /// I used to think that being right was everything.
// @bigfoot.com /// Then I matured into the realization that getting
//////////////////// along was more important. Except on usenet.
------------------------------
Date: 22 Oct 1999 01:42:25 GMT
From: ebohlman@netcom.com (Eric Bohlman)
Subject: Re: define global variables
Message-Id: <7uofe1$jcn$4@nntp1.atl.mindspring.net>
Tim & Barbara Aldrich (aldricht@onslowonline.net) wrote:
: I am aware of my and local for defining local variables. If I don't use
: either of these ... does that make it a global variable. Specifically if I
Unless you declare it with my, it will be a global variable. local's
name is quite misleading; it works on *global* variables, saving the
current value for the duration of the block.
------------------------------
Date: Thu, 21 Oct 1999 19:15:18 +1700
From: John Iyezack <snapperhead_420NOsnSPAM@hotmail.com.invalid>
Subject: Having very strange results using the if statement [ can anyone help ? ]
Message-Id: <004aa0e3.c0bb3799@usw-ex0101-001.remarq.com>
I am having very strange results when using the if statement.
Heres the code:
$count = '0';
until ( $count eq '6' ) {
if (( $www_time_off_1[0]->[$count] ne '0' ) || (
$www_time_on_1[0]->[$count] ne '0' )) {
$db_total_hours[$count] = $www_time_off_1[0]->[$count] -
$www_time_on_1[0]->[$count];
} else {
$db_total_hours[$count] = '0';
$www_time_on_1[0]->[$count] = '---';
$www_time_off_1[0]->[$count] = '---';
}
$count++;
}
This really dosn't show why I am using it this way, this is only a very
small part of the program. But, this is were my problem is.
Ok, in theroy, this is saying that if ether condition is false, it will
preform the next set of code. But.... its not. I can not get it to go
into the if statement by using the ||. Does the problem exsist between
the keyboard and chair ???
Here is another one of my nightmares.
if ( $FORM{'on_hour_thurs_1'} >= '13' ) {
$FORM{'on_hour_thurs_1'} = $FORM{'on_hour_thurs_1'} - 12;
}
if ( $FORM{'off_hour_thurs_1'} ge '13' ) {
$FORM{'off_hour_thurs_1'} = $FORM{'off_hour_thurs_1'} - 12;
[ BTW: yes, the code is very strange, you'd have to see the rest of the
program to understand ]
The problem is, this works, why doesn't this ?!?
if ( $FORM{'on_hour_thurs_1'} lt '13' ) {
$FORM{'on_hour_thurs_1'} = $FORM{'on_hour_thurs_1'} - 12;
}
I have to use the >= or else it won't work what so ever!!
Can anyone explain why this is refusing to co-operate ?
* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!
------------------------------
Date: 22 Oct 1999 03:07:36 GMT
From: lt lindley <ltl@rgsun40.viasystems.com>
Subject: Re: Having very strange results using the if statement [ can anyone help ? ]
Message-Id: <7uokdo$3l6$2@rguxd.viasystems.com>
John Iyezack <snapperhead_420NOsnSPAM@hotmail.com.invalid> wrote:
[snip]
I'm too sleepy to work my way through your convoluted program, but
did you try running it through the built-in perl debugger?
perl -d your_script_name
h # Gives you some tips about how to use the debugger
b line_number_somewhere_before_where_you_think_problem_appears
c #continue program until the breakpoint set in line above
x $some_variable_to_inspect # examine the contents of a variable
print "true" if (some_logic_you_want_to_test)
The Perl debugger is really cool. `perldoc perldebug` but
don't get too caught up in the details. Just try it.
--
// Lee.Lindley /// I used to think that being right was everything.
// @bigfoot.com /// Then I matured into the realization that getting
//////////////////// along was more important. Except on usenet.
------------------------------
Date: 22 Oct 1999 03:30:57 GMT
From: sholden@pgrad.cs.usyd.edu.au (Sam Holden)
Subject: Re: Having very strange results using the if statement [ can anyone help ? ]
Message-Id: <slrn80vmjg.4bb.sholden@pgrad.cs.usyd.edu.au>
On Thu, 21 Oct 1999 19:15:18 +1700, John Iyezack
<snapperhead_420NOsnSPAM@hotmail.com.invalid> wrote:
>I am having very strange results when using the if statement.
>
>Heres the code:
>
>$count = '0';
why not = 0 ?
>until ( $count eq '6' ) {
why not == 6 ?
> if (( $www_time_off_1[0]->[$count] ne '0' ) || (
Why not != 0 ?
>$www_time_on_1[0]->[$count] ne '0' )) {
Why not != 0 ?
> $db_total_hours[$count] = $www_time_off_1[0]->[$count] -
>$www_time_on_1[0]->[$count];
> } else {
> $db_total_hours[$count] = '0';
Why not = 0 ?
> $www_time_on_1[0]->[$count] = '---';
> $www_time_off_1[0]->[$count] = '---';
Oh that's why ne '0'. You use $www_time_off_1[0]->[$count] in a mathematical
expression a few lines ago. Now you assign a string to it.
That is majorly inconsistant, and is going to cause you no end of trouble.
What does the variable represent? If it represents a number then don't assign
a string. If it doesn't represent a number then don't use it in a mathematical
expression where a number is expected.
If what it represents changes inside a loop, then don't bother asking for help
until you change it to make it consistant.
> }
> $count++;
>}
>
>This really dosn't show why I am using it this way, this is only a very
>small part of the program. But, this is were my problem is.
>
>Ok, in theroy, this is saying that if ether condition is false, it will
>preform the next set of code. But.... its not. I can not get it to go
>into the if statement by using the ||. Does the problem exsist between
>the keyboard and chair ???
No it's saying if both conditions are false it will execute
the 'else' block. If one of the conditions is true it will execute the
'if' block.
>
>
>Here is another one of my nightmares.
>
> if ( $FORM{'on_hour_thurs_1'} >= '13' ) {
Why '13', 13 works well enough. At least you ddin't use ge '13'...
> $FORM{'on_hour_thurs_1'} = $FORM{'on_hour_thurs_1'} - 12;
> }
> if ( $FORM{'off_hour_thurs_1'} ge '13' ) {
Oh now you did use ge... >= 13 is fine you know...
You do realise that 2 ge '13' is true?
Don't use string comparisons on integers and vice-versa...
> $FORM{'off_hour_thurs_1'} = $FORM{'off_hour_thurs_1'} - 12;
>
>[ BTW: yes, the code is very strange, you'd have to see the rest of the
>program to understand ]
>
>The problem is, this works, why doesn't this ?!?
>
> if ( $FORM{'on_hour_thurs_1'} lt '13' ) {
> $FORM{'on_hour_thurs_1'} = $FORM{'on_hour_thurs_1'} - 12;
> }
>
>I have to use the >= or else it won't work what so ever!!
Probably becuse 2,3,4,5,6,7,8 and 9 are all greater than 13 when
compared as strings.
>Can anyone explain why this is refusing to co-operate ?
I suggest you read 'perldoc perlop'. The read 'perldoc perl'. Then read
most of the man pages is references. Then delete you code and start again,
this time use the right operators, and use your variables consistantly.
And use a nicer way of naming hash keys.
And look at few of the Date and Time modules available for perl.
--
Sam
I would like to tell you that Perl is simple in its complexity. But some
people won't understand that. So pretend I didn't say that, unless you
do. --Larry Wall
------------------------------
Date: Thu, 21 Oct 1999 20:46:31 -0700
From: tgy@chocobo.org (Neko)
Subject: Re: How do you split a string into fixed sized pieces?
Message-Id: <kNsPOF76+hAec4A74PAud2ews1+7@4ax.com>
On Thu, 21 Oct 1999 15:52:06 -0700, Larry Rosler <lr@hpl.hp.com> wrote:
>In article <8E66B7B0Dscottfrazerericssonc@cnn.exu.ericsson.se> on 21 Oct
>1999 22:03:28 GMT, Scott Frazer <scott.frazer@ericsson.com> says...
>> OK, I'm new to Perl and can't figure out how to split a long string
>> into fixed sized pieces. For example:
>>
>> input -> "0123456789abcdef"
>> output -> an array of "0123", "4567", "89ab", "cdef"
>>
>> It seems like I should be able to use 'split', but since there are
>> no delimiters I don't know how to do it. I tried various regexp's
>> to force a match on four of any character, but couldn't work it out.
>
>I didn't do well with split() either, without another filter for null
>strings. Perhaps someone else can help.
@a = split /(?<=\G....)/s;
[snip regex, unpack, and split/grep solutions]
--
Neko | tgy@chocobo.org | Will hack Perl for a moogle stuffy! =^.^=
------------------------------
Date: Fri, 22 Oct 1999 01:48:50 GMT
From: bmccoy@foiservices.com (Brett W. McCoy)
Subject: Re: in need of example...
Message-Id: <slrn80vgsl.o8a.bmccoy@moebius.foiservices.com>
Also Sprach lt lindley <ltl@rgsun5.viasystems.com>:
>Neko <tgy@chocobo.org> wrote:
>:>On Thu, 21 Oct 1999 07:32:06 -0700, Larry Rosler <lr@hpl.hp.com> wrote:
>
>:>>Just one reason. Please?
>
>:>From DirHandle:
>:> The only objective benefit to using DirHandle is that it avoids
>:> namespace pollution by creating globs to hold directory handles.
>
>Well, so did the local typeglob assignment that Larry did. So that
>isn't a good enough reason.
I thought the Perl motto was TMTOWTDI. Some people like to use DirHandle,
some like to use the local typeglob assignment. They both get the job
done. That's why I use Perl: to get a job done.
--
Brett W. McCoy bmccoy@foiservices.com
Computer Operations Manager (Alpha Geek) http://www.foiservices.com
FOI Services, Inc./DIOGENES 301-975-0110
---------------------------------------------------------------------------
------------------------------
Date: 22 Oct 1999 02:52:43 GMT
From: lt lindley <ltl@rgsun40.viasystems.com>
Subject: Re: in need of example...
Message-Id: <7uojhr$3l6$1@rguxd.viasystems.com>
Brett W. McCoy <bmccoy@foiservices.com> wrote:
:>Also Sprach lt lindley <ltl@rgsun5.viasystems.com>:
:>>Neko <tgy@chocobo.org> wrote:
:>>:>On Thu, 21 Oct 1999 07:32:06 -0700, Larry Rosler <lr@hpl.hp.com> wrote:
:>>
:>>:>>Just one reason. Please?
:>>
:>>:>From DirHandle:
:>>:> The only objective benefit to using DirHandle is that it avoids
:>>:> namespace pollution by creating globs to hold directory handles.
:>>
:>>Well, so did the local typeglob assignment that Larry did. So that
:>>isn't a good enough reason.
:>I thought the Perl motto was TMTOWTDI. Some people like to use DirHandle,
:>some like to use the local typeglob assignment. They both get the job
:>done. That's why I use Perl: to get a job done.
OK. Then there has already been a better answer in this thread.
I don't recall who made it.
To paraphrase an appropriate answer to Larry's request for
a reason to use DirHandle:
"I use DirHandle because the object oriented syntax looks
better to me. It is a matter of style and taste. I prefer
the DirHandle objects for asthetic reasons."
Then we couldn't argue about your Perl (code bloat not
withstanding). But your style sense is still fair game. :-)
--
// Lee.Lindley /// I used to think that being right was everything.
// @bigfoot.com /// Then I matured into the realization that getting
//////////////////// along was more important. Except on usenet.
------------------------------
Date: Thu, 21 Oct 1999 21:50:16 -0500
From: sqz1909@is2.nyu.edu (Sergej Zoubok)
Subject: Re: Mastering Algorithms With Perl-- Reviews
Message-Id: <sqz1909-2110992150160001@dial6-async17.dial.net.nyu.edu>
My thanks to all who replied her and via email. The general consensus is
that it's a valuable reference.
Regards,
Sergej
In article <i78MOKaV3W9nk02PcwlRretuYWJM@4ax.com>, Dave Cross
<dave@dave.org.uk> wrote:
>On Tue, 19 Oct 1999 16:47:33 GMT, sqz1909@is2.nyu.edu (Sergej Zoubok)
>wrote:
>
>>Has anyone read "Mastering Algorithms With Perl" by Jon Orwant, et al.? It
>>seems like an ideal addition to my Perl reference shelf but I've been
>>unable to find a copy to browse. I'd be interested, as I hope others
>>would be, in hearing more about it.
>
>It's great. Buy it!
>
>--
>Dave Cross <dave@dave.org.uk>
><http://www.dave.org.uk>
--
Sergej Zoubok
Samarkand Inc.
"Fine Products for the Go Enthusiast"
webmaster@samarkand.net
<http://www.samarkand.net>
------------------------------
Date: Fri, 22 Oct 1999 01:51:49 GMT
From: bmccoy@foiservices.com (Brett W. McCoy)
Subject: Re: mv file routine in perl for NT & Unix?
Message-Id: <slrn80vh29.o8a.bmccoy@moebius.foiservices.com>
Also Sprach John Kit <jkit@jpmorgan.com>:
>Is there a move file, mv, routine in perl which would
>work for NT & Unix?
See the documentation for rename.
--
Brett W. McCoy bmccoy@foiservices.com
Computer Operations Manager (Alpha Geek) http://www.foiservices.com
FOI Services, Inc./DIOGENES 301-975-0110
---------------------------------------------------------------------------
------------------------------
Date: 21 Oct 1999 20:58:52 -0500
From: Kent Perrier <kperrier@blkbox.com>
Subject: Re: NEED URGENT HELP
Message-Id: <984EA8B9BB967EDD.118D017CBD7B8820.1D2F500125BA75B9@lp.airnews.net>
Tom Briles <sariq@texas.net> writes:
>
> Do you know what barbecue sauce goes best with penguin?
>
> I can't find a good one.
Probably the same one that tastes real good with Spotted Owl.
Kent
--
When you think of Red Hat, think of Perrier (bottled water). Water is
essentially free, or at least very low cost to produce. Red Hat is
selling the name, and some service.
-- Ed Young
------------------------------
Date: 22 Oct 1999 03:52:46 GMT
From: efflandt@xnet.com (David Efflandt)
Subject: Re: Obtaining a users login username
Message-Id: <slrn80vntg.ck.efflandt@efflandt.xnet.com>
On Thu, 21 Oct 1999 21:44:02 GMT, cwoodsy@my-deja.com
<cwoodsy@my-deja.com> wrote:
>
> When a user logs into my site using the .htaccess method, is there
>any way that I can get the username with a perl script, so I can
>display it and have a unique web page according to who is logged in?
This really has nothing to do with Perl. Check your webserver docs. In
apache it is $ENV{REMOTE_USER}, but only if the CGI itself is in the
protected dir.
>Thanks,
>
>Chris Woods
>cwwoodsy@yahoo.com
--
David Efflandt efflandt@xnet.com http://www.xnet.com/~efflandt/
http://www.de-srv.com/ http://cgi-help.virtualave.net/
http://thunder.prohosting.com/~cv-elgin/
------------------------------
Date: Fri, 22 Oct 1999 03:06:33 GMT
From: "Tom Dawson" <tomd@istar.ca>
Subject: Perl and NT IIS4
Message-Id: <ZwQP3.7476$Y6.731162@news1.telusplanet.net>
We recently got a copy of Apian's Survey Pro with its Net.Collect module to
provide web enabled surveys.
The paper based version of this is great but I have been struggling trying
to get the .pl file to run on our IIS 4 server.
I have been back and forth with their technical support group but they can't
offer an answer that works.
I have tried finding someone that knows what they are doing with Perl and
IIS 4 to no avail, then a friend suggested I try this news group.
I have the HTML file, the Perl .pl file and access to the server via
PCAnywhere and would be interested in finding someone that is confident they
can get this running by talking me through the necessary changes to either
the files, or the server.
I am prepared to pay the "going rate" for this service and would like to
hear from anyone that thinks they can help. Please email me at:
tomd@tsdconsulting.ca
Thanks
Tom
------------------------------
Date: 22 Oct 1999 01:31:04 GMT
From: ebohlman@netcom.com (Eric Bohlman)
Subject: Re: Persistent objects
Message-Id: <7uoeoo$jcn$2@nntp1.atl.mindspring.net>
Robert Kiesling (kiesling@monolith2.nodomain.nowhere) wrote:
: I'm looking for literature that discusses persistent objects...
: mainly implementations in Perl for end-user data and indiviual
: structures, although if anyone has looked into saving the state of a
: perl interpreter, using any implementation langauge, I'd be interested
: in that also. Any bibliographies or references would be welcome.
: Thanks!
Damian Conway's _Object Oriented Perl_ is probably the best place to start.
------------------------------
Date: Thu, 21 Oct 1999 19:38:02 +1700
From: dreon <famouswizardNOfaSPAM@yahoo.com.invalid>
Subject: Re: Problem enabling Cookies when using LWP::UserAgent
Message-Id: <11f733ec.c6c38224@usw-ex0101-008.remarq.com>
BEFORE the line
$ua->cookie_jar($cookies); #enable cookies
add the following line:
$cookies = HTTP::Cookies::Netscape->new(
File => "$ENV{HOME}/.netscape/cookies",
AutoSave => 1,
);
Worked for me after I added this! :-)
In article <37C562AE.8BE81D47@home.com>, Douglas Galbraith
<douglas@home.com> wrote:
> Hello, and thanks in advance for the help.
> I'm trying to read data from a Web site that requires the perl
> script to
> have cookies enabled. According to the LWP::UserAgent
> documentation,
> the method "ua->cookie_jar($cookies);" should tell the server that
> cookies are accepted. But the response from the server states:
> "We're sorry, but you must have cookies enabled in your browser
> to
> use our web site."
> Below is a simple script illustrating my problem.
> How do I get it to work?
> use LWP::UserAgent;
> $ua = LWP::UserAgent->new;
> $ua->cookie_jar($cookies); #enable cookies
> $request = HTTP::Request->new(GET =>
> 'http://quotes.freerealtime.com/rt/frt/quotes?symbol=ACTL');
> $request->authorization_basic('D046', 'D046'); #password
> $response = $ua->request($request);
> print $response->content;
> thanks,
> DGalbra862@aol.com
* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!
------------------------------
Date: Fri, 22 Oct 1999 02:45:40 GMT
From: Rick Delaney <rick.delaney@home.com>
Subject: Re: Reference challenge
Message-Id: <380FCFE2.AFDE1B56@home.com>
[posted & mailed]
Sean McAfee wrote:
>
> The challenge:
>
> Write a 100% Perl, 100% portable, 100% bulletproof subroutine that will
> determine whether its single argument is a reference, and if so, what type
> of reference it is.
>
> Conditions:
...
> 2) You may not use any implementation details of your particular version
> of Perl. If the manual states only that a given function returns FALSE
> under certain circumstances, then you may not distinguish its return value
> between the values undef, "", 0, and "0".
Is this condition the one that rules out stringification as shown in the
> thread entitled "Underlying data structure behind blessed reference".
?
I *think* this meets all your conditions, but I guess you need to be the
judge of that.
#!/usr/local/bin/perl -w
use strict;
my $scalar = bless \$a => 'ARRAY';
my $array = bless [1, 2] => 'HASH';
my $hash = bless {1 => 2} => 'GLOB';
my $glob = bless \*STDOUT => 'CODE';
my $code = bless sub { 1 } => 'SCALAR';
BEGIN {
my %text = (
'$' => 'SCALAR',
'@' => 'ARRAY',
'%' => 'HASH',
'*' => 'GLOB',
'&' => 'CODE',
);
sub reference ($) {
my $ref = shift;
return unless ref $ref;
for (qw/* $ @ %/) {
eval "$_\$ref";
if ($@ !~ /Not an? $text{$_} reference/) {
return $text{$_};
}
}
return $text{'&'};
}
}
for ($scalar, $array, $hash, $glob, $code, ) {
print "Here be ", reference $_, " ref\n";
}
--
Rick Delaney
rick.delaney@home.com
------------------------------
Date: Thu, 21 Oct 1999 23:02:08 -0400
From: "Dave Kaufman" <david@gigawatt.com>
Subject: Re: Reference challenge
Message-Id: <ctQP3.32$cV4.4226@nntp1>
Sean McAfee <mcafee@waits.facilities.med.umich.edu> wrote...
> The challenge:
>
> Write a 100% Perl, 100% portable, 100% bulletproof subroutine that will
> determine whether its single argument is a reference, and if so, what type
> of reference it is.
The Solution:
sub isref {
return ref{shift};
}
-dave
------------------------------
Date: Thu, 21 Oct 1999 23:04:06 -0400
From: "Dave Kaufman" <david@gigawatt.com>
Subject: Re: Reference challenge
Message-Id: <2vQP3.33$cV4.4160@nntp1>
Dave Kaufman <david@gigawatt.com> wrote...
> Sean McAfee <mcafee@waits.facilities.med.umich.edu> wrote...
> > The challenge:
> >
> > Write a 100% Perl, 100% portable, 100% bulletproof subroutine that will
> > determine whether its single argument is a reference, and if so, what
type
> > of reference it is.
>
> The Solution:
> sub isref {
> return ref{shift};
> }
oops -- i used curly braces instead of parens where neither was really
needed... that's hardly bulletproof portable perl, eh?
sub isref {
return ref shift;
}
-dave
------------------------------
Date: Fri, 22 Oct 1999 03:10:46 GMT
From: Rick Delaney <rick.delaney@home.com>
Subject: Re: Reference challenge
Message-Id: <380FD5C6.5A6A6AC1@home.com>
[posted & mailed]
Dave Kaufman wrote:
>
> Sean McAfee <mcafee@waits.facilities.med.umich.edu> wrote...
> > The challenge:
> >
> > Write a 100% Perl, 100% portable, 100% bulletproof subroutine that will
> > determine whether its single argument is a reference, and if so, what type
> > of reference it is.
>
> The Solution:
> sub isref {
> return ref{shift};
> }
That will always return 'HASH' (and an "odd" warning). Even if you got
the brackets right, it is easily fooled.
my $array = bless [1, 2] => 'HASH';
print isref($array), "\n";
You really should read the thread Sean pointed to.
--
Rick Delaney
rick.delaney@home.com
------------------------------
Date: Thu, 21 Oct 1999 23:11:45 -0400
From: Jeff Pinyan <jeffp@crusoe.net>
Subject: Re: Reference challenge
Message-Id: <Pine.GSO.4.10.9910212309090.15158-100000@crusoe.crusoe.net>
On Oct 22, Sean McAfee blah blah blah:
> Write a 100% Perl, 100% portable, 100% bulletproof subroutine that will
> determine whether its single argument is a reference, and if so, what type
> of reference it is.
How is my regular expression,
($type) = $obj =~ /([A-Z]+)\(0x[a-f0-9]+\)\z/;
not valid as the subroutine?
sub trueref { (shift =~ /([A-Z]+)\(0x[a-f0-9]+\)\z/)[0] }
> 1) You may not call any functions which are not themselves 100% Perl, except
> those described in the perlfunc man page. This includes UNIVERSAL::isa().
Is the regex a bad thing?
> 2) You may not use any implementation details of your particular version
> of Perl. If the manual states only that a given function returns FALSE
> under certain circumstances, then you may not distinguish its return value
> between the values undef, "", 0, and "0".
Ok.
> 3) If the reference passed is a coderef, you may not execute it to establish
> this fact. (The subroutine may take a long time, crash the interpreter,
> or do other undesirable things, after all.)
Yeah, I don't do that.
--
MIDN 4/C PINYAN, USNR, NROTCURPI
jeff pinyan japhy@pobox.com
perl stuff japhy+perl@pobox.com
CPAN ID: PINYAN http://www.perl.com/CPAN/authors/id/P/PI/PINYAN/
------------------------------
Date: Thu, 21 Oct 1999 23:19:29 -0400
From: Jeff Pinyan <jeffp@crusoe.net>
Subject: Re: Reference challenge
Message-Id: <Pine.GSO.4.10.9910212318420.15158-100000@crusoe.crusoe.net>
On Oct 22, Rick Delaney blah blah blah:
> my $array = bless [1, 2] => 'HASH';
> print isref($array), "\n";
Your code, Rick, only did 5 of the reference types...
SCALAR, ARRAY, HASH, CODE, IO, REF, GLOB
I believe those are all seven.
--
MIDN 4/C PINYAN, USNR, NROTCURPI
jeff pinyan japhy@pobox.com
perl stuff japhy+perl@pobox.com
CPAN ID: PINYAN http://www.perl.com/CPAN/authors/id/P/PI/PINYAN/
------------------------------
Date: Fri, 22 Oct 1999 03:32:42 GMT
From: Rick Delaney <rick.delaney@home.com>
Subject: Re: Reference challenge
Message-Id: <380FDAEA.24E0D8D9@home.com>
[posted & mailed]
Jeff Pinyan wrote:
>
> On Oct 22, Rick Delaney blah blah blah:
>
> > my $array = bless [1, 2] => 'HASH';
> > print isref($array), "\n";
>
> Your code, Rick, only did 5 of the reference types...
>
> SCALAR, ARRAY, HASH, CODE, IO, REF, GLOB
>
> I believe those are all seven.
Oh yeah. Oh well, I still don't see what's wrong with your
stringification solution anyway.
--
Rick Delaney
rick.delaney@home.com
------------------------------
Date: Fri, 22 Oct 1999 10:20:10 -0400
From: Daniella Malin <daniella@cs.umass.edu>
Subject: sorting hashes in an array of hashes
Message-Id: <3810729A.8B25C8F6@cs.umass.edu>
Thanks in advance, I'm kinda new to this and I
must be doing something stupid but I don't see it
and the faq and back messages on this topic
haven't helped me decipher.
I have an array of hashes. I can access any element like so:
$ptr[$num]{$term}
Doesn't that mean that $ptr[$num] is a hash?
Then I should be able to operate on it as such.
I want to print each of my hashes out reverse value order. Why does
for $term ( sort { $hash{$b} <=> $hash{$a} } keys $ptr[$num] } )
give me syntax errors?
-Daniella
------------------------------
Date: Fri, 22 Oct 1999 02:52:01 GMT
From: Rick Delaney <rick.delaney@home.com>
Subject: Re: sorting hashes in an array of hashes
Message-Id: <380FD160.9AEE6AED@home.com>
[posted & mailed]
Daniella Malin wrote:
>
> I have an array of hashes. I can access any element like so:
> $ptr[$num]{$term}
>
> Doesn't that mean that $ptr[$num] is a hash?
No, it is a reference to a hash.
> Then I should be able to operate on it as such.
Yes.
> I want to print each of my hashes out reverse value order. Why does
>
> for $term ( sort { $hash{$b} <=> $hash{$a} } keys $ptr[$num] } )
>
> give me syntax errors?
Because a reference is a scalar and C<keys> expects a hash.
keys %{ $ptr[$num] };
perldoc perlref
Also, %hash is defined nowhere so don't use that in the sort sub.
Access the elements as you said you can above.
--
Rick Delaney
rick.delaney@home.com
------------------------------
Date: Fri, 22 Oct 1999 03:05:36 GMT
From: mgjv@comdyn.com.au (Martien Verbruggen)
Subject: Re: sorting hashes in an array of hashes
Message-Id: <4wQP3.344$WM1.4793@nsw.nnrp.telstra.net>
On Fri, 22 Oct 1999 10:20:10 -0400,
Daniella Malin <daniella@cs.umass.edu> wrote:
> Thanks in advance, I'm kinda new to this and I
> must be doing something stupid but I don't see it
> and the faq and back messages on this topic
> haven't helped me decipher.
>
> I have an array of hashes. I can access any element like so:
> $ptr[$num]{$term}
>
> Doesn't that mean that $ptr[$num] is a hash?
A reference to a hash. Not a hash
> Then I should be able to operate on it as such.
Indeed. As a reference to a hash :)
> I want to print each of my hashes out reverse value order. Why does
>
> for $term ( sort { $hash{$b} <=> $hash{$a} } keys $ptr[$num] } )
Because %hash doesn't exist?
You sure it's a _syntax_ error? Next time, please include the error.
And make sure you run with -w and 'usr strict';
$ptr[$num] is a reference to a hash. I suspect that the error you get
is something like:
Type of arg 1 to keys must be hash (blablabla)
right?
Maybe this will help:
#!/usr/local/bin/perl -w
use strict;
my @ptr = (
{ qw(a 1 b 2 c 3 d 4 e 5 f 6 g 7) },
{ qw(a 6 b 7 c 1 d 4 e 5 f 2 g 3) },
);
# This is possible, but gets a bit confusing
for (my $num = 0; $num < @ptr; $num++)
{
print "\n";
foreach my $term (
sort { $ptr[$num]->{$a} <=> $ptr[$num]->{$b} }
keys %{$ptr[$num]}
)
{
print "$term -> $ptr[$num]->{$term}\n";
}
}
# This, IMO, is much better and cleaner
foreach my $href (@ptr)
{
print "\n";
foreach my $term ( sort { $href->{$a} <=> $href->{$b} } keys %{$href} )
{
print "$term -> $href->{$term}\n";
}
}
As you can see, I am someone who explicitly dereferences. The
reason I do that, is because then it is immediately clear what is a
reference, and what isn't.
The documentation about all this, which you probably already read:
# perldoc perlref
# perldoc perldata
# perldoc perllol
# perldoc perldsc
Martien
--
Martien Verbruggen |
Interactive Media Division | Unix is user friendly. It's just
Commercial Dynamics Pty. Ltd. | selective about its friends.
NSW, Australia |
------------------------------
Date: 22 Oct 1999 03:57:28 GMT
From: pete@theory2.phys.cwru.edu (Peter J. Kernan)
Subject: Re: subroutine
Message-Id: <slrn80vo58.15d2.pete@theory2.phys.cwru.edu>
On Thu, 21 Oct 1999 14:17:46 -0700, Larry Rosler <lr@hpl.hp.com> wrote:
.=ObPerlGolf:
.=
.= sub PrepareTextForDB { (my ($x) = @_) =~ s/'/''/g; $x }
.=
.=All spaces except the first one are optional.
but what if you take a ref in;
{ ${ (shift) } =~ s/'/''/g}
--
Pete
------------------------------
Date: 22 Oct 1999 03:36:16 GMT
From: sholden@pgrad.cs.usyd.edu.au (Sam Holden)
Subject: Re: Unlink
Message-Id: <slrn80vmtg.4bb.sholden@pgrad.cs.usyd.edu.au>
On Thu, 21 Oct 1999 16:51:59 -0700,
Shuba Swaminathan <sswaminathan@micron.com> wrote:
>Folks,
>
>Have you had any experience with "unlink" being
>unreliable? I understand unlink will not work with
>wildcards but is there a restriction for the
>concatenation operator? My code line looks like:
>
>unlink("LOCK.$num") or eval {print ERROR "022
>Could not delete lock file\n"; exit 1;};;
There is no concatanation operator there. If $num = '001' then
you will be trying to delete the file : 'LOCK.001'. If you want to
delete 'LOCK001' then remove the . from the string...
>
>I am interfacing this code with another system,
>which I am starting to think, is where the problem
>lies. The folks there are pointing to my script.
>Please help! Your input is much appreciated.
Why don't you ask perl what the problem is....
unlink("LOCK.$num") or die "022 Could not delete lock file $!";
Feel free to change the die into your convoluted eval mess if you like ;)
--
Sam
Can you sum up plan 9 in layman's terms? It does everything Unix does
only less reliably.
--Ken Thompson
------------------------------
Date: 22 Oct 1999 01:32:44 GMT
From: ebohlman@netcom.com (Eric Bohlman)
Subject: Re: Using Modules
Message-Id: <7uoers$jcn$3@nntp1.atl.mindspring.net>
Samay (samay1NOsaSPAM@hotmail.com.invalid) wrote:
: Sometime..I want to use module to see how it works, and there are no
: simple ways I can find that out..
: Included in Perl Core Distribution from ActiveX, the XML modules are
: like that..
: One simple example how to use it would be suffice..
: thanks..
Most modules have built-in documentation in POD format. Use perldoc to
read it. For example,
perldoc XML::Parser
would display the documentation for XML::Parser, which includes an
example of how to use it.
------------------------------
Date: Thu, 21 Oct 1999 18:28:24 -0700
From: "David Christensen" <dchristensen@california.com>
Subject: Re: Win32: Communicate w/ printer
Message-Id: <380fba3b_2@news5.newsfeeds.com>
Hi David!
Thanks for your reply!
I'll answer you via email, I don't like dejanews (but im sitting
behind a very robust firewall and have no direct news access:( ).
On Mon, 18 Oct 1999, David Christensen wrote:
> I have been thinking about how to pretty print forms-based
> reports (fonts, tables, graphics, etc.) in a platform independent
> way from a Perl console or Perl/Tk app. No answers yet, just
> some ideas:
>
> 1) Perl/Tk graphics viewer and a postscript or LaserJet PCL
> back-end (how to submit?);
I dislike it, because one has to do all text formating on a canvas
and it should be better to take an advanced program to do this job
of rendering and take an Perl/Tk graphics viewer as one backend and
the printer an another.
> 2) postscript generator and a viewer (ghostscript) (how to
> automate?);
Item 2) is similar (except you think of latex/lout/jade as
postscript generators). It's not so good to program postscript
directly (these programs can do that better ;-).
> 3) HTML generator (Perl/Cgi) and a browser (how to automate?).
Why use a browser as backend (see item 4)? But it would be right,
if you want to start the browser as viewer and the user has to
press the "print button" to hand it over to printer.
4) SGML/XML/HTML generator and jade/dsssl backend to ps, via
ghostscript (as a filter) to any printer language, that's easy
to automate.
5) LaTeX generator and "latex -> dvi -> ps -> printer lang" or
"latex -> dvi -> printer lang", that's easy too.
Disadvantage of item 4) and 5) is the installation of any text
rendering software (maybe docbook dtd+jade are not as large as
LaTeX or lout and it supports many output formats.
> As for querying printer/ queue status, I dunno. Sounds like a
> platform-independent abstraction (Perl API) and
> architecture-specific modules are needed (?).
And that's the problem. I'd like to write a module (e.g.
printer.pm) and encapsulate the different flavours of printer
access. I think it's easy on platforms w/ lpr-support ... that
should be all unixes (aix has a much more sophisticated spooling
system, but emulates the lpr-system. I hope the other unixes do so
too). I have BeOS installed and hope, this will play this game too.
My largest hurdle is an access to win printers. I think, there
should be some information in the registry, and there should be a
program like "print" in dos. I asked Jan Dubois (Programmer and
Maintainer of Win32::OLE), but he couldn't help me. But I think, it
should be possible via OLE?!
> Does anybody else have ideas or experience trying a solution?
>
> --
> David Christensen
> dchristensen@california.com
The module "printer.pm" should detect all kinds of printer access
during initialistion and query the state of all possible printers
on demand. If you want to print something, there should be
something like a filehandle and the usual print/printf/... cmds. No
big deal at all ;-)
That's my state of expierence.
Maybe, we could get it right?
Ciao for now, Dirk
--
Dirk Ruediger, Magdeburg, Germany
Time goes, you say? Ah, no! Alas, Time stays, we go.
Austin Dobson (1840-1921), The Paradox of Time
-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
http://www.newsfeeds.com The Largest Usenet Servers in the World!
------== Over 73,000 Newsgroups - Including Dedicated Binaries Servers ==-----
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
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 V9 Issue 1147
**************************************