[comp.lang.perl] "MOST COMMON NOVICE BUG" contest

vsh@etnibsd.UUCP (Steve Harris) (03/07/90)

In article <15232@bfmny0.UU.NET> tneff@bfmny0.UU.NET (Tom Neff) writes:
>
>I still think PL12 creates a new 'most common novice bug' situation
>in the language, but at least it works as advertised.


So, what are the "TEN MOST COMMON NOVICE BUGS" in perl?  I nominate:

    Placing a comma after a filehandle in a print statement.

Anybody care to add more?  Novices, SPEAK UP!!
Experts, try to remember when you weren't!

print STDOUT "Just another Perl novice\n";
-- 
Steve Harris - Eaton Corp. - Beverly, MA - uunet!etnibsd!vsh

zwicky@sparkyfs.istc.sri.com (Elizabeth Zwicky) (03/09/90)

In article <1118@etnibsd.UUCP> vsh@etnibsd.UUCP (Steve Harris) writes:
>So, what are the "TEN MOST COMMON NOVICE BUGS" in perl?  I nominate:
>
>    Placing a comma after a filehandle in a print statement.

My personal favorite is using == instead of eq or != for ne. I *still*
do that all too often...

On the 10 most wanted features list, I have the common desire for
interesting output formatting of numbers with formats instead of
printf, and the not-so-common and perhaps unobtainable desire to be
able to use C libraries.

We have Sybase, see, and they provide all these fun
libraries that are the *only* way you can talk directly to the
database server. I find it intensely frustrating that it has this
perfectly good socket with a database server on it that I can't talk
to. I've assuaged my frustration some by writing a tiny
Sybase-server-server in C that I *can* talk to, but still...

Sybase also led me into one of the more twisted uses I've made of
Perl; a Perl program writes Perl programs that write
SQL programs that convert files to Sybase databases.

	Elizabeth Zwicky

cpcahil@virtech.uucp (Conor P. Cahill) (03/09/90)

In article <1118@etnibsd.UUCP> vsh@etnibsd.UUCP (Steve Harris) writes:
>So, what are the "TEN MOST COMMON NOVICE BUGS" in perl?  I nominate:
>
>    Placing a comma after a filehandle in a print statement.
>
>Anybody care to add more?  Novices, SPEAK UP!!

Every dang time I write, or modify, a script I always have
at least 1 syntax error where I didn't put the $ before a variable in 
an assignment.  Too much shell programming, I guess.

-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 

jh@efd.lth.se (Joergen Haegg) (03/09/90)

I'm trying to convince a friend to give up awk, and use perl instead.
He replies: "Give me 10-20 simple examples, like in the awk books!"

He just want to get started directly, and not read the manual.
(Always the same problem when you use something, and don't have the
time to learn something new.)

So, is there someone who has stumbled on this problem and have
made some simple examples? (I just don't want to reinvent something :-)
--
Joergen Haegg				jh@efd.lth.se	postmaster@efd.lth.se
System manager @ efd			046-107492
Lund Institute of Technology		Sweden

pem@frankland-river.aaii.oz.au (Paul E. Maisano) (03/09/90)

Everytime I write a perl script I invariably leave out the & from a
subroutine call. I have only been using perl for about two months
so I guess I just haven't got used to it yet.
 
It's not really a bug (refering back to the subject line) but neither is 
forgetting not to put a comma after a filehandle in a print statement. 

------------------
Paul E. Maisano
Australian Artificial Intelligence Institute
1 Grattan St. Carlton, Vic. 3053, Australia
Ph: +613 663-7922  Fax: +613 663-7937
Email: pem@aaii.oz.au   UUCP: {uunet,mcsun,ukc,nttlab}!munnari!aaii.oz.au!pem

schaefer@ogicse.ogi.edu (Barton E. Schaefer) (03/09/90)

In article <1990Mar9.013018.14672@virtech.uucp> cpcahil@virtech.UUCP (Conor P. Cahill) writes:
} In article <1118@etnibsd.UUCP> vsh@etnibsd.UUCP (Steve Harris) writes:
} >So, what are the "TEN MOST COMMON NOVICE BUGS" in perl?
} 
} Every dang time I write, or modify, a script I always have
} at least 1 syntax error where I didn't put the $ before a variable in 
} an assignment.  Too much shell programming, I guess.

In a similar vein,

	foreach variable_missing_dollar (@List) { ... }

I also find myself automatically typing one of "else if" or "elif"
instead of "elsif".  (Why in the world did you pick that particular
contraction, Larry?  Just because nobody else did?)
-- 
Bart Schaefer          "EARTH: Surrender IMMEDIATELY or we PICKLE DAN QUAYLE"

                                                                    "THPPFT!"
schaefer@cse.ogi.edu (used to be cse.ogc.edu)

jv@mh.nl (Johan Vromans) (03/10/90)

> Every dang time I write, or modify, a script I always have
> at least 1 syntax error where I didn't put the $ before a variable in 
> an assignment.  Too much shell programming, I guess.

I have shell complaining about $ before a variable in an assignment...

Anyway: my most common error is to forget the trailing semicolon.

Johan
--
Johan Vromans				       jv@mh.nl via internet backbones
Multihouse Automatisering bv		       uucp: ..!{uunet,hp4nl}!mh.nl!jv
Doesburgweg 7, 2803 PL Gouda, The Netherlands  phone/fax: +31 1820 62944/62500
------------------------ "Arms are made for hugging" -------------------------

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/10/90)

In article <7856@ogicse.ogi.edu> schaefer@ogicse.ogi.edu (Barton E. Schaefer) writes:
: I also find myself automatically typing one of "else if" or "elif"
: instead of "elsif".  (Why in the world did you pick that particular
: contraction, Larry?  Just because nobody else did?)

Obviously, so that I could say that Perl is derived from Ada.     :-)

"else if" implies dangling statements, which I abhor mildly.  "elif" doesn't
pronounce right, even if it does spell "file" backwards.  "elseif" is ugly.
What's left?

Larry

tneff@bfmny0.UU.NET (Tom Neff) (03/10/90)

In article <7366@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:
>"else if" implies dangling statements, which I abhor mildly.  "elif" doesn't
>pronounce right, even if it does spell "file" backwards.  "elseif" is ugly.
>What's left?

 #    #   ####   #    #
 #    #  #    #  #    #
 #    #  #    #  #    #
 # ## #  #    #  # ## #
 ##  ##  #    #  ##  ##
 #    #   ####   #    #

merlyn@iwarp.intel.com (Randal Schwartz) (03/10/90)

In article <1990Mar9.094346.13142@lth.se>, jh@efd (Joergen Haegg) writes:
| I'm trying to convince a friend to give up awk, and use perl instead.
| He replies: "Give me 10-20 simple examples, like in the awk books!"
| 
| He just want to get started directly, and not read the manual.
| (Always the same problem when you use something, and don't have the
| time to learn something new.)
| 
| So, is there someone who has stumbled on this problem and have
| made some simple examples? (I just don't want to reinvent something :-)

He could wait about two months and read a draft of the first chapter
of the Perl book.  Or, you can feed him all my JAPH one-liners.
Naaahhh!  Actually, the archived stuff from the perl-users mailing
list, which became comp.lang.perl, is probably pretty useful.  Or, if
anyone saved my postings to comp.unix.questions regarding common tasks
in Perl, that'd be a good start too.

There *are* quite a few good fragments in the 69-page manpage.  Read
that first.  I found that the stuff in 'eg' in the distribution, as
well as the stuff in 't' (yes, the tests!) were good to grab some of
the idioms.

for($i=129600;@x=gmtime($i),(print substr('Just another Perl hacker,',$x[5]*12+$x[4]-840,1)),($i+=2639520)<66117600;){;}
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

jgreely@oz.cis.ohio-state.edu (J Greely) (03/10/90)

In article <1990Mar9.094346.13142@lth.se> jh@efd.lth.se (Joergen Haegg) writes:
>I'm trying to convince a friend to give up awk, and use perl instead.
>He replies: "Give me 10-20 simple examples, like in the awk books!"

Well, you could pick up Tom's examples, sitting in the archive area on
tut.cis.ohio-state.edu (ftp) and osu-cis (uucp), in
{pub,~}/perl/scripts/tchrist (respectively).  I've been shoving lots
of stuff into the archive as I get time, although the READMEs are
hopelessly out of date.

Current contents are:
      2048 Oct 27 03:58 3.0/
       382 Sep 12 11:40 README
      2048 Oct 11 17:41 perl-users/
      2048 Feb  1 22:45 quickref/
      2048 Dec 14 12:54 quickref-old/
      2048 Mar  7 15:40 scripts/

3.0:
       182 Oct 18 21:46 README
      2048 Oct 18 22:26 kits@0/
      2048 Mar  2 12:38 patches/

3.0/kits@0:
     23992 Oct 18 22:24 perl.kit01.Z
     24366 Oct 18 22:24 perl.kit02.Z
     18709 Oct 18 22:24 perl.kit03.Z
     15616 Oct 18 22:24 perl.kit04.Z
     20021 Oct 18 22:24 perl.kit05.Z
     23689 Oct 18 22:24 perl.kit06.Z
     21132 Oct 18 22:24 perl.kit07.Z
     24396 Oct 18 22:24 perl.kit08.Z
     24095 Oct 18 22:24 perl.kit09.Z
     22389 Oct 18 22:24 perl.kit10.Z
     20275 Oct 18 22:24 perl.kit11.Z
     21902 Oct 18 22:24 perl.kit12.Z
     21629 Oct 18 22:25 perl.kit13.Z
     21005 Oct 18 22:25 perl.kit14.Z
     21577 Oct 18 22:25 perl.kit15.Z
     20172 Oct 18 22:25 perl.kit16.Z
     23276 Oct 18 22:25 perl.kit17.Z
     22373 Oct 18 22:25 perl.kit18.Z
     23082 Oct 18 22:25 perl.kit19.Z
     24470 Oct 18 22:25 perl.kit20.Z
     23766 Oct 18 22:26 perl.kit21.Z
     23945 Oct 18 22:26 perl.kit22.Z
     23518 Oct 18 22:26 perl.kit23.Z
     19890 Oct 18 22:26 perl.kit24.Z

3.0/patches:
     51073 Nov 13 16:03 patch1
     44820 Mar  2 12:37 patch10
     50212 Mar  2 12:37 patch11
     33677 Mar  2 12:38 patch12
     43571 Nov 13 16:03 patch2
     39341 Nov 13 16:03 patch3
     46510 Nov 13 16:03 patch4
     40587 Nov 17 21:04 patch5
     31786 Nov 17 21:04 patch6
     47521 Dec 22 14:24 patch7
     46758 Dec 22 14:24 patch8
     41635 Mar  2 12:36 patch9

perl-users:
    144687 Oct 11 17:41 1-158.Z

quickref:
     57487 Feb  1 22:44 perl-guide-a4.ps.Z
     57443 Feb  1 22:45 perl-guide-us.ps.Z
     56812 Feb  1 22:46 perl-refcard.ps.Z

scripts:
      7689 Feb 15 09:25 IPC.example
      8534 Oct 27 22:17 Install.shar
       839 Nov 18 00:23 README
      5281 Oct 27 22:17 cmail.shar
      1454 Oct 24 20:49 dvips42.patch
        56 Sep  9  1989 fixshar*
      9269 Mar  7 15:40 gsh.merlyn
      2967 Oct 27 22:17 mail2news
      9054 Feb 15 09:24 makelib.stuff
      6290 Mar  2 12:40 pstree
      3608 Nov  3 17:41 readabst
      7873 Mar  2 12:42 refetch.shar
      5422 Oct 27 22:17 shell-mode
      4323 Oct 27 22:17 ssl
      2048 Mar  2 12:40 tchrist/
      4884 Mar  2 12:42 termcap.pl
     30606 Sep  9  1989 up.old
     33613 Oct 24 15:32 up.shar
      1274 Nov 18 00:23 yagrip.pl

scripts/tchrist:
       203 Feb 21 17:43 MANIFEST
      1981 Feb 21 17:43 README
      1390 Feb 21 17:43 badrhosts
     10596 Feb 21 17:43 cfman
      6219 Feb 21 17:43 cfman.8l
       784 Feb 21 17:43 easy
       575 Feb 21 17:43 flush.pl
      1548 Feb 21 17:43 forwmail
       616 Feb 21 17:43 getalias
       658 Feb 21 17:43 getline
      5100 Mar  2 12:40 glob.shar
      2300 Feb 21 17:43 itimers.pl
      1037 Feb 21 17:43 nfinger
      1496 Feb 21 17:43 nlist
      6721 Feb 21 17:43 pstruct
      4597 Feb 21 17:43 pstruct.1l
       364 Feb 21 17:43 rename
       471 Feb 21 17:43 sortbyfield
       401 Feb 21 17:43 source.pl
      1458 Feb 21 17:43 syslog.pl
       911 Feb 21 17:43 today
       657 Feb 21 17:43 ttimer
      1448 Feb 21 17:43 uuhost
      4135 Feb 21 17:43 uuhostd
       548 Feb 21 17:43 who
       644 Feb 21 17:43 xpost


(quoted from osu-cis!~/GNU.how-to-get)

How to reach osu-cis via uucp
===============================
Here is a set of L.sys or Systems file lines suitable for osu-cis:

#
# Direct Trailblazer
#
osu-cis Any ACU 19200 1-614-292-5112 in:--in:--in: Uanon
#
# Direct V.32 (MNP 4)
#
osu-cis Any ACU 9600 1-614-292-1153 in:--in:--in: Uanon
#
# Micom port selector, at 1200, 2400, or 9600 bps.
# Replace ##'s below with 12, 24, or 96 (both speed and phone number).
# NOTE: 9600 bps Micom access may not yet be operational, or may be flaky.
#
osu-cis Any ACU ##00 1-614-292-31## "" \r\c Name? osu-cis nected \c GO \d\r\d\r\d\r in:--in:--in: Uanon

Modify as appropriate for your site, of course, to deal with your
local telephone system.  There are no limitations concerning the hours
of the day you may call.



                        "Okay, Max, we need something from
                         each of the four basic food groups:
                         something from the dough-and-frosting
                         group, something from the carbonation-
                         and-caramel-coloring group --"

                                    "How about the chewy-nougat-
                                     center-and-chocolaty-coating
                                     group?"

                        "Don't forget the orange marshmellow
                         peanuts!  I think those are a
                         vegetable."
--
J Greely (jgreely@cis.ohio-state.edu; osu-cis!jgreely)

tchrist@convex.COM (Tom Christiansen) (03/10/90)

In article <1990Mar9.094346.13142@lth.se> jh@efd.lth.se (Joergen Haegg) writes:
|I'm trying to convince a friend to give up awk, and use perl instead.
>He replies: "Give me 10-20 simple examples, like in the awk books!"
>
>He just want to get started directly, and not read the manual.
>(Always the same problem when you use something, and don't have the
>time to learn something new.)
>
>So, is there someone who has stumbled on this problem and have
>made some simple examples? (I just don't want to reinvent something :-)

Sure, you can look in the perl source directory under eg/.  I also have
an increasingly large set of pedagogical and practical examples, many
of which I used in the tutorial I gave at the last USENIX.  Since
Convex isn't currently allowing anonymous FTP in, jgreely@cis.ohio-state.edu 
has in a highly laudable act of altruism consented to keeping a
pub/perl/scripts/tchrist/ directory of these things on his machine.  I need
to either send him an update or else finagle some anon FTP on one of my
machines.  Here is a sorted list of the files under 100 lines kept there,
which should be kept there.  I will also mail off the complete set to anyone
who asks, using the tarmailchunky package to split it up into smaller,
btoa-encoded pieces.  The suite also has larger examples, like cfman and
pstruct, which even come with man pages.

      15 source.pl
      23 xpost
      26 getalias
      26 sortbyfield
      28 getline
      29 today
      34 easy
      36 flush.pl
      38 ttimer
      50 nfinger
      52 forwmail
      58 nlist
      61 badrhosts
      65 uuhost
      68 syslog.pl
      81 itimers.pl

There is a README file included telling what each of these demos.  I
hear from Randal that if you just try to blindly reply to my mail and
postings there may be problems -- he at least has them.  Consult my
signature for better reply addresses than news sticks in the headers.

--tom
--

    Tom Christiansen                       {uunet,uiucdcs,sun}!convex!tchrist 
    Convex Computer Corporation                            tchrist@convex.COM
		 "EMACS belongs in <sys/errno.h>: Editor too big!"

allbery@NCoast.ORG (Brandon S. Allbery) (03/11/90)

As quoted from <1118@etnibsd.UUCP> by vsh@etnibsd.UUCP (Steve Harris):
+---------------
| So, what are the "TEN MOST COMMON NOVICE BUGS" in perl?  I nominate:
| 
|     Placing a comma after a filehandle in a print statement.
| 
| Anybody care to add more?  Novices, SPEAK UP!!
| Experts, try to remember when you weren't!
+---------------

1) forgetting that "if (EXPR) BLOCK" *requires* the {}
2) not saving $0, ..., $9 across regexps, or $0 at the start of a program

Just another Perl diver,						;-)
++Brandon
-- 
Brandon S. Allbery (human), allbery@NCoast.ORG (Inet), BALLBERY (MCI Mail)
ALLBERY (Delphi), uunet!cwjcc.cwru.edu!ncoast!allbery (UUCP), B.ALLBERY (GEnie)
BrandonA (A-Online) ("...and a partridge in a pear tree!" ;-)

pvo@sapphire.OCE.ORST.EDU (Paul O'Neill) (03/11/90)

I keep thinking split() has a default delimiter. (' ')


Paul O'Neill                 pvo@oce.orst.edu
Coastal Imaging Lab
OSU--Oceanography
Corvallis, OR  97331         503-737-3251

tom@ssd.csd.harris.com (Tom Horsley) (03/12/90)

My most confusing bug as a novice user was believing that local() provided
some form of lexical scoping when what it really does is just push a new
definition for things with that name. I finally figured this out after
trying to debug something that looked like:

sub fred {
   local($barney);
   ...
}

...

   do fred($barney);

It took me a long time to figure out why my subroutine 'fred' was never able
to get to the value I thought I passed as the parameter...

Actually local() is documented pretty well in the man page, but subroutines
are described as using call by reference, but if it was really call by
reference, then I would be able to get to the value of the outermost
$barney, but I can't. It actually seems closer to call by name.

This was especially easy for me to do, because I generally try to use the
same name for "the same" variables in all my code.
--
=====================================================================
domain: tahorsley@ssd.csd.harris.com  USMail: Tom Horsley
  uucp: ...!novavax!hcx1!tahorsley            511 Kingbird Circle
      or  ...!uunet!hcx1!tahorsley            Delray Beach, FL  33444
======================== Aging: Just say no! ========================

pda@stiatl.UUCP (Paul D. Anderson) (03/13/90)

pvo@sapphire.OCE.ORST.EDU (Paul O'Neill) writes:

>I keep thinking split() has a default delimiter. (' ')

Me too!  And when I remember that I have to give it a delimiter,
I invariably use a literal instead of a regexp.

-paul
-- 
--
Paul Anderson * h:404-565-0761 w:404-841-4000
{mathcs.emory,gatech}.edu!stiatl!pda  ||  pda@SalesTech.Com

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/13/90)

In article <16731@orstcs.CS.ORST.EDU> pvo@sapphire.OCE.ORST.EDU (Paul O'Neill) writes:
: I keep thinking split() has a default delimiter. (' ')

It does if you leave the parens off.  It's not precisely ' ' though.  You
get split(/\s+/), the difference being that ' ' will trim leading whitespace
before splitting.

Chief Hairsplitter,
Larry

dan@kfw.COM (Dan Mick) (03/13/90)

In article <16731@orstcs.CS.ORST.EDU> pvo@sapphire.OCE.ORST.EDU (Paul O'Neill) writes:
>I keep thinking split() has a default delimiter. (' ')

Um...it does, right?  Whitespace ([ \t\n]+), not just ' ', but default and 
usually-useful, nonetheless...

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/13/90)

In article <9306@stiatl.UUCP> pda@stiatl.UUCP (Paul D. Anderson) writes:
: pvo@sapphire.OCE.ORST.EDU (Paul O'Neill) writes:
: 
: >I keep thinking split() has a default delimiter. (' ')
: 
: Me too!  And when I remember that I have to give it a delimiter,
: I invariably use a literal instead of a regexp.

Which should work just fine (at least until you use a regexp metacharacter).
Perl even recognizes that your "runtime" expression is invariant and turns
it into a compile-once expression.

Larry

composer@bucsf.bu.edu (Jeff Kellem) (03/14/90)

In article <TOM.90Mar12071911@hcx2.ssd.csd.harris.com> tom@ssd.csd.harris.com (Tom Horsley) writes:

 > Date: 12 Mar 90 12:19:11 GMT
 >
 > My most confusing bug as a novice user was believing that local() provided
 > some form of lexical scoping when what it really does is just push a new
 > definition for things with that name. I finally figured this out after
 > trying to debug something that looked like:
 >
 > sub fred {
 >    local($barney);
 >    ...
 > }
 >
 > ...
 >
 >    do fred($barney);
 >
 > It took me a long time to figure out why my subroutine 'fred' was never able
 > to get to the value I thought I passed as the parameter...

Well, not exactly.  You forgot to assign the @_ array (the args to the sub)
to $barney.

Probably what you wanted is something like...

	sub fred {
	   local($barney) = @_;
	   ...
	}

then...

	do fred($barney);

should do what you want.

Cheers...

				-jeff

Jeff Kellem
INTERNET: composer@cs.bu.edu  (or composer@bu.edu)
UUCP: ...!harvard!bu-cs!composer

inc@tc.fluke.COM (Gary Benson) (03/16/90)

In article <1118@etnibsd.UUCP> vsh@etnibsd.UUCP (Steve Harris) writes:

# So, what are the "TEN MOST COMMON NOVICE BUGS" in perl?  I nominate:
# 
#     Placing a comma after a filehandle in a print statement.
# 
# Anybody care to add more?  Novices, SPEAK UP!!
# Experts, try to remember when you weren't!
# 
# print STDOUT "Just another Perl novice\n";
# -- 
# Steve Harris - Eaton Corp. - Beverly, MA - uunet!etnibsd!vsh


Having just finished my first 500 lines of perl script qualifies me as a
novice, I'd say. I'm not sure what a novice bug is, but the two things I
do wrong most frequently are:

    1. Leaving off ; at the end of a statement;

       This is usually the first thing I look for on the line just before
       the one perl reports is responsible for stopping compilation.

    2. Losing track of my pairs of {  }

       This is always reported as an EOF problem, so I know right away what
       I did wrong. I used to always look at the bottom of the file (or the
       line reported), but now I know to look in the last place I was
       working.

I was the person who posted a list of novice questions a month or so ago,
and I have come up with a few "journeyman novice" questions:

    1. It seems that perl scripts are compiled just prior to execution.
       Would there be any benefit to a perl compiler that, like C, would
       JUST do the compilation and create a file (a.out might be a cool name
       or maybe p.out). This way, I could store the executable somewhere and
       not have to recompile each and every time. Why is perl NOT like that
       right now? I realize that compilation doesn't take long for my little
       100-liners, but for a SUPER MACHO HACKER program, it might be
       significant...

    2. I am studying C right now, too, and have just learned about
       multi-dimensional arrays. Are these possible in perl? Do you set them
       up similarly to the way C does it? If they are possible, does anyone
       have a (well-commented) example showing how to set up and access,
       e.g., a 3-D array? If they are not possible, why not? Is similar
       functionality provided some other way? I think they'd be really
       useful, espcially in database applications.

    3. I have not been able to sort the output of a perl script yet. I
       always have the script just create a "raw" file, then go out to unix
       to do the sort. Can someone show me the best way to do those kinds of
       things inside perl? What other unix-y things are easier, faster,
       prettier, handier, or more elegant done inside the script than
       outside?

Thanks to all for this wonderful newsgroup - I am saving upwards of 10
postings a day. Now I've got to write a perl script to extract the lines of
perl! Before I subscribed to this, I sent email to Larry asking for help.
Wow. His auto-mail-answerer informed me he's booked till the turn of the
century, so I am especially grateful that he is active in this group.


open (WOW, "# wowmon$$"); print WOW "Hack another just perl";
-- 
Gary Benson    -=[ S M I L E R ]=-   -_-_-_-inc@fluke.tc.com_-_-_-_-_-_-_-_-_-

There are very few personal problems which cannot be solved
by a suitable application of high explosives.    -Anonymous

tchrist@convex.COM (Tom Christiansen) (03/16/90)

In article <1990Mar16.010322.18464@tc.fluke.COM> inc@tc.fluke.COM (Gary Benson) writes:
>    1. It seems that perl scripts are compiled just prior to execution.
>       Would there be any benefit to a perl compiler that, like C, would
>       JUST do the compilation and create a file (a.out might be a cool name
>       or maybe p.out). This way, I could store the executable somewhere and
>       not have to recompile each and every time. 

Well, you can compile it, sort of, using the dump operator or the -u
flag.  I really love the speedup I get from a dumped perl script, but the
disk space of including all of perl in each one is almost always
prohibitive.  I would like perl to dump JUST the dataspace and enough
special text that says to re-invoke itself in a way indicating that it
should load its data from this file.

>
>    2. I am studying C right now, too, and have just learned about
>       multi-dimensional arrays. Are these possible in perl? Do you set them
>       up similarly to the way C does it? If they are possible, does anyone
>       have a (well-commented) example showing how to set up and access,

With the expression subscripting, we're getting closer to multi-dimmed
arrays.  It's still not very feasible though.  You can use the 
$foo{$a, $b, $c} emulation, but when you ask for its keys, you'll
only get back one set of them, not three.  Consider this:

    @a = ( "this is", "that was", "those aren't" );
    printf "a[1] is \"%s\"\n", $a[1];
    printf "a[1][1] is \"%s\"\n", $a[1][1];

that line doesn't work.

    printf "a[1][1] is \"%s\"\n", split(' ',$a[1])[1];

neither does that

    printf "a[1][1] is \"%s\"\n", (split(' ',$a[1]))[1];

but that one does.  It's pretty expensive an operation, and
I wouldn't like to generalize it for N dimensions.  You could
hide this in a subroutine calling &a($i,$j) to get at pieces.
Larry addressed the multi-dim'd array question in his message
<7125@jpl-devvax.JPL.NASA.GOV> addressed to this group on 21 Feb.

>    3. I have not been able to sort the output of a perl script yet. I
>       always have the script just create a "raw" file, then go out to unix
>       to do the sort. Can someone show me the best way to do those kinds of
>       things inside perl? 

Here's one way:

    while (<>) {
	# munge $_
	push(@output, $_);
    }
    print sort @output;

If you don't like the default sorting and can take a performance hit, 
do something like this:

    print sort myfun @output;

where myfun is a subroutine that has its own idea of collation.  Here's
one that sorts numerically highest to lowest according to the digits 
held in columns 20-24.  

    sub myfun { (substr($a,20,5) > substr($b,20,5)) ? 1 : -1; }

To sort by field $N, you can do something like this 

    print &sort_by_field($N, @output);

where &sort_by_field is this

    # somewhat warmed-over Randal code
    sub sort_by_field {
	local($fieldno, @whole) = @_;
	local(@x,@f,$a,$b);

	sub byfield { $f[$a] lt $f[$b] ? -1 : 1;  }

	for (@whole) {
	    @x = split(' ');
	    push(@f,$x[$fieldno]);
	}

	return @whole[sort byfield $[..$#whole];
    }

If you're storing your data in a hashed array %AZ, you could do something
like this:
	
    $, = "\n";
    print @A{sort keys %A};
or 
    ( $,  , $\ ) = ( "\t", "\n" );  # assumed for following examples, too
    for $key (sort keys %A) { 
	print $key, $A{$key};
    }


You could also reopen STDOUT to be a sort pipe and send your output 
there.  Here's an easy way:

    open (">-", "|sort");
    for $key (keys %ENV) {
	print $key, $ENV{$key};
    }

But you could even do the work yourself (not claiming this is
a realistic example, but it works):

    if ($kid = open (">-", "|-")) {
	die "can't pipe: $!" if $kid == -1;
	for $key (keys %ENV) {
	    print $key, $ENV{$key};
	}
    } else {
	print sort <STDIN>;
    }
    exit 0;

That should give you more than enough ways to sort.

--tom
--

    Tom Christiansen                       {uunet,uiucdcs,sun}!convex!tchrist 
    Convex Computer Corporation                            tchrist@convex.COM
		 "EMACS belongs in <sys/errno.h>: Editor too big!"

composer@bucsf.bu.edu (Jeff Kellem) (03/16/90)

In article <1990Mar16.010322.18464@tc.fluke.COM> inc@tc.fluke.COM (Gary Benson) writes:
 > Date: 16 Mar 90 01:03:22 GMT
 >
 >     1. It seems that perl scripts are compiled just prior to execution.
 >	  Would there be any benefit to a perl compiler that, like C, would
 >	  JUST do the compilation and create a file (a.out might be a cool name
 >	  or maybe p.out). This way, I could store the executable somewhere and
 >	  not have to recompile each and every time. Why is perl NOT like that
 >	  right now? I realize that compilation doesn't take long for my little
 >	  100-liners, but for a SUPER MACHO HACKER program, it might be
 >	  significant...

Actually, there is a way and it is documented in the manual.  Look under
the '-u' option to "cause perl to dump core after compiling your script."
You will need the "undump" program on your machine to turn this core dump
into an executable file.  This program is NOT supplied with perl, as it is
somewhat machine dependent.  ;-)

 >     2. I am studying C right now, too, and have just learned about
 >	  multi-dimensional arrays. Are these possible in perl? Do you set them
 >	  up similarly to the way C does it? If they are possible, does anyone
 >	  have a (well-commented) example showing how to set up and access,
 >	  e.g., a 3-D array? If they are not possible, why not? Is similar
 >	  functionality provided some other way? I think they'd be really
 >	  useful, espcially in database applications.

The best way to emulate multidimensional arrays is via associative arrays.
So, you could do something like:

	$foo{$a,$b}

to emulate 2-dimensional arrays.  Look at the documentation under '$;'.
Sorry I don't have an example on hand to give you.  If you still have
questions on this, just ask.

As for your other questions, I'll check back later today.  I have some
important work to complete right now.

Good luck!!

				-jeff

Jeff Kellem
INTERNET: composer@cs.bu.edu  (or composer@bu.edu)
UUCP: ...!harvard!bu-cs!composer

raymond@sunkist.berkeley.edu (Raymond Chen) (03/27/90)

My vote goes to converting

	while (condition) {
	    ....
	    next if condition;
	    ....
	}

into

	do {
	    ....
	    next if condition;
	    ....
	} while (condition);

According to The Great Larry (Book 3, Chapter 6, Verses 10 and 11.)

	(... Note also that the loop control commands described later will
	NOT work in this construct, since modifiers don't take loop
	labels.  Sorry.)

Not as sorry as we are for missing that parenthetical...

$_='x"Not ";"x\"another \";\'x\\"perl \\";x\\"hacker,\\"\'"';s/x/print/g;eval eval eval;
--
raymond@math.berkeley.edu     Maintainer of the csip Frequently Asked Questions