[19438] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1633 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Aug 28 00:05:26 2001

Date: Mon, 27 Aug 2001 21:05:07 -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: <998971506-v10-i1633@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Mon, 27 Aug 2001     Volume: 10 Number: 1633

Today's topics:
    Re: Can't get correct IP nobull@mail.com
    Re: DB_File on Tru64.. In-memory DBTree still uses /tmp nobull@mail.com
    Re: delete duplicate records <goldbb2@earthlink.net>
    Re: delete duplicate records <goldbb2@earthlink.net>
    Re: form post to https server, best method nobull@mail.com
        how to get character from string one by one? <cheng@cs.wustl.edu>
    Re: I need perl for beginners (Tad McClellan)
    Re: is perl an 'interpretative' language? (John Lin)
    Re: is perl an 'interpretative' language? <krahnj@acm.org>
    Re: It's to AM for me to think <iltzu@sci.invalid>
    Re: Perl 101 Question <godzilla@stomp.stomp.tokyo>
    Re: redirect stderr for sub-routine nobull@mail.com
    Re: round off operator on Perl? (Martien Verbruggen)
    Re: Tie and complex hashes nobull@mail.com
    Re: tut question nobull@mail.com
    Re: warnings with cgi will crash Win32 perl core <matthew.garrish@sympatico.ca>
    Re: Will Perl report on variables no longer used?? <goldbb2@earthlink.net>
    Re: Will Perl report on variables no longer used?? <goldbb2@earthlink.net>
    Re: Will Perl report on variables no longer used?? <iltzu@sci.invalid>
    Re: Will Perl report on variables no longer used?? (Mark Jason Dominus)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 27 Aug 2001 14:21:45 +0100
From: nobull@mail.com
Subject: Re: Can't get correct IP
Message-Id: <u97kvp3afq.fsf@wcl-l.bham.ac.uk>

"Peter Chan" <peter.cch@mailexcite.com> writes:

> Newsgroups: comp.lang.perl.misc

This is a Perl newsgroup. Please post here only questions that in some
way relate to Perl.

> i try to get the user IP address with the environment variable.
> But it wasn't correct.
> 
> I use this syntax:
> 
> print "IP: $ENV{'REMOTE_ADDR'}\n";

This is Perl code to print out the value of an environment variable.
Why do you believe that the fact that you are reading this variable
using Perl code would give you a different result from reading the
same environment variable in any other language?  If you do not
believe this then why do you post here?

> but what the IP that i get is always like this:
> 
> IP: 127.0.0.1
> IP: 127.0.0.1
> IP: 127.0.0.1
> IP: 127.0.0.1

> Can anyone provide me the solution to get the correct IP such as this:
> IP: 202.188.1.5
> IP: 203.152.100.5
> IP: 202.188.0.133

Speak to the person who set up your web server they may be able to
explain what they have done, why they did it, and how you can get
arround it.

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: 27 Aug 2001 14:13:01 +0100
From: nobull@mail.com
Subject: Re: DB_File on Tru64.. In-memory DBTree still uses /tmp ??
Message-Id: <u9ae0l3aua.fsf@wcl-l.bham.ac.uk>

psaunder@comcen.com.au (Patrick Saunders) writes:

> I've hunted the net like a madman
> for an answer to this.
> My program simply reads lines from STDIN
> and puts them into BTree. I want to use
> memory (we have 12Gb of RAM) but it still uses
> /tmp, even when I use 'undef' as file name 
> as per documentation.

Passing a NULL file pointer to the underlying dbopen(3) implementation
simply informs it that you don't want a _persistant_ file.  How the
underlying DB implementation chooses to deliver a non-persistant
database is up to it.  Of course, on some OS a file in /tmp is the
same thing as being in ordinary (virtual) memory.

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: Mon, 27 Aug 2001 21:36:00 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: delete duplicate records
Message-Id: <3B8AF580.B226B19B@earthlink.net>

Graham Wood wrote:
> 
> Charles Cowdrick wrote:
> 
> > I'm trying to delete duplicate records in a flatfile.
> > I searched Google under "delete duplicate records" and the Camel
> > book, to no avail.
> > Here's my code. I know that line 4 is weird, but I don't know how
> > else to compare the current record with the next record.
> > #!perl
> >
> > RECORD: while (<DATA>) {
> >     next RECORD if $_ eq $_[$_++];
> >  print "$_\n";
> > }
> >
> > print "Duplicates removed.\n";
> >
> > __DATA__
> > NewUser ADAL6322 123456322
> > NewUser ADAM2437 123452437
> > NewUser ADAM5045 123455045
> > NewUser ADAM8340 123458340
> > NewUser ADAM8340 123458340
> > NewUser ADCO6906 123456906
> > NewUser ADCO6906 123456906
> > NewUser ADCO9430 123459430
> 
> This is a good candidate for using a hash.  You can't have duplicate
> keys in a hash so putting your records into one will automatically
> remove the duplicates.
> 
> open(DATA,"yourfile") || die "Can't open yourfile $!\n";
> @data=<DATA>;
> close DATA;
> 
> @temphash{@data}=1; #the 1 is unimportant, just the setting of a value
> for the key is.
> 
> @unique_values=(sort keys(%tmphash));
> 
> You then have unique values in @unique_values.

The more traditional perlish idiom is this:

	@unique_values = do { my %seen; grep !$seen{$_}++, @data };

Which keeps the data items in their original order, and minimizes the
needed scope of the hash.

Also, where your code has:
	@temphash{@data}=1;
You should keep in mind what happens when assignment between lists of
unequal length -- and a hash slice is a list.  This code is effectively
the same as
	@temphash{@data} = ( 1, (undef) x $#data );
It would be faster to either do:
	@temphash{@data} = ();
or, better yet:
	undef @temphash{@data};

These will be faster.

-- 
I'm not a programmer but I play one on TV...


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

Date: Mon, 27 Aug 2001 21:40:37 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: delete duplicate records
Message-Id: <3B8AF695.7720E00C@earthlink.net>

Charles Cowdrick wrote:
> 
> I'm trying to delete duplicate records in a flatfile.
> I searched Google under "delete duplicate records" and the Camel book,
> to no avail.
> Here's my code. I know that line 4 is weird, but I don't know how else
> to compare the current record with the next record.
> #!perl
> 
> RECORD: while (<DATA>) {
>     next RECORD if $_ eq $_[$_++];
>  print "$_\n";
> }

You can't compare the current to the *next*, only to the previous.

my $previous;
while( <DATA> ) {
	next if $_ eq $previous && defined $previous;
	print;
	$previous = $_;
}

> print "Duplicates removed.\n";
> 
> __DATA__
> NewUser ADAL6322 123456322
> NewUser ADAM2437 123452437
> NewUser ADAM5045 123455045
> NewUser ADAM8340 123458340
> NewUser ADAM8340 123458340
> NewUser ADCO6906 123456906
> NewUser ADCO6906 123456906
> NewUser ADCO9430 123459430

Note that since I don't chomp the data, I don't have to add back a
newline when I print it.

-- 
I'm not a programmer but I play one on TV...


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

Date: 27 Aug 2001 15:09:19 +0100
From: nobull@mail.com
Subject: Re: form post to https server, best method
Message-Id: <u9vgj91to0.fsf@wcl-l.bham.ac.uk>

"Nitin G" <niting@onebox.com> writes:

> am looking at implementing a solution where I need to post some data to a
> https server. Before I went down a path that led to nowhere, I wanted to get
> input from people who might have implemented a similar solution. What's the
> best recommended module to use for handling such a situation?

It's far from perfect but I'd still use LWP.  

Setting up HTTPS support for LWP can be fiddly - in particular I've
seen a lot of posts saying it's difficult on Win32 (not that I've ever
tried).  Also if you don't give OpenSSL the requisite settings to
validate server certs your HTTPS transations will fail and LWP will
give you no helpful information at all - the error handling in the
LWP::Protocol::https/IO::Socket::SSL stack is[1] seriously broken.  If
I ever had to do any serious projects invloving HTTPS I'd probably
want to fix this first.

Since most of the time I'm only using HTTPS for encryption (not
validation) I simply switch off server certificate
checking. Unfortunately I can't recall just now the name of the
package variable I had to set.  According to the manual it's
IO::Socket::SSL_verify_mode=0 but ISTR it wasn't quite as
straight-forward as it should have been.

[1] At least was last time I looked.
-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: Mon, 27 Aug 2001 22:26:10 -0500
From: cheng huang <cheng@cs.wustl.edu>
Subject: how to get character from string one by one?
Message-Id: <9mf2tu$q18$1@newsreader.wustl.edu>

Instead of using substr(), can sb. give a better solution? Thanks.

-- Cheng


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

Date: Mon, 27 Aug 2001 22:23:51 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: I need perl for beginners
Message-Id: <slrn9om05n.pqa.tadmc@tadmc26.august.net>

Bob Walton <bwalton@rochester.rr.com> wrote:

>I highly recommend the boox
>"Learning Perl" by Schwartz and Christiansen, O'Reilly.


Do not buy that (2nd edition) book.

Buy the 3rd edition of that book instead (By Schwartz and Phoenix).


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


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

Date: 27 Aug 2001 20:04:20 -0700
From: johnlin@chttl.com.tw (John Lin)
Subject: Re: is perl an 'interpretative' language?
Message-Id: <a73bcad1.0108271904.15863e30@posting.google.com>

Ilmari Karonen wrote
> Ilya Martynov wrote:

> >I'm not Perl internals guru but AFAIK Perl is simular to
> >bytecode-interpetation like Java.

> Well, sort of.  What perl compiles your program into is actually a sort
> of a parse tree, known as the optree.  It may or may not resemble what
> you originally wrote very much, after the magic tokenizer, the parser,
> and the peephole optimizer have been over it, but it does contain enough
> information to "deparse" it into more or less equivalent code.

Can we separate "language" from "how program is run - compiled or interpreted"?
Can this "language" have different "implementation" from the existing one?

For example, BASIC is a "language".  We can run BASIC by either interpreter or
by compiled binary machine code.

Another example, Java is the "language".  We can compile Java programs into
either byte code or machine code to run.

Then, for Perl, it is a "language".  Conceptually, if we don't touch the
internal "implementation", the following Perl program:

for my $i (1..9) {
    for my $j (1..9) {
        print "$i x $j = ",$i*$j;
    }
}

can be equivalent to this Pascal program:

var i,j : integer;
for i = 1 to 9
    for j = 1 to 9
        writeln(i,' x ',j,' = ',i*j);

and equivelent to this C program:

#include <stdio.h>
void main() {
    int i,j;
    for(i=1;i<=9;i++)
        for(j=1;j<=9;j++)
            printf("%d x %d = %d\n",i,j,i*j);
}

Ideally, the Perl program should be able to be compiled into machine code
just like the Pascal program and the C program.

I believe there exists such a subset of Perl that programs in "Perl language"
are compiled-linked then executed from machine codes.

When your program uses 'grep', the linker links grep.lib into your binary code
at linking-time; otherwise no extra "grep" implementation will be included.

If your program doesn't use regular expressions, the regex-engine will not be
linked into your binary executable.

Basically the subset should avoid including the entire perl interpreter into
your machine code, thus the subset should exclude the usages of
    eval, AUTOLOAD, require, ...

Using this "excluding" method, if some functionality is hard to modulize
into a library to be linked at linking-time, then we can exclude the usage of it
from the subset.  Exclude this and exclude that, finally we will find
a subset of Perl language that can be compiled-linked then run.

Do you think so?
A language is a language and it can be separated from how it is implemented?

Thank you.
John Lin
--
Does perlcc always include the whole perl interpreter into the executable?


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

Date: Tue, 28 Aug 2001 03:55:50 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: is perl an 'interpretative' language?
Message-Id: <3B8B16B4.E65A024F@acm.org>

John Lin wrote:
> 

use integer;

> for my $i (1..9) {
>     for my $j (1..9) {
>         print "$i x $j = ",$i*$j;

         print "$i x $j = ",$i*$j, "\n";

>     }
> }
> 
> can be equivalent to this Pascal program:
> 
> var i,j : integer;
> for i = 1 to 9
>     for j = 1 to 9
>         writeln(i,' x ',j,' = ',i*j);
> 
> and equivelent to this C program:
> 
> #include <stdio.h>

#include <stdlib.h>

> void main() {

int main( void ) {

>     int i,j;
>     for(i=1;i<=9;i++)
>         for(j=1;j<=9;j++)
>             printf("%d x %d = %d\n",i,j,i*j);

return EXIT_SUCCESS;

> }



:-)
John
-- 
use Perl;
program
fulfillment


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

Date: 28 Aug 2001 01:36:44 GMT
From: Ilmari Karonen <iltzu@sci.invalid>
Subject: Re: It's to AM for me to think
Message-Id: <998962555.11327@itz.pp.sci.fi>

In article <9me1e0$c5n$1@mamenchi.zrz.TU-Berlin.DE>, Anno Siegel wrote:
>
>That could only happen if the overhead of "shift @array" vs. "$array[ 0]"
>is greater than one round through the loop.  I was betting that it isn't
>(and still do), but you're right to point it out.

Besides, it could well be that shift() is actually faster than fetching
an array element by index.  No, I'm not going to benchmark it right now.

-- 
Ilmari Karonen -- http://www.sci.fi/~iltzu/
"Get real!  This is a discussion group, not a helpdesk.  You post something,
we discuss its implications.  If the discussion happens to answer a question
you've asked, that's incidental."           -- nobull in comp.lang.perl.misc




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

Date: Mon, 27 Aug 2001 18:57:33 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Perl 101 Question
Message-Id: <3B8AFA8D.2B7A8AB1@stomp.stomp.tokyo>

Logan Shaw wrote:
 
>  Jimmy wrote:

(snipped)

> >I'm trying to solve what should be a pretty simple problem.  If you can
> >help, please read on.

> >I have a couple of hundred text files that are not formatted very well.
> >Here's an example:

> I'd do it something like this:
 
>         #! /usr/local/bin/perl
 


It is prudent to test code before posting. Doing so
will assist you in avoiding this embarrassment of
posting code which does not work.


Godzilla!


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

Date: 27 Aug 2001 14:33:58 +0100
From: nobull@mail.com
Subject: Re: redirect stderr for sub-routine
Message-Id: <u93d6d39vd.fsf@wcl-l.bham.ac.uk>

"David Hilsee" <davidhilseenews@yahoo.com> writes:

> The idea is covered in perldoc, if you can find it (perldoc -q local worked
> for me):
> "How can I make a filehandle local to a subroutine?"

Be aware that as discussed here within the last few weeks,
local()ising the STDERR file handle did not "do the right thing" wrt
to implicit STDERR operations until relatively recently (5.6.0).

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: Tue, 28 Aug 2001 01:29:56 GMT
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: round off operator on Perl?
Message-Id: <slrn9olt0k.7cl.mgjv@verbruggen.comdyn.com.au>

On 24 Aug 2001 13:07:38 -0500,
	Logan Shaw <logan@cs.utexas.edu> wrote:
> In article <3B869590.F0B56210@yahoo.com>,
> Valentin 30IR976  <radiotito@yahoo.com> wrote:
>>Hello. I have results on my Perl program like 2.5, 87.9, and I want to
>>have only the "integer" part of this number. I know I can do something
>>like:
>>
>>printf("%d",87.9);
>>
>>and it prints 87 (what I want), but my question is if Perl has any
>>operator/function that make this thing automatic.
> 
> What do you mean by "automatic"?  Presumably, when the Perl
> interpreter executes
> 
> 	printf ("%d%", 87.9);
> 
> it doesn't require the user's assistance to finish.  So in my mind,
> it's automatic.
> 
> Do you mean you'd like to be able to do this:
> 
> 	$x = 3.2;
> 	print "x is $x";
> 
> and have it print "x is 3"?  I don't think there's a way to do that.

Not entirely automatically, but...

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

   @Tie::TruncatedScalar::ISA = "Tie::StdScalar";
sub Tie::TruncatedScalar::FETCH { sprintf "%d", ${$_[0]} }

tie my $x, "Tie::TruncatedScalar";
$x = 3.2;
print "\$x is $x\n";

Martien
-- 
Martien Verbruggen                      |
Interactive Media Division              | "In a world without fences,
Commercial Dynamics Pty. Ltd.           |  who needs Gates?"
NSW, Australia                          |


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

Date: 27 Aug 2001 20:17:12 +0100
From: nobull@mail.com
Subject: Re: Tie and complex hashes
Message-Id: <u9snedtirr.fsf@wcl-l.bham.ac.uk>

"Mark Riehl" <mark.riehl@agilecommunications.com> writes:

> I'd like to use Tie so that I can print the hash using insertion order.
> I've used Tie before, but not on nested hashes.
> 
> I'm using
> 
> tie %RxTxData, "Tie::IxHash";
> 
> at global scope, but I'm assuming that it applies only to the keys at the
> top level.

At the risk of sounding like Godzilla, I suggest that rather than
assuming anything you should experiment.  Not having looked at the
implementation of Tie::IxHash but having been involved in a number of
discussions here anout how fiendishly difficult it is to implement a
tied hash that automagically ties autovivified subhashes I strongly
suspect your assumption is correct.  That said I would not, in your
position, have posted without looking at the
source/documentation/behaviour of Tie::IxHash to be sure.

> How do I use Tie for one of the nested hashes, for example:
> 
> $RxTxData{$key}{statistics}{$key2}

Assuming your assumprion to be right, the brute force solution is to
explcitly tie that which requires tying.

foreach my $key (whatever) {
  tie %{$RxTxData{$key}{statistics}}, 'Tie::IxHash';
}

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: 27 Aug 2001 14:48:31 +0100
From: nobull@mail.com
Subject: Re: tut question
Message-Id: <u9zo8l1umo.fsf@wcl-l.bham.ac.uk>

Chris Etzel <cetzel@midsouth.rr.com> writes:

> Subject: Re: tut question

Please use the "Subject" to you post to describe the subject of you post. 

> I have 'Learning Perl' by Oreilly and it has helped me learn a bunch,
> but unfortunately it is very vague on definition and usage of $_ and @_

As a rule of thumb, when you arrive at the point where you find a
beginners' tutorial manual is glossing-over things too much for your
liking, then it is time to reach for the reference manual.

> ... any suggestions on getting clear definitions and usage examples of
> these two things.

Clear definitions can be found in the relevant reference manual: perlvar.

Examples of use of these variables can be found by looking up the
refence manaual entries on the constucts mentioned in the definitions
of these variables given in perlvar.

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: Mon, 27 Aug 2001 23:58:29 -0400
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: warnings with cgi will crash Win32 perl core
Message-Id: <ZCEi7.25092$Hr2.1491009@news20.bellglobal.com>


"Godzilla!" <godzilla@stomp.stomp.tokyo> wrote in message
news:3B8A6AA7.F9E533A1@stomp.stomp.tokyo...
> You should research your shovels better before piling
> mule manure within this group. This child's plastic
> play toy shovel you use, is most inefficient.
>
>
> Run this script from a DOS command line, then via a browser.
>
>
> #!perl -w
>
> BEGIN
>  {
>   use CGI::Carp qw(carpout);
>   open(LOG, ">carpout.txt");
>   carpout(*LOG);
>  }
>
> print "Content-type: text/plain\n\n";
>
> print substr ($ENV{REMOTE_ADDR}, 0, 6);
>
> exit;
>

What's so difficult to understand Queen of Dementia? When you run this from
the command line, you are not going to get a REMOTE_ADDR returned to you
(it's an environment variable passed from the *server*, thus the reason why
you have test your cgi scripts from a server! amazing, eh?). You're also not
going to have the warning displayed to your prompt because you are using
CGI::Carp, which is redirecting your warning signal to the error file you
opened in the BEGIN block (and which was unlikely ever meant for testing
from the command line). To quote from the perldocs:


> use CGI::Carp

> And the standard warn(), die (), croak(), confess() and carp() calls will
> automagically be replaced with functions that write out nicely
time-stamped
> messages to the HTTP server error log.

Remove the BEGIN block and you'll magically find that warnings goes back to
doing what it always does: reminding you of your idiocy. Maybe you should
take a few minutes and run down to the store and buy a copy of 'Learning
Perl' before you spout off at everyone who tries to point out how illogical
your self-professed logic is.

Matt




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

Date: Mon, 27 Aug 2001 21:06:12 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Will Perl report on variables no longer used??
Message-Id: <3B8AEE84.1F1BBA78@earthlink.net>

Abigail wrote:
> 
> Benjamin Goldberg (goldbb2@earthlink.net) wrote on MMCMXVIII September
> MCMXCIII in <URL:news:3B8A1425.4D5CC9D1@earthlink.net>:
> ~~ Abigail wrote:
> ~~ >
> ~~ > Furthermore, I quite often use lexical variables that appear only
> ~~ > once in the program - yet if the compiler would give a warning of
> ~~ > being used only once, it would be an annoyance as the variable is
> ~~ > needed.
> ~~ > There are after all no anonymous scalars.
> ~~ >
> ~~ >     sub TIESCALAR {
> ~~ >         my $proto = shift;
> ~~ >         my $class = ref $proto || $proto;
> ~~ >
> ~~ >         bless \(my $foo = shift) => $class;
> ~~ >     }
> ~~
> ~~ I think this is a special case -- it's used once, but a reference
> ~~ to it is returned to be used elsewhere.  Here's Craig Berry's
> ~~ example:
> ~~
> ~~   sub foo {
> ~~     my $bar;
> ~~     1;
> ~~   }
> ~~
> ~~ Neither $bar nor a reference to it is ever used -- yet perl gives no
> ~~ warning about it.
> 
> So what? Unlike package variables, it's hard to come up with examples
> where such unused variables can lead to a problem.
> 
> I'd be interested if you can find an algorithm that Perl should use
> to figure out any "unused variables" that doesn't lead to false
> positives and doesn't solve the halting problem either.
> 
> Abigail
> --
> perl -wle 'eval {die ["Just another Perl Hacker"]}; print ${${@}}[$#{@{${@}}}]'
> #    Ten woodpeckers above
> #    a rice field. A pair of eagles
> #    fly north. Ryokan.

-- 
I'm not a programmer but I play one on TV...


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

Date: Mon, 27 Aug 2001 21:23:04 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Will Perl report on variables no longer used??
Message-Id: <3B8AF278.3D689C06@earthlink.net>

Abigail wrote:
> 
> Benjamin Goldberg (goldbb2@earthlink.net) wrote on MMCMXVIII September
> MCMXCIII in <URL:news:3B8A1425.4D5CC9D1@earthlink.net>:
> ~~ Abigail wrote:
> ~~ >
> ~~ > Furthermore, I quite often use lexical variables that appear only
> ~~ > once in the program - yet if the compiler would give a warning of
> ~~ > being used only once, it would be an annoyance as the variable is
> ~~ > needed.
> ~~ > There are after all no anonymous scalars.
> ~~ >
> ~~ >     sub TIESCALAR {
> ~~ >         my $proto = shift;
> ~~ >         my $class = ref $proto || $proto;
> ~~ >
> ~~ >         bless \(my $foo = shift) => $class;
> ~~ >     }
> ~~
> ~~ I think this is a special case -- it's used once, but a reference
> ~~ to it is returned to be used elsewhere.  Here's Craig Berry's
> ~~ example:
> ~~
> ~~   sub foo {
> ~~     my $bar;
> ~~     1;
> ~~   }
> ~~
> ~~ Neither $bar nor a reference to it is ever used -- yet perl gives
> ~~ no warning about it.
> 
> So what? Unlike package variables, it's hard to come up with examples
> where such unused variables can lead to a problem.
> 
> I'd be interested if you can find an algorithm that Perl should use
> to figure out any "unused variables" that doesn't lead to false
> positives and doesn't solve the halting problem either.

Any lexical which is declared but never used within it's scope, and for
which the "my" is in a void context [ie, is not at the end of the block]

This might not catch everything you might want it to, but it doesn't
produce any false positives, and doesn't solve the halting problem.

-- 
I'm not a programmer but I play one on TV...


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

Date: 28 Aug 2001 01:33:19 GMT
From: Ilmari Karonen <iltzu@sci.invalid>
Subject: Re: Will Perl report on variables no longer used??
Message-Id: <998960983.10957@itz.pp.sci.fi>

In article <slrn9ol1lv.plv.abigail@alexandra.xs4all.nl>, Abigail wrote:
>Benjamin Goldberg (goldbb2@earthlink.net) wrote on MMCMXVIII September
>MCMXCIII in <URL:news:3B8A1425.4D5CC9D1@earthlink.net>:
>~~ 
>~~   sub foo {
>~~     my $bar;
>~~     1;
>~~   }
>~~ 
>~~ Neither $bar nor a reference to it is ever used -- yet perl gives no
>~~ warning about it.
>
>So what? Unlike package variables, it's hard to come up with examples
>where such unused variables can lead to a problem. 
>
>I'd be interested if you can find an algorithm that Perl should use
>to figure out any "unused variables" that doesn't lead to false
>positives and doesn't solve the halting problem either.

Well, the compiler certainly could be made to check for only-once-used
lexicals just as it currently checks for only-once-used globals.  That
approach, of course, does produce both false positives and negatives.
It does not, to my knowledge, solve the halting problem.

Whether such a check would be useful is a different matter.  I would
argue that it would be more trouble than it'd be worth, and the example
you gave earlier in this thread is a strong case in point.


As for literally interpreting your challenge, it would be impossible
even if you could solve the halting problem.  This is because, using
symrefs/eval and a suitable source of randomness, it is possible to
write a Perl script that uses non-deterministically named variables.

(Unless, of course, one argued that such a script would potentially use
all possible variables, and therefore could have no unused ones.  But if
one made the odds of a given variable being used sufficiently small, one
could then claim that any program had a higher probability of using any
variable due to random mutations introduced by cosmic rays.  Which would
either invalidate the argument, or make the existence of a truly unused
variable in any program impossible.)

-- 
Ilmari Karonen -- http://www.sci.fi/~iltzu/
"Get real!  This is a discussion group, not a helpdesk.  You post something,
we discuss its implications.  If the discussion happens to answer a question
you've asked, that's incidental."           -- nobull in comp.lang.perl.misc



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

Date: Tue, 28 Aug 2001 03:41:33 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: Will Perl report on variables no longer used??
Message-Id: <3b8b12e9.1b7d$2d9@news.op.net>

In article <slrn9ol1lv.plv.abigail@alexandra.xs4all.nl>,
Abigail <abigail@foad.org> wrote:
>I'd be interested if you can find an algorithm that Perl should use
>to figure out any "unused variables" that doesn't lead to false
>positives and doesn't solve the halting problem either.

I don't think you could, because:

        my $bar;
        if (foo()) {
          $bar = 1;
        } else {
          # 
        }
        exit;

You can't know whether foo() will return true unless you run it.
-- 
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


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

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


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