[13335] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 745 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Sep 8 23:07:19 1999

Date: Wed, 8 Sep 1999 20: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)

Perl-Users Digest           Wed, 8 Sep 1999     Volume: 9 Number: 745

Today's topics:
    Re: <== Getting the Database Column Names with DBD::ODB (Gyepi SAM)
    Re: Apeal for UN intervention - Stop East Timor  Genoci (Larry Rosler)
        browser queries-> perl daemon dhuang11@my-deja.com
    Re: converting a number into a binary? <tchrist@mox.perl.com>
    Re: converting a number into a binary? (Kragen Sitaker)
    Re: Is My computer busted? <uri@sysarch.com>
    Re: Is My computer busted? <tchrist@mox.perl.com>
    Re: Is My computer busted? (Bill Moseley)
    Re: Is My computer busted? <uri@sysarch.com>
    Re: Is My computer busted? (Bill Moseley)
    Re: make this a one-liner? (Eric Bohlman)
    Re: Need help with some pattern matching... (Bill Moseley)
    Re: Pattern Matching Question (elephant)
        perl objects and hashes of hashes <duane@blur.com>
    Re: Programmer's Editor (elephant)
    Re: script does not run but is printed on the screen?! (elephant)
    Re: Why, why, why, -w and use strict? <wyzelli@yahoo.com>
    Re: Why, why, why, -w and use strict? <tchrist@mox.perl.com>
    Re: Why, why, why, -w and use strict? (Kragen Sitaker)
    Re: Why, why, why, -w and use strict? <agray@infoscience.otago.ac.nz>
    Re: Y2K bugs on the Internet (Dik T. Winter)
        Digest Administrivia (Last modified: 1 Jul 99) (Perl-Users-Digest Admin)

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

Date: Wed, 8 Sep 1999 20:40:22 -0400
From: gyepi@magnetic.praxis-sw.com (Gyepi SAM)
Subject: Re: <== Getting the Database Column Names with DBD::ODBC ==>
Message-Id: <slrn7te0o1.irr.gyepi@magnetic.praxis-sw.com>

On 8 Sep 1999 19:57:01 GMT, Jeff Zucker <jeff@vpservices.com> wrote:
>Kevin Howe wrote:
>> 
>> I am trying to get the column names of a datasheet in my database using
>> DBD::ODBC.
>
>Try:
>
>    my $sth = $dbh->prepare("SELECT * FROM $table");


my $sth = $dbh->prepare("SELECT * FROM $table where 1=0");

Will execute faster.  
Much, much faster if your tables are large.

-- 
  Gyepi Sam  --+--  Designer/Programmer  --+--  Network/System Administrator   
  gyepi@praxis-sw.com         --+--          http://www.praxis-sw.com/gyepi

  I can resist anything but temptation.



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

Date: Wed, 8 Sep 1999 17:58:43 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Apeal for UN intervention - Stop East Timor  Genocide
Message-Id: <MPG.1240a61c5514de2e989f36@nntp.hpl.hp.com>

[Posted and a dis-courtesy copy mailed.]

In article <7r5vl4$s42$1@duke.telepac.pt> on 8 Sep 1999 15:33:56 GMT, 
survey1_pt@hotmail.com <survey1_pt@hotmail.com> says...
 ...
> PS:Thanks, and our apologies for the off topic subject.

Apologies NOT accepted!  This is totally irresponsible Usenet abuse!

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Thu, 09 Sep 1999 01:17:33 GMT
From: dhuang11@my-deja.com
Subject: browser queries-> perl daemon
Message-Id: <7r71r4$e0g$1@nnrp1.deja.com>

I have just finished up writing a simple web
server using Perl. However, there is a problem
with it. I can make it so that when I use
Netscape or IE to go to the URL for example:
http://www.test.com/food
My server will respond on port 80 and grabs a
page automatically. How do I read in the browser
query for in this example "food" and pass that
along to the perl daemon as a string. Any modules
take care of this problem? Thanks for any and all
responses.

Daniel Huang
Development Engineer - Redamo Ventures LLC


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.


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

Date: 8 Sep 1999 18:47:14 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: converting a number into a binary?
Message-Id: <37d70392@cs.colorado.edu>

     [courtesy cc of this posting mailed to cited author]

In comp.lang.perl.misc, 
    lr@hpl.hp.com (Larry Rosler) writes:
:But I now 
:see that the general way to convert an arbitrary 32-bit integer to 
:binary is this:
:
:      $bits = unpack 'B*', pack 'N', $n;

Well, yes, but I now tell you a more excellent way.

    $n = 181;
    printf "%b", $n;
10110101

It's got an inverse, as in 

    $n = 0b010110101;

or read from external data:

    $n = oct('0b010110101')

----------------
Version 5.005_55   
----------------

[  2546] By: jhi                                   on 1998/12/31  11:18:17
        Log: From: Wilson P. Snyder II
             To: perl5-porters@perl.org
             Subject: [PATCH v5.5.53] REV2: Binary number support
             Date: 1998/11/30
             Message-ID: <199811301543.KAA15689@vulcan.maker.com>
     Branch: cfgperl
           ! pod/perldata.pod pod/perldelta.pod pod/perlfunc.pod pp.c
           ! proto.h sv.c t/op/oct.t t/op/sprintf.t t/pragma/warn/util
           ! toke.c util.c


--tom
-- 
"Did you know that black paint is an excellent stain remover?"
                                       - Dogbert


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

Date: Thu, 09 Sep 1999 00:52:25 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: converting a number into a binary?
Message-Id: <dxDB3.12468$r5.814905@typ11.nn.bcandid.com>

In article <37D661F5.1293E218@ic.sunysb.edu>,
Z. Huang <zhuang@ic.sunysb.edu> wrote:
>   can anyone tell me how to convert a number (integer or float) into a
>binary? For, example:
>              8 ---> 100
>             0.125--->  0.001

pack and unpack works fine for integer.  Here's a solution I just
hacked up for float.  It took me 15 minutes; I had the following bugs:
- ternary operator's low precedence kept expressions from meaning what
  I'd hoped
- I forgot to tack the - sign on at the end of the process

It's probably a bit more expression-oriented and less imperative than
it should be; I must confess to doing some Scheme lately.

I'm using POSIX::floor because perldoc -f int said not to use int for
rounding.  (What the hell else do you use int for?)

Handling of leading and trailing zeroes could be improved.  8 produces
100.00000 by default; 0.125 produces .00100 by default.

#!/usr/bin/perl -w
use strict;
use POSIX qw(floor);

# convert a positive integer to binary
sub num2bin_int {
	my ($int) = @_;
	return ($int == 0) ? "" : (num2bin_int(floor($int/2)) . (
		($int % 2) ? "1" : "0"));
}

# fractional part of positive number
sub frac {
	my ($num) = @_;
	return $num - floor($num);
}

# convert a positive fraction to binary
sub num2bin_frac {
	my ($frac, $digits) = @_;
	return "" unless $digits > 0;
	return (($frac >= 0.5) ? "1" : "0") . 
		num2bin_frac(frac($frac * 2), $digits - 1);
}

# convert a number to binary
sub num2bin {
	my ($num, $digits) = @_;
	my $neg = ($num < 0);
	$num = abs $num;
	return ($neg ? "-" : "") . num2bin_int (floor($num)) . "." . 
		num2bin_frac(frac($num), $digits);
}

my $x = <>;
print num2bin ($x, $ARGV[0] || 5), "\n";

-- 
<kragen@pobox.com>       Kragen Sitaker     <http://www.pobox.com/~kragen/>
Wed Sep 08 1999
61 days until the Internet stock bubble bursts on Monday, 1999-11-08.
<URL:http://www.pobox.com/~kragen/bubble.html>


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

Date: 08 Sep 1999 20:48:18 -0400
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Is My computer busted?
Message-Id: <x73dwooqp9.fsf@home.sysarch.com>

>>>>> "BM" == Bill Moseley <moseley@best.com> writes:

  BM> Am I importing the wrong constants from Fcntl?  I'm on Solaris.
  BM> #! perl -w
  BM> use strict;

  BM> use Fcntl ':flock';

  BM> open FH, ">test.out" or die "dead opening file $!";

  BM> eval { flock( FH, LOCK_EX) or die "$!\n" } 
  BM>            or print "LOCK_EX reported '$@'\n";

no need for the eval. flock returns a boolean which is all you need.
only on systems which don't have flock do you need the eval to handle
that.

  BM> eval { flock( FH, LOCK_SH) or die "$!\n" } 
  BM>            or print "LOCK_SH reported '$@'\n";
  BM> print "Done.\n";

the first lock worked in exclusive mode so this share lock failed.

  BM> LOCK_SH reported 'Bad file number'

not sure why that error shows up.

uri


-- 
Uri Guttman  -----------------  SYStems ARCHitecture and Software Engineering
uri@sysarch.com  ---------------------------  Perl, Internet, UNIX Consulting
Have Perl, Will Travel  -----------------------------  http://www.sysarch.com
The Best Search Engine on the Net -------------  http://www.northernlight.com
"F**king Windows 98", said the general in South Park before shooting Bill.


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

Date: 8 Sep 1999 18:50:04 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Is My computer busted?
Message-Id: <37d7043c@cs.colorado.edu>

     [courtesy cc of this posting mailed to cited author]

In comp.lang.perl.misc, moseley@best.com (Bill Moseley) writes:
:Am I importing the wrong constants from Fcntl?  I'm on Solaris.

Good question.  Try this:

    perl -w -Mstrict -MFcntl=:flock -le 'print LOCK_NB'

--tom
-- 
Anyone can be taught to sculpt.
    Michaelangleo would have had to have been taught how not to.
	The same is true of great programmers.


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

Date: Wed, 8 Sep 1999 18:04:06 -0700
From: moseley@best.com (Bill Moseley)
Subject: Re: Is My computer busted?
Message-Id: <MPG.1240a765617a1538989724@nntp1.ba.best.com>

[This followup was posted to comp.lang.perl.misc and a copy was sent to 
the cited author.]

Tom Christiansen (tchrist@mox.perl.com) seems to say...
>      [courtesy cc of this posting mailed to cited author]
> 
> In comp.lang.perl.misc, moseley@best.com (Bill Moseley) writes:
> :Am I importing the wrong constants from Fcntl?  I'm on Solaris.
> 
> Good question.  Try this:
> 
>     perl -w -Mstrict -MFcntl=:flock -le 'print LOCK_NB'

34) ~/public_html/lii/docs %perl -w -Mstrict -MFcntl=:flock -le 'print 
LOCK_NB'
4
35) ~/public_html/lii/docs %perl -w -Mstrict -MFcntl=:flock -le 'print 
LOCK_SH'
1
36) ~/public_html/lii/docs %perl -w -Mstrict -MFcntl=:flock -le 'print 
LOCK_EX'
2

-- 
Bill Moseley mailto:moseley@best.com
pls note the one line sig, not counting this one.


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

Date: 08 Sep 1999 21:21:58 -0400
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Is My computer busted?
Message-Id: <x7wvu0nakp.fsf@home.sysarch.com>

>>>>> "BM" == Bill Moseley <moseley@best.com> writes:


  BM> Tom Christiansen (tchrist@mox.perl.com) seems to say...
  >> [courtesy cc of this posting mailed to cited author]
  >> 
  >> perl -w -Mstrict -MFcntl=:flock -le 'print LOCK_NB'

  BM> 34) ~/public_html/lii/docs %perl -w -Mstrict -MFcntl=:flock -le 'print 
  BM> LOCK_NB'

tom, why the use strict? no vars are used in the -e code.

uri

-- 
Uri Guttman  -----------------  SYStems ARCHitecture and Software Engineering
uri@sysarch.com  ---------------------------  Perl, Internet, UNIX Consulting
Have Perl, Will Travel  -----------------------------  http://www.sysarch.com
The Best Search Engine on the Net -------------  http://www.northernlight.com
"F**king Windows 98", said the general in South Park before shooting Bill.


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

Date: Wed, 8 Sep 1999 19:19:06 -0700
From: moseley@best.com (Bill Moseley)
Subject: Re: Is My computer busted?
Message-Id: <MPG.1240b8f8e0c47c13989725@nntp1.ba.best.com>

Uri Guttman (uri@sysarch.com) seems to say...
>   BM> eval { flock( FH, LOCK_EX) or die "$!\n" } 
>   BM>            or print "LOCK_EX reported '$@'\n";
> 
> no need for the eval. flock returns a boolean which is all you need.
> only on systems which don't have flock do you need the eval to handle
> that.

That was just for the posted demo program so I could try the different 
(and possibly die()ing) locks.

> 
>   BM> eval { flock( FH, LOCK_SH) or die "$!\n" } 
>   BM>            or print "LOCK_SH reported '$@'\n";
>   BM> print "Done.\n";
> 
> the first lock worked in exclusive mode so this share lock failed.

Is that correct?  You can change share modes -- how else could you go 
from a share to an exclusive and then release it again?


>   BM> LOCK_SH reported 'Bad file number'
> 
> not sure why that error shows up.

That is the question!

BTW -- NOT checking the return value of flock and the locking works as 
expected.

From perlfunc for flock

  Note that the emulation built with lockf(3) doesn't
  provide shared locks, and it requires that FILEHANDLE be
  open with write intent. These are the semantics that
  lockf(3) implements. Most if not all systems implement
  lockf(3) in terms of fcntl(2) locking, though, so the
  differing semantics shouldn't bite too many people.

Could this be applying here?  And if so, then do I always LOCK_EX?  
That's what I was trying to avoid.


-- 
Bill Moseley mailto:moseley@best.com
pls note the one line sig, not counting this one.


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

Date: 9 Sep 1999 00:52:58 GMT
From: ebohlman@netcom.com (Eric Bohlman)
Subject: Re: make this a one-liner?
Message-Id: <7r70da$44l@dfw-ixnews9.ix.netcom.com>

Brandon Metcalf (bmetcalf@nortelnetworks.com) wrote:
: Can anyone come up with a short one-liner that can be passed to perl -e
: that will do the same thing as the code below?  This may not even be the
: most efficient way to accomplish what I need.  I'm calling this script
: from a ksh script, but I would rather not have to rely on the extra
: script.

I don't really see how you'd gain anything by making it a one-liner: the 
extra time to open a file and read the script from it rather than reading 
it from the command line is likely to be negligible.

: This code basically parses the output of a recursive make and looks for
: /Error code.*ignored/ in between the 'building all in ...' and the
: 'Leaving ...' output.

If "recursive" means that it's possible for one "building...leaving" 
sequence to be nested inside another, then your code won't handle them
properly; when it hits the "building" line for a subsequence the directory
and contents will overwrite the ones from the enclosing sequence, keeping
its ending from being matched. You really need to keep a stack of the
directory names: 

my (@lines,@dirs);
while (<LOG>) {
  if (/building all in (.*)$/) {
    push @dirs,$1;
    push @lines,'';
  }
  elsif (/Leaving directory/) {
    print "$dirs[-1]\n" if $lines[-1] =~ /Error code.*ignored/;
    pop @lines;
    pop @dirs;
  }
  else {
    $lines[-1].=$_ if @dirs;
  }
}

Or you could use recursion:

while (<LOG>) {
  process($1) if /building all in (.*)$/;
}

sub process {
  my $dir=shift;
  my $line='';
  while (<LOG>) {
    if (/building all in (.*)$/) {
      process($1);
    }
    elsif (/Leaving directory/) {
      print "$dir\n" if $lines =~ /Error code.*ignored/;
      return;
    }
    else {
      $lines.=$_;
    }
  }
}



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

Date: Wed, 8 Sep 1999 17:31:26 -0700
From: moseley@best.com (Bill Moseley)
Subject: Re: Need help with some pattern matching...
Message-Id: <MPG.12409fb8416ac184989723@nntp1.ba.best.com>

Kragen Sitaker (kragen@dnaco.net) seems to say...
> >AR,54,03-514-682-692,514682692,ADS,Argentina,Cordoba
> >(INTERNET/SECURE/PPP/SLIP/V.34)

Bill's:
perl -pe "s/^([^(]+).*$/$1/" source > output

> s/\(.*// would do the trick on the line you posted.  Bill Moseley's
> regex didn't work because he didn't \ the (.

Mine probably didn't work because I tested it on a Windows machine and 
didn't use single quotes.  I didn't want to backslash because I was 
capturing.

But I like yours better.

-- 
Bill Moseley mailto:moseley@best.com
pls note the one line sig, not counting this one.


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

Date: Thu, 9 Sep 1999 10:53:47 +1000
From: elephant@squirrelgroup.com (elephant)
Subject: Re: Pattern Matching Question
Message-Id: <MPG.1241b021685734e2989cba@news-server>

David Alan Black writes ..
>elephant@squirrelgroup.com (elephant) writes:
>
>>stuart.mcintosh@db.com writes ..
>>>I'm trying to come up with something that will match all of the
>>>following when they come up in the middle of a line of text (I use 'POS'
>>>to set the search start position to the correct starting place first)
>>>
>>>123.45678  P
>>>  3.4567890P
>>> 23.456789
>>>
>>>I tried m/\S+\s*\S/g - according to the "programming perl" book the \s*
>>>should match zero or more occurances of a whitespace character but I
>>>found it was failing on the second example above...
>
>>it fails because on the second one the first \S+ matches everything .. 
>>then the \s* successfully matches a zero length string .. but then the 
>>regex wants a second \S .. but it can't find it because the whole string 
>>was matched by the \S+
>
>
>Except.... it actually succeeds, thanks to backtracking:

ha haa .. serves me right for trusting a poster to this group *8^)

  /me wipes egg from his face

actually I think that the originator said that the strings existed in 
the middle of other strings .. must have something to do with that

-- 
 jason - elephant@squirrelgroup.com -


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

Date: Wed, 08 Sep 1999 18:47:49 -0700
From: duane powell <duane@blur.com>
Subject: perl objects and hashes of hashes
Message-Id: <37D711C5.D669826A@blur.com>

My question is about hashes of hashes and perl objects. It's clear to me
how to create a self extending object with a hash reference. But how do
I define a hash of hashes within a class?

Thanks,
Duane

package Tree;
sub new {
 my($class,$name) = @_;
 my $class = ref($class) || $class;

 my $self    = {};
 $self->{NAME}   = $name;

 $self->{_BRANCHES}  = {};

 bless($self, $class);
 return $self;
}

sub growBranch {
 my($self,$branch) = @_;
 $self->{_BRANCHES}->{$branch}++;
}

sub draw {
 my($self) = shift;
 print "$self->{NAME}\n";
 foreach $b ( sort ( keys %{ $self->{_BRANCHES} } ) ) {
  print "\t", $b, "\t", $self->{_BRANCHES}->{$b}, "\n";
 }
}


package main;
my $t = Tree->new("myTree");

$t->growBranch("branchName");
$t->growBranch("someOtherBranchName");
$t->growBranch("aBranch");
$t->growBranch("branchName");
$t->draw();





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

Date: Thu, 9 Sep 1999 11:06:10 +1000
From: elephant@squirrelgroup.com (elephant)
Subject: Re: Programmer's Editor
Message-Id: <MPG.1241b3094e9820c6989cbb@news-server>

Randal L. Schwartz writes ..
>>>>>> "Greg" == Greg Bacon <gbacon@itsc.uah.edu> writes:
>
>Greg> Perhaps, but "incomplete" is a subset of the union of "will bite you in
>Greg> the ass at the most inopportune time" and "will cause great confusion".
>
>Yes, and that's my point.

my point is that the syntax highlighting included with the vim 
distribution understands enough perl constructs to be useful .. if I was 
doing something complicated then vim would be the last thing I'd rely on 
to tell me whether I was parsing it correctly or not .. more than once 
I've turned the syntax highlighting off (although not yet in Perl *8^)

I agree that those who claim that any editor (other than perl.exe *8^) 
properly parses all Perl code are having themselves on .. I'm sure that 
in a couple of years when we're all working on today's supercomputers 
we'll have enough processor power to run a real-time Perl parser in our 
editors just to do colour coding .. but until then - 99% of the time I'm 
happy with vim's colour coding (and - yes - I realise that's more a 
comment on what I do in Perl than on how good vim's colour coding is)

>On rare occasion, I must change the code I write so that it doesn't
>confuse cperl-mode, so that cperl-mode doesn't continue to confuse me.

can't you just switch the colour coding off ?

-- 
 jason - elephant@squirrelgroup.com -


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

Date: Thu, 9 Sep 1999 11:07:41 +1000
From: elephant@squirrelgroup.com (elephant)
Subject: Re: script does not run but is printed on the screen?!
Message-Id: <MPG.1241b3669c3f9f50989cbc@news-server>

lisaseeman@my-deja.com writes ..
>I was running some nice scripts of my virtual host (NT), when they
>“improved” their service. Now my scripts do not run, they are either
>downloaded or they are printed on the screen. Does anyone know what is
>going on.

yes .. your host stuffed up the script mapping

>          (needless to say my host doesn’t answer support request.)

get a new host

-- 
 jason - elephant@squirrelgroup.com -


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

Date: Thu, 9 Sep 1999 09:35:21 +0930
From: "Wyzelli" <wyzelli@yahoo.com>
Subject: Re: Why, why, why, -w and use strict?
Message-Id: <5RCB3.10$BT5.2060@vic.nntp.telstra.net>

Bart Simpson <phony@nospam.com> wrote in message
news:7r6rui$pg0$0@216.39.133.108...
> I learned perl mostly from a book "PERL and CGI for the WWW" by Elizabeth
> Castro, and it only recomends using taint checking and only for some
> scripts.
>
> Why is it a big deal to be forced to predeclare any variables?  If not
> declaring them saves a line of code, doesn't that help?
>
> The faq tells me -w turns on warnings to help with debugging.
>
> Once your script was working, wouldn't removing them speed things up a
bit?
> Or why even use them unless the script isn't working to begin with.
>

I'd be a little careful about the book from Elizabeth, she is one of those
who recommends installing your Perl.exe in the cgi-bin directory.

Wyzelli




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

Date: 8 Sep 1999 18:36:20 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Why, why, why, -w and use strict?
Message-Id: <37d70104@cs.colorado.edu>

In comp.lang.perl.misc, "Bart Simpson" <phony@nospam.com> writes:
:I learned perl mostly from a book "PERL and CGI for the WWW" by Elizabeth
:Castro, and it only recomends using taint checking and only for some
:scripts.  Why is it a big deal to be forced to predeclare any variables?
:If not declaring them saves a line of code, doesn't that help?  The faq
:tells me -w turns on warnings to help with debugging.  Once your script
:was working, wouldn't removing them speed things up a bit?  Or why even
:use them unless the script isn't working to begin with.

You'll get a real answer when I see a real address.

--tom
-- 
Today I dialed a wrong number...The other person said, "Hello?" and I said,
"Hello, could I speak to Joey?"...
They said, "Uh...I don't think so...he's only 2 months old."
I said, "I'll wait."  -- Steven Wright


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

Date: Thu, 09 Sep 1999 01:05:37 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: Why, why, why, -w and use strict?
Message-Id: <BJDB3.12481$r5.818064@typ11.nn.bcandid.com>

In article <7r6rui$pg0$0@216.39.133.108>,
Bart Simpson <phony@nospam.com> wrote:
>I learned perl mostly from a book "PERL and CGI for the WWW" by Elizabeth
>Castro, and it only recomends using taint checking and only for some
>scripts.
>
>Why is it a big deal to be forced to predeclare any variables?  If not
>declaring them saves a line of code, doesn't that help?

Shorter scripts are not necessarily better.  All other things being
equal, shorter scripts are better, because they're easier to read; but
in some cases, the benefits of doing something exceed the drawbacks of
making your script longer.

Several things are better about use strict 'vars':
- 'my' variables don't get dynamic scoping.  This is a Good Thing.
  Your scripts will be much easier to debug if you don't have variables
  with dynamic scoping in them.
- If you misspell a variable name when you're trying to read it, you
  will end up reading an undefined value.  If you misspell it when you're
  trying to write it, you will end up writing someplace that never gets
  read.  Either way you have introduced a bug into your script.  use strict 
  'vars' keeps you from doing this by accident.
- If you accidentally try to read or write a variable outside the part
  of the script where it's used, use strict 'vars' will often catch this
  -- the lexical scope of the variable will not extend that far.  This
  isn't much of an advantage in my experience; nine times out of ten I
  end up moving the variable declaration so its scope is bigger.

> The faq tells me -w turns on warnings to help with debugging.
>
> Once your script was working, wouldn't removing them speed things up a bit?
> Or why even use them unless the script isn't working to begin with.

Well, if you are confident that your script is completely bug-free,
then removing -w might be harmless.  (I don't know whether it speeds
things up or not.)

If you are confident that your script is completely bug-free, one of
the following must be true:
- it's a very short script.
- you've really spent a lot of time checking it and having other people
  check it -- a day or so per five lines.
- you're a much, much better programmer than I am, for whom mistakes
  are so rare that you don't need the safeguards of -w.
- you're an inexperienced programmer who hasn't yet learned how many
  mistakes he really makes :)

I suspect the last.  Me, I take all the help I can get for finding
errors in my programs.  -w has saved my butt more than once.

Oh, by the way, if you post as 'phony@nospam.com', people will think
you're a total lame loser.

Kragen
-- 
<kragen@pobox.com>       Kragen Sitaker     <http://www.pobox.com/~kragen/>
Wed Sep 08 1999
61 days until the Internet stock bubble bursts on Monday, 1999-11-08.
<URL:http://www.pobox.com/~kragen/bubble.html>


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

Date: 09 Sep 1999 14:43:28 +1200
From: Andrew Gray <agray@infoscience.otago.ac.nz>
Subject: Re: Why, why, why, -w and use strict?
Message-Id: <upuzsiz3j.fsf@infoscience.otago.ac.nz>

> Bart Simpson <phony@nospam.com> wrote in message
> news:7r6rui$pg0$0@216.39.133.108...
> > I learned perl mostly from a book "PERL and CGI for the WWW" by Elizabeth
> > Castro, and it only recomends using taint checking and only for some
> > scripts.

Does she really use all caps for `PERL'?  <sigh>  At least she
recommends taint checking I guess.

> > Why is it a big deal to be forced to predeclare any variables?  If not
> > declaring them saves a line of code, doesn't that help?

It doesn't always save a line of code since you can declare and define
them together on one line.  The idea isn't necessarily to write the
shortest possible script, but generally to write the most efficient
and maintainable one.  By scoping your variables you prevent many
common errors.

You may know which variables you are using in your script (perhaps),
but what if you start including modules (including your own)?  What if
you assume that a variable is undefined (zero), not realising that it
was used globally and already contains a non-zero value?

use strict also provides many other benefits.  Sometimes you don't
want all of the restrictions.  In these cases specify which stricts
you do want (refs, vars, subs).  

> > The faq tells me -w turns on warnings to help with debugging.
> > Once your script was working, wouldn't removing them speed things up a
> > bit?

Then everytime you change your script you would need to run it again
with -w to check for compile-time warnings.  Even allowing for that,
what about run-time warnings?  See perldoc perldiag for some examples.

> > Or why even use them unless the script isn't working to begin with.

How do you know that your script isn't working?  Many bugs are
difficult to spot in the generated output from complex programs.

Cheers,
Andrew




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

Date: Thu, 9 Sep 1999 02:07:08 GMT
From: dik@cwi.nl (Dik T. Winter)
Subject: Re: Y2K bugs on the Internet
Message-Id: <FHrsJx.B9J@cwi.nl>

In article <slrn7tce3l.mea.M.Ray@carlova.ulcc.ac.uk> M.Ray@ulcc.ac.uk (Malcolm Ray) writes:
 > On Tue, 07 Sep 1999 12:40:22 GMT, finsol@ts.co.nz <finsol@ts.co.nz> wrote:
 > >Is it any surprise that there is such a lot of Y2K bugged code about
 > >when many prominent programmers do very little in informing their
 > >programming community on this subject and in some cases deny there is
 > >indeed any problem?
 > 
 > You're so right!

More to it.  In (at least) one of his examples he notes that the usage of
a perl variable ($centure=19) works correctly for the year but that other
uses of it are suspect.  If he had obtained a mediocre knowledge of perl
he would know that the other uses are not suspect.  But my knowledge is
also not more than mediocre.

One more point.  Another example he shows displays indeed insufficient
knowledge of the programmer.  What he does *not* note is that the program
has an off-by-one error!  So the Y2K bug in that program is moot.
-- 
dik t. winter, cwi, kruislaan 413, 1098 sj  amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn  amsterdam, nederland; http://www.cwi.nl/~dik/


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

Date: 1 Jul 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 1 Jul 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.  

To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

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

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq" from
almanac@ruby.oce.orst.edu. 

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


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