[12091] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5691 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon May 17 00:07:25 1999

Date: Sun, 16 May 99 21:00:18 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 16 May 1999     Volume: 8 Number: 5691

Today's topics:
        ``/system(); local vs remote behavior problem (Jerrad Pierce)
        DBD-CSV/SQL-Statement Problem <neurotic@DELETEME.home.com>
    Re: Duplicate Line - ignoring second line of text (Tad McClellan)
        dynamic loading problem Chen Lim
        Help translating data files <corlando@NOTpop.phnx.uswest.net>
        How can I read/write the volume label on my CDR (or dis (rogDH)
        Need two optimization tips tbsmith@deltacom.net
    Re: Newbie: HTTP_HOST / Redirect? (Jerrad Pierce)
    Re: Perl "constructors" <rra@stanford.edu>
    Re: Perl "constructors" <rra@stanford.edu>
    Re: Perl "constructors" <rra@stanford.edu>
    Re: Perl "constructors" <rra@stanford.edu>
    Re: Perl "constructors" <rra@stanford.edu>
    Re: Perl "constructors" <rra@stanford.edu>
    Re: Perl "constructors" <rra@stanford.edu>
    Re: Perl "constructors" (Larry Rosler)
    Re: Perl "constructors" (Larry Rosler)
    Re: Perl "constructors" (Larry Rosler)
    Re: perl script to check balanced (), {}, [], begin/end todd_b_smith@my-dejanews.com
    Re: perltootc - OO Tutorial for Class Data in Perl <metcher@spider.herston.uq.edu.au>
        The Sendmail Program <wassimk@iname.com>
    Re: TROLL ALERT (Re: Perl "constructors") <rra@stanford.edu>
    Re: TROLL ALERT (Re: Perl "constructors") <rra@stanford.edu>
        Turning on NumLock <erd@umr.edu>
        Special: Digest Administrivia (Last modified: 12 Dec 98 (Perl-Users-Digest Admin)

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

Date: Sun, 16 May 1999 05:40:34 GMT
From: belg4mit@mit.edu (Jerrad Pierce)
Subject: ``/system(); local vs remote behavior problem
Message-Id: <7hnvj8$l7l$1@beetle.hardlink.com>

Okay so I have a CGI script and I need to execute an external program.
I need the output so I should use ``, but the program doesn't seem to be 
executing it. The file permissions are okay. Isystem doesn't work either,
it returns status 256.        PS> Please (b)cc me with an replies, thanks!

                                                    /\         /\
                                               /\  /  \       /.:\/\
       I am the lorax, I speak for the trees. /  \ /  \    /\/.::/.:\/\
  o  ' I speak for the trees for the          /  \     \  /.:\ /\.::/.:\
 \|/   trees have no tongues.                /    \    \ /.:::/.:\ /.:::\
 / \                                           ||   || /:/.:/.::\\:\/.::\\
        http://www.mit.edu/~belg4mit/        http://www.blkmktbkz.com


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

Date: Mon, 17 May 1999 02:17:44 GMT
From: "Justin McMichael" <neurotic@DELETEME.home.com>
Subject: DBD-CSV/SQL-Statement Problem
Message-Id: <c%K%2.4974$5i7.2771@news.rdc1.va.home.com>

Sorry for the crosspost......
-----------------------------------------------

I am having a major problem.

Using Perl 5 on Intel Linux (Redhat 6).
Most recent Text-CSV_XS, SQL-Statement, DBI, DBD-CSV installed.

Attempting to port a CGI script from Win32.

Follows is an extremely simplified code snippet which gives me trouble.
-------------Begin-------------
#!/usr/bin/perl

use CGI;
use DBI;

$query = new CGI;

$dbh =
DBI->connect("DBI:CSV:f_dir=/home/jmcmicha/perl/apps/rhspbook/db");
$dbh->{'RaiseError'} = 1;
$@ = '';

$org = $query->param('org');

$sql = 'SELECT * FROM records WHERE (org like "' .$org. '")';
eval{$sqlH= $dbh->prepare($sql);$sqlH->execute();};
if ($@) {print $query->header;print "Database Error: " .
$dbh->errstr();exit;}

print $query->header;
print "$org<br>";

while ($row = $sqlH->fetchrow_hashref) {
print $row->{org} . "," .$row->{lname} . "<br>\n";
}

print "Printed results<br>";

exit;
---------------End--------------------

Any attempt to insert a the value of a variable extracted from the CGI
object results in a database error.

The error message from the Apache error log (Actually not output by
$dbh->errstr() for some reason):
DBD::CSV::Statement=HASH(0x81b6920) is not a valid SQL::Statement object
at /usr/lib/perl5/site_perl/5.005/DBD/File.pm line 173

If $sql is created without concatenating anything from CGI (ie..
$sql='SELECT * FROM records WHERE (org LIKE "BOB")';)  Then it works
without a hitch.

Interestingly enough.  If I just print $sql to STDOUT it looks just like
it should.

I've tried using CGI's function methods with the same results.

I've scoured the docs, spent time searching DejaNews and passing my
question around in IRC and I have made no headway.

I have no trouble whatsoever running this script under Win32.

I'm pretty desparate here.  Any help will be greatly appreciated.

Thanks,
Justin McMichael
neurotic@deleteme.home.com





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

Date: Sun, 16 May 1999 17:54:03 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Duplicate Line - ignoring second line of text
Message-Id: <rpenh7.3k6.ln@magna.metronet.com>

Robert Bruno (rbruno@mindspring.com) wrote:
: How do I have the write the code to ignore the second line of text since
: it is a duplicate of the first line? 

:                          ---- NUMBER OF PRIMARY CALLS -----
: TIME                OFFRD   HAND  OTHER   ABAND
: ---------------------------------------------------------------
:  10:30-10:59           205       190       3      10   95.0   5.0

: 10:30-10:59           205       190       3      10   95.0   5.0

: Please help.


   The answer to the FAQ below has parts that can be adapted to
   work with lines instead of arrays.

   Perl FAQ, part 4:

      "How can I extract just the unique elements of an array?"


   [ think _hash_ ]


--
    Tad McClellan                          SGML Consulting
    tadmc@metronet.com                     Perl programming
    Fort Worth, Texas


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

Date: Sun, 16 May 1999 12:50:09 GMT
From: Chen Lim
Subject: dynamic loading problem
Message-Id: <373ebd38.13327641@news.mindspring.com>

Hi guys :

While running DBI module, I got the following error messages:-
"Can't load module DBI, dynamic loading not available in this perl.
(You may need to rebuild a new perl which either supports dynamic
loading or has the DBI module statically linked into it."

I tried to rebuild my perl to configure dynamic loading. I got this
message when I run "sh Configure". "The bundled C compiler can not
produce shared libraries so you will not be able to use dynamic
loading."

Any suggestion on how I should this problem ? Is there a work around
to make dynamic loading work on my system ? And how do I statically
linked the module tomy script ?

Thanks for any response.

chen


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

Date: Sun, 16 May 1999 20:04:57 -0700
From: "rootdog" <corlando@NOTpop.phnx.uswest.net>
Subject: Help translating data files
Message-Id: <3GL%2.1008$T3.38647@news.uswest.net>

Given a data file created by a  C program ( file utility reports it as
'data' ). Is it possible to parse the file and and output it in  delimited
text  form without knowing the struct declarations which created the
records?

What is the minimum information I need to translate the file?
Is this even possible?

We are trying to translate data files for a conversion and the original
vendor who created the program is long gone

I have read through the camel book , perl func , several posts
etc. (pack,unpack etc )but I have not been able  to find an example where
the strucure of the records was not known before hand.

Thank you for your help before hand.








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

Date: Mon, 17 May 1999 01:57:58 GMT
From: rogdh@iname.com (rogDH)
Subject: How can I read/write the volume label on my CDR (or diskette drive) ?
Message-Id: <373f7786.6584738@news.earthlink.net>

tia



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

Date: Mon, 17 May 1999 02:46:27 GMT
From: tbsmith@deltacom.net
Subject: Need two optimization tips
Message-Id: <7hnvu3$kp6$1@nnrp1.deja.com>

I wrote a program that plays the game called "logic," found here ->

http://www.game-land.com/games/logic/

It uses random games (because I can't figure out any strategies).
My current code will follow at the end. I welcome any tips, but my two
questions are:

-Every 1000 games played, I want some eye candy so I can watch the
progress. The trouble is that for every game it plays, the script has to
increment a variable and check to see if it mod 1000 is 0. This slows it
down a lot. Is there a better way to do it?

-When I'm changing the script, I set $DEBUG to 1 so I can see the
game progress. When i'm done changing things, I don't want to delete all
these prints because I'll probably need them later. Is there some way I
can let the program not check for $DEBUG if it's not set? Like some
preprocessing stuff? I don't really want to make a big eval string.

The board is set in @grid, and as of this week everyone has already
found a win with eight moves, so I have it set to check for a win
after seven. And the board changes every week, so @grid and
the '7' in 'for (1..7) { flip play } ' should be changed after Friday
(or Sunday?).

----my code----

#!/usr/bin/perl -w

sub check_for_win();
sub flip(@);
sub make_grid();
sub one_time();
sub play();
sub show();
sub start();
$|++;
$DEBUG = 0;

start;

sub start() {
	one_time;
	make_grid;
	show if $DEBUG;
	while (1) {
		for (1..7) { flip play }
		check_for_win;
		if ((++$games) % 1000 == 0) {
			print "played $games games\n";
		}
		undef @plays;
		make_grid;
		show if $DEBUG;
	}
}

sub show() {
	for $y (1..5) {
		for $x (1..5) {
			print " $grid{$x,$y} ";
		}
		print "\n";
	}
	print "\n";
	@plays = ("Start") unless @plays;
	print join ' ', @plays;
	print "\n------------------------------\n\n";
}

sub play() {
	@random[(rand @random), (rand @random)]
}

sub check_for_win() {
	show if $DEBUG;
	if ( eval $eval_string ) {
		print join ' ', @plays, "\n";
		exit;
	}
}

sub flip(@) {
	push @plays, "($_[0],$_[1])";

	# middle
	$grid{$_[0],$_[1]}^=1;

	# top
	if ($_[1]-1) {
		$grid{$_[0],$_[1]-1}^=1;
	}

	# right
	if ($_[0]+1 != 6) {
		$grid{$_[0]+1,$_[1]}^=1;
	}

	# left
	if ($_[0]-1) {
		$grid{$_[0]-1,$_[1]}^=1;
	}

	# bottom
	if ($_[1]+1 != 6) {
		$grid{$_[0],$_[1]+1}^=1;
	}
}

sub make_grid() {
	$count = 0;
	for $y (1..5) {
		for $x (1..5) {
			$grid{$x,$y} = $grid[++$count];
		}
	}
}

sub one_time() {
	@random = 1..5;
	$games = $ARGV[0];
	@grid =
(undef,1,0,1,0,0,1,1,0,1,1,1,1,1,0,0,0,1,1,1,0,1,0,0,0,0);
	for $y (1..5) {
		for $x (1..5) {
			push @eval_string, '$grid{'.$x.','.$y.'}';
		}
	}
	$eval_string = join ' and ', @eval_string;
}

__END__

Thanks!

Todd Smith -japh
ITC^DeltaCom
tbsmith@deltacom.net


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


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

Date: Sun, 16 May 1999 06:07:41 GMT
From: belg4mit@mit.edu (Jerrad Pierce)
Subject: Re: Newbie: HTTP_HOST / Redirect?
Message-Id: <7ho16c$lle$1@beetle.hardlink.com>
Keywords: This should work, and is much impler.

It well send any host to host/index.html

#!/usr/bin/perl
print "Location: http://$ENV{'HTTP_HOST'}/index.html\n\n"; #could also use 
$ENV{'SERVER_NAME'}

                                                    /\         /\
                                               /\  /  \       /.:\/\
       I am the lorax, I speak for the trees. /  \ /  \    /\/.::/.:\/\
  o  ' I speak for the trees for the          /  \     \  /.:\ /\.::/.:\
 \|/   trees have no tongues.                /    \    \ /.:::/.:\ /.:::\
 / \                                           ||   || /:/.:/.::\\:\/.::\\
        http://www.mit.edu/~belg4mit/        http://www.blkmktbkz.com


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

Date: 16 May 1999 19:29:05 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: Perl "constructors"
Message-Id: <ylhfpczaxa.fsf@windlord.stanford.edu>

armchair <armchair@my-dejanews.com> writes:
> Russ Allbery <rra@stanford.edu> wrote:

>> In fact, Perl has a form of Hungarian notation built in, just enough to
>> not be as annoying and actually be somewhat useful.  Adding Hungarian
>> notation on *top* of that strikes me as redundant and ugly.

> If there was a separate notation for reference scalar, number scalar,
> and string scalar, it would be redundant, but their ain't, so it ain't.

You don't need to worry about this.  Really.  Honest.  Although the only
way you're probably ever going to convince yourself of this is write a
bunch of Perl code and discover that it doesn't matter, so me spending a
bunch of time trying to tell you that probably is pointless.

-- 
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
 00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print


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

Date: 16 May 1999 19:25:55 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: Perl "constructors"
Message-Id: <ylk8u8zb2k.fsf@windlord.stanford.edu>

armchair <armchair@my-dejanews.com> writes:
> Russ Allbery <rra@stanford.edu> wrote:

>> Er, why on earth would you write it that way in Perl?

>> 
>> sub first_line {
>>    my $filename = shift;
>>    open (FILE, $filename) or return undef;
>>    return <FILE>;
>> }
>> print first_line ('filename');

> You didn't follow my spec,

Yes, I intentionally didn't follow your spec because your spec was for a C
or C++ function, not for a Perl function.  The answer to your question is
"you wouldn't do it that way in Perl, normally."  I *could* write a Perl
function that did what you wanted, exactly, but it would be a lot longer
than it needed to be and wouldn't look or interact like a normal part of
Perl.

> which had a specific purpose, which was not: "how should I solve this
> particular problem?", but how would you check for a missing hash key
> without using exists().

No offense, but that's a nonsensical question.  It's like asking how to
allocate memory in C without calling malloc().  To check to see if a hash
key is there, you call exists().  That's what it's *for*.

> The filename is inside of a hash that I am passing down as a reference
> (the hash reference is the first parameter passed down.) And I want to
> know from the return code exactly what failed - bad value of a
> parameter, missing parameter, file not readable, file not found. You are
> only telling me something went wrong.

No, I'm giving you all that information too.  See $!.

> And the second parameter passed is a reference to an array where I want
> the first line placed, not returned via return().

Well, if you really want that, it's an easy mod.

sub first_line {
   my $filename = shift;
   my $array = shift;
   open (FILE, $filename) or return undef;
   push (@$array, scalar <FILE>);
   return 1;
}

> I don't want to grep any reason in $!, because the wording may change or
> be system dependent,

Don't, then.

        use Errno;
        if ($! == ENOENT) { warn "filename doesn't exist\n" }

> or, if you don't like that, because I want it done via a return code.

You can make any language a bear to program in by insisting on programming
conventions that don't fit that language.

> Let me change the spec to say that I want an error returned if stat()
> reveals that the file is less than 800 bytes.

That'd be a better reason to pass in the array by reference.  If I needed
that, I'd probably have the function return 0 on success and an error
message on failure, which is a bit reversed but works well if there's only
one type of success and a bunch of types of failure.

Then you can do tests like:

        my $status = first_line ('filename', \@array);
        if ($status ne '') { warn "first_line failed: $status\n" }

> And, as we have standardized here at our hypothetical programming shop
> that all functions which do not receive parameters that are expected,
> must return the value 10.

Let Perl figure out if what gets passed in isn't an array.  Don't test it
yourself.  The push will fail just fine if someone tries to pass it
something other than an array.  You'll find that it'll never happen.

Or if you *really* need that testing, do:

        sub first_line ($\@) { ... }

which will make sure, at compile time, that the second argument starts
with @ and is a literal array, and will give your sub a reference to it.

> How is returning undef superior to returning an integer value?

Because you get a three-valued notion of truth.  Both 0 and undef are
false, but undef is also !defined.  It's very valuable for distinguishing
between expected failure and exceptional failure.

-- 
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
 00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print


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

Date: 16 May 1999 19:30:49 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: Perl "constructors"
Message-Id: <ylemkgzaue.fsf@windlord.stanford.edu>

Randal L Schwartz <merlyn@stonehenge.com> writes:
>>>>>> "Russ" == Russ Allbery <rra@stanford.edu> writes:

> Russ> Perl is a garbage-collected language that handles memory
> Russ> allocation itself.

> Picking small nits here, it's not *currently* a GC language.  It's a
> reference-counting language (which means that memory can be orphaned).
> Someday, we may get mark-n-sweep style GC.

> I suppose if you define GC as the set of "transparent memory allocation"
> languages, yours would do.  I don't recall hearing it used that way
> though.

Hm.  Well, I'd have to go check some books to be sure, but I seem to
remember reference counting being presented as one *method* of doing
garbage collection.  (Mark and sweep being another common one.)  Garbage
collection, at least to me, doesn't imply anything about the method, just
the general principle of "the memory is freed when you're not using it any
more and the programming language figures out when you're not using it
automatically for you."

-- 
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
 00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print


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

Date: 16 May 1999 19:32:48 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: Perl "constructors"
Message-Id: <ylbtfkzar3.fsf@windlord.stanford.edu>

Ilya Zakharevich <ilya@math.ohio-state.edu> writes:
> Russ Allbery <rra@stanford.edu> writes:
>> Someone else wrote:

>>> If it's possible to implement sleep-and-retry with $^M, please tell me
>>> how. The real $^M documentation is nonexistent as far as I can
>>> tell. (perlvar refers to INSTALL, but there's nothing in there)

>> Ilya would probably be the expert on that, but I don't think there's a
>> way of doing that with that mechanism.

> Why?  What makes it impossible to sleep and retry?  I'm not sure that
> *re*allocation of $^M is allowed right now, but at least you may try it
> *once*.  And if it not allowed, anyone interested may allow it spending
> a couple of minutes.

Aha... it throws an exception.  Sorry, I wasn't reading the documentation
correctly.  Cool.

-- 
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
 00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print


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

Date: 16 May 1999 19:39:41 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: Perl "constructors"
Message-Id: <yl675szafm.fsf@windlord.stanford.edu>

armchair <armchair@my-dejanews.com> writes:

> An object in Perl can only contain one reference.

No, I'm sorry, this simply isn't true.

In Perl, an object *is* a reference.  It can *contain* anything you
please, including as many references as you want to put in it, provided
that it's a reference to an appropriate data structure.  I realize that
you're still very confused about this, but please stop repeating something
that's utterly incorrect until you understand what people are trying to
tell you.

> You were making a point that Perl has advantages over C++ by doing it
> this way, and I was pointing out that in C++ you can do it exactly like
> Perl.

No, you can't.  You cannot create a blessed array in C++, for example.
You can create an object with a bunch of overloaded operators that mostly,
but not entirely, acts like an array except for the stuff that you forgot,
but that's not the same thing.

> Create only one member variable, and have it be a pointer (ala Perl) and
> then have it point to a simple or complex data structure. Name the
> pointer "self", and then all the code that you look at in the member
> function referring to member data will look like self->xxx, just like
> Perl, only without the $.

This is nonsense.  Perl passes its object pointer to its methods in
precisely the same way that C++ does.  It just doesn't automagically put
it in a variable named "this" for you; you can do that yourself or not as
you choose.

-- 
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
 00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print


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

Date: 16 May 1999 19:34:01 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: Perl "constructors"
Message-Id: <yl90aozap2.fsf@windlord.stanford.edu>

chris <chris+usenet@netmonger.net> writes:

> Unfortunately, you just gave a pretty decent example of how easy it is
> to make certain mistakes in Perl.  That sub does not do what you think
> it does.  You're calling it in list context, so <> will read the whole
> file.

> And just to be nitpicky about context, "return;" is preferred over
> "return undef;" since in the event you _do_ call it in a list context,
> you usually want it to return the list equivalent of "false" rather than
> the one-element (undef).

Teaches me to write code off the top of my head without checking it
first.  :)  Normally I do just use return, but I was doing an explicit
return undef to make it clearer.  Feh.

-- 
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
 00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print


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

Date: 16 May 1999 19:50:13 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: Perl "constructors"
Message-Id: <yl3e0wz9y2.fsf@windlord.stanford.edu>

Alan Curry <pacman@defiant.cqc.com> writes:
> Russ Allbery  <rra@stanford.edu> wrote:

>> Um, you'll find that the *vast* majority of all programs immediately
>> exit on a memory allocation failure.  Probably 99% of them.  That's
>> usually what you want to do.  Long running processes that should keep
>> running in the face of any failure condition are the *very uncommon*
>> case.

> Would you use perl if it died every time a fork() failed with EAGAIN?
> Would you use perl if it died every time a bind() failed with EADDRINUSE?
> Would you use perl if it died every time an open() failed with ENFILE?

Yes, although they would annoy me to varying degrees.  I could care less
about the first one; I've never had a Perl program get an EAGAIN from
fork().  I apparently don't write programs that potentially have that
problem.  On the other hand, I write lots of code that could result in
EADDRINUSE and a decent amount that could get ENFILE, so that would bug me
more, although dying is often the right decision to make in those cases.

> Why then do you tolerate a language that dies whenever a brk() fails
> with ENOMEM?

Because I can count on one hand the number of times I've seen a program I
wrote fail by running out of memory under conditions that weren't just a
bug in my program, over the sixteen years that I've been programming.

> Where do you draw the line that makes it acceptable for a
> general-purpose interpreter to turn a temporary error into a fatal one?

Where it hardly ever happens.  I've had programs die because the machine
hardware failed more frequently than I've had them die from running out of
memory.

> Other parts of this thread claim that perl doesn't force any particular
> programming style.

All these sorts of claims are relative, and I'd think that would be
obvious.  Perl doesn't let you write programs that are compiled to machine
code and distributable independent of Perl's support library either (at
least if you're using modules).  Perl doesn't let you implement your own
malloc().  Perl is a pretty crappy language for writing device drivers,
things that have to go really fast, and anything that uses threads
(currently).

Perl supports a wide variety of programming styles.  It doesn't let you do
*everything*.  Handling memory failures in an interpreted language that's
doing its own allocation behind the scenes is pretty hard to do, and it's
rather impressive that $^M works as well as it does.  Perl isn't ever
going to be everything for everyone.  Just like some of the things
mentioned in the first paragraph may someday be fixed, such as with better
versions of the Perl compiler and thread support, the memory stuff may
someday be fixed.  In the meantime, it doesn't bother me.

-- 
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
 00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print


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

Date: Sun, 16 May 1999 20:34:23 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Perl "constructors"
Message-Id: <MPG.11a92e167b197983989a7d@nntp.hpl.hp.com>

In article <7hn64t$91c$1@schenectady.netmonger.net> on 16 May 1999 
19:26:21 GMT, chris+usenet@netmonger.net <chris+usenet@netmonger.net> 
says...
> In article <MPG.11a882953134d43c989a77@nntp.hpl.hp.com>,
> Larry Rosler <lr@hpl.hp.com> wrote:
 ...
> >   sub first_line {
> >       my ($hash_ref, $array_ref) = @_;
> >       my $filename = $hash_ref->{filename} or return 10; # FALSE param
> 
> Until you have a directory full of numbered files and you want to
> open "0".

And the directory just happens to be the current directory.  I am well 
aware of the definition of FALSE, and didn't think this was a place to 
be concerned about that particular nit.

> >       open FILE, $filename or return -$!;                # -errno
> >       -s FILE < 800 and return 42;                    # What you will.
> >       $array_ref->[0] = <FILE>;
> >       return 0;                                          # Success!!!
> >    }
> >    print $array[0] unless first_line('filename') != 0;
> 
> 'filename' is a hash reference?

No.  I just copied the old call without changing the arguments.

       print $array[0] unless first_line(\%hash, \@array) != 0;

Big F Deal.

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


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

Date: Sun, 16 May 1999 20:43:33 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Perl "constructors"
Message-Id: <MPG.11a9303b810aa47a989a7e@nntp.hpl.hp.com>

In article <7hn8eh$5ng$1@nnrp1.deja.com> on Sun, 16 May 1999 20:05:38 
GMT, armchair@my-dejanews.com <armchair@my-dejanews.com> says...
> In article <MPG.11a882953134d43c989a77@nntp.hpl.hp.com>,
>   lr@hpl.hp.com (Larry Rosler) wrote:
> >       my $filename = $hash_ref->{filename} or return 10; # FALSE param
> 
> Thanks Larry, the line above is what I was most looking for. How to
> determine something is not in a hash without using exists(). So it
> apears that any scalar that does not exist (including, surprisingly
> this one -> $a = undef)  returns undef, which evaluates to "" which
> is false.

I wouldn't put it quite that way.  When used by itself (in a Boolean 
test), undef is false.  That is what is happening in that snippet.

When used in a string context, undef is converted to "" which is false.  
When used in a numeric context, undef is converted to 0 which is false.  
In these two cases, the '-w' flag generates a warning.

Besides those three, the only other false value is the string '0', which 
readily interchanges with the number 0, as you know by now.

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


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

Date: Sun, 16 May 1999 20:55:53 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Perl "constructors"
Message-Id: <MPG.11a9332714615594989a7f@nntp.hpl.hp.com>

[Posted and a courtesy copy sent.]

In article <slrn7jud9q.3ab.sholden@pgrad.cs.usyd.edu.au> on 16 May 1999 
21:14:34 GMT, Sam Holden <sholden@pgrad.cs.usyd.edu.au> says...
> On Sun, 16 May 1999 19:28:53 GMT, armchair@my-dejanews.com wrote:
 ...
> >I personally prefer to return 0 on success and multiple non-zero codes
> >for various errors (ala the C and Unix libraries). Doing that I can not
> >write:
> >
> >my_func() || die "";
> >
> >But I could do:
> >
> >if ( $return_code = my_func() )  { fatal_error( $return_code ); }
> 
> or even...
> 
> $return_code = my_func() && die $return_code;

I'm afraid not, Sam.  Nothing gets assigned to $return_code if my_func() 
returns a true value, because of the precedences.  You should write:

  $return_code = my_func() and die $return_code;

Several people have been skating around that problem in this thread, 
using '||' instead of 'or'.  The ice finally cracked under you.  :-)

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


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

Date: Mon, 17 May 1999 03:43:18 GMT
From: todd_b_smith@my-dejanews.com
Subject: Re: perl script to check balanced (), {}, [], begin/end, etc
Message-Id: <7ho38m$mv4$1@nnrp1.deja.com>

In article <7hn7b7$oeb$1@sunburst.ccs.yorku.ca>,
  friendly@hotspur.psych.yorku.ca (Michael Friendly) wrote:
> Looking for a perl script to read a text file and check for any of a
> series of balanced strings, posibly nested, including character pairs
> like (), {}, [], and strings like begin/end, do/end, etc.
>
> --
> Michael Friendly     Email: friendly@yorku.ca (NeXTmail OK)
> Psychology Dept
> York University      Voice: 416 736-5115  Fax: 416 736-5814
> 4700 Keele Street    http://www.math.yorku.ca/SCS/friendly.html
> Toronto, ONT  M3J 1P3 CANADA
>

I wrote this about a month after I learned perl and never used it since.
So it might work. here ya go.

#!/usr/bin/perl

open (IN, "$ARGV[0]");
@in = <IN>;
close IN;
$x = 0;
$number = 0;
for (@in) {
next if /^\s*\#/;
        $number++;
        chomp;
        if (/\{/) {
                print "$_\n";
                $a = 1;
                push(@loops, $_);
        }
        if (/\}/) {
                print"$_\n" unless $a;
                pop(@loops);
        }
        $a = 0;
}

print "------\ncurlys left open:";
print map{"$_\n"}@loops;
print "\n----------\n";


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


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

Date: Mon, 17 May 1999 13:48:52 +1000
From: Jaime Metcher <metcher@spider.herston.uq.edu.au>
Subject: Re: perltootc - OO Tutorial for Class Data in Perl
Message-Id: <373F91A4.67FB181A@spider.herston.uq.edu.au>



"David L. Cassell" wrote:
> 
> Tom Christiansen wrote:
> >
> > [snip of ToC]
> >
> > I've gotten some feedback that this posting was too large to be easily
> > digested in a quick read, and that perhaps it should have been posted in
> > smaller segments, such as each piece in the outline above.  Would that
> > really have helped people read and understand these?  Wouldn't it have
> > seemed more like spam than like an ten-part mini-series? :-)
> 
> I liked it the way it was.  I would think most people would
> appreciate the ability to read it or skip it with one swift
> button-click.  OTOH, considering my eccentricities, the majority
> of readers are probably in the other camp.  :-)
> 

No, no - this *is* the "eccentricities" camp.  The other camp isn't reading this
ng.
I liked it the way it was, too.  It's not as if Tom posted twenty pages of
protocol analyzer dump.

One small suggestion. In "More Inheritance Concerns", after:

If you subclass Some_Class, now all you need to do to get separate,
per-class class attributes is to override the &_classobj
method in the derived class.

you could append:

Unless you also declare %ClassData as a lexical in the derived class's file
scope, it will be autovivified as a package variable in the derived class's
package.

Justification:
In the discussion of overriding _classobj to provide access to subclass state,
refugees from more patriarchal languages may be distracted by the fact that the
subclass's version of _classobj is accessing an *undeclared variable* <shock,
horror>.  The presence of a *declared* variable with (sort of) the same name
right there in front of my eyes momentarily caused the Pascal programmer in me
rise from it's grave and distract the Perl programmmer.  A phatic mention of
autovivification here might be kindly.  


-- 
Jaime Metcher


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

Date: Sun, 16 May 1999 22:51:43 -0500
From: "Wassim Metallaoui" <wassimk@iname.com>
Subject: The Sendmail Program
Message-Id: <373f9337.0@wznews.webzone.net>

Can anyone tell me what

open (MAIL, "|$mailprog -t")

does? I am wondering about the pipe and the -t


What I am wanting to do is send out an e-mail to everyone listed in a text
file.
I was told to do it using the sendmail program that way.. But I don't
understand it.
Who cares if it works if I don't know how it works! :-)


Also they said something about using $pid and $pid=fork();
what does that mean and how would I incorporate it into my mailing list
program.



Can you please e-mail wassimk@iname.com or post a reply.




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

Date: 16 May 1999 19:55:33 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: TROLL ALERT (Re: Perl "constructors")
Message-Id: <ylzp34xv4q.fsf@windlord.stanford.edu>

armchair <armchair@my-dejanews.com> writes:
> Russ Allbery <rra@stanford.edu> wrote:

>> It's logically a constructor, however, which is my point.  It
>> constructs an object.  The C++ distinction is one of language
>> semantics, not actual usage in a program.

> I am afraid you lose me on this. A constructor is a member function or
> functions that create an object of a certain class type. If object a is
> created in a member function Print() of object b, it just means that the
> Print() function is calling the b constructor, not that Print() is a
> constructor. Print() instantiates, but it doesn't construct anything,
> that is left for b's constructor function to do.

I really wish you'd try to think outside the box that you're bringing to
programming.  Your paragraph goes directly to what I was saying:  This
definition of constructor is a matter of *language semantics.*  Yes, I
agree, what you just said are the language semantics.

The caller *conceptually* doesn't have to care about any of that.  If it
calls something and gets an object back, as far as it's concerned, what it
called constructed an object for it.  It doesn't matter to the caller
whether this was done with some other function or not.

C++ has a legitimate need for the distinction because of things like
static constructors, allocating objects off the stack, and other such
things.  None of those special cases fit Perl in the same way, so a more
general definition of constructor is appropriate.

-- 
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
 00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print


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

Date: 16 May 1999 19:58:26 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: TROLL ALERT (Re: Perl "constructors")
Message-Id: <ylwvy8xuzx.fsf@windlord.stanford.edu>

armchair <armchair@my-dejanews.com> writes:

> Perl can have multiple "constructors" as long as they have different
> names.  Is that being practiced to any great extent?

Depends on how you define "great extent".  I know several modules that
provide multiple constructors, often to do things equivalent to what you
would do in C++ with copy constructors.  Also take a look at
IO::File::new() and IO::File::new_tmpfile(), which are two separate
constructors in Perl (and would probably be about five or six in C to
handle the wide variety of arguments that IO::File::new() can take).

-- 
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
 00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print


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

Date: Mon, 17 May 1999 03:52:18 +0000
From: Erich Douglass <erd@umr.edu>
Subject: Turning on NumLock
Message-Id: <373F9272.9FAA58F1@umr.edu>

How do you turn on/off numlock from a Perl script?  I've looked around
on CPAN for some sort of module that would make this easy, but with no
success.  Any comments or suggestions are welcome.  Please reply via
e-mail.

Erich Douglass


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

Date: 12 Dec 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Special: Digest Administrivia (Last modified: 12 Dec 98)
Message-Id: <null>


Administrivia:

Well, after 6 months, here's the answer to the quiz: what do we do about
comp.lang.perl.moderated. Answer: nothing. 

]From: Russ Allbery <rra@stanford.edu>
]Date: 21 Sep 1998 19:53:43 -0700
]Subject: comp.lang.perl.moderated available via e-mail
]
]It is possible to subscribe to comp.lang.perl.moderated as a mailing list.
]To do so, send mail to majordomo@eyrie.org with "subscribe clpm" in the
]body.  Majordomo will then send you instructions on how to confirm your
]subscription.  This is provided as a general service for those people who
]cannot receive the newsgroup for whatever reason or who just prefer to
]receive messages via e-mail.

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". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". 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". It appears twice
weekly in the group, but is not distributed in the digest.

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 V8 Issue 5691
**************************************

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