Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!apple!veritas!amdcad!sono!porky!mayer From: mayer@sono.uucp (Ronald &) Newsgroups: comp.lang.perl Subject: First impressions of new perl user (code, questions, ...) Message-ID: Date: 21 Mar 91 22:19:00 GMT Sender: mayer@sono.uucp (Ronald Mayer) Organization: Acuson; Mountain View, California Lines: 178 After recently hearing many good things about perl from people from my former school, I decided to try to set it up on my workstation at work. I copied the perl.4.0.beta version from osu-cis (I know, Since I'm a beginner at perl I shouldn't be using a beta version; but it existed as a single tar file, and as this was my first attempt at using uucp, I felt safer trying to copy just one file.) and installed it on my workstation [congrats on the very impressive Configure script]. I read the man pages, but don't have access to any other perl references. Since I just found out about perl last week I don't know if these questions have been discusses in this newsgroup unless they were discussed last week (I read the FAQ too, but don't remember seeing this there; I've heard of but never seen the Camel Book.). Here are a few questions [again, my apologies if these are stoopid. please flame me and point direct me to info where I can find these answers myself if these questions are unappropriate for this newsgroup]. ====================== A question (and a possible bug): I'm trying to make a perl script which will convert strings into case-insensitive regular expressions for sed [don't ask why :-(] for example: "$include (asdf)" gets converted into: "$[iI][nN][cC][lL][uU][dD][eE] ([aA][sS][dD][fF])" The only way I was able to do this in sed was to use one line for each character: /[aA]/s//[aA]/g ..... I was hoping this would be easier (or at least cleaner) to do in perl. What I was hoping would work is something like the following program; is ther a better way? --------------------------- >#!/u9/mayer/bin/sun4/perl >while (<>) { >s/([a-zA-Z])/do {$c = $1; > $1 =~ y\/A-Z\/a-z\/; > $c =~ y\/a-z\/A-Z\/; > "[".$1.$c."]";}/ge; >print; >} Unfortunatelly, this is what I get when I run it: >% perl case_insens #the script is called case_insens >asdf #I type this input >Bus error #My output. instead of [aA]... Here's some version/os info: >% perl -v > >This is perl, version 4.0 > >$Header: perly.c,v 4.0.beta 91/01/11 18:22:48 lwall Locked $ >Patch level: 0 > >Copyright (c) 1989, 1990, 1991, Larry Wall > >Perl may be copied only under the terms of the GNU General Public License, >a copy of which can be found with the Perl 4.0 distribution kit. >% more /etc/motd >SunOS Release 4.1 (STANDALONE_1.2) #1: Wed Jul 25 00:05:22 PDT 1990 If possible, novice users would prefer some way to avoid "Bus error" type messages (or at least man-page instructions hinting how to avoid them). ============================ A question (or request for a new feature): Is there any function which will show all my defined variables (and/or subroutins and/or open files). I'm not sure it's really useful, but I was thinking an interesting interactive perl application would be a calculator which would convert algebraic input into perl-commands and eval them. Is this practical? Trying to set this up I wanted a function to show all defined variables. ============================ A random idea (not really a request; just something I was thinking about): In the unpack function do "A" and "a" mean the same thing? If not, are the differences doccumented (not in my man page). If they do do the same thing, somehow I'd perfer if one of them only copied the string up to a null into the variable; because (with C-generated binary files) this is usually what is desired. I guess what I want is: ($text) = unpack('A10',$data); to be the equivalent of; ($text) = unpack('a10',$data); $text =~ s/\000.*//; or vice versa. ============================ perl examples source question: I notice that "The code examples contained [in the Camel book] are available via anonymous FTP from uunet.uu.net in nutshell/perl/perl.tar.Z for your retrieval." Are they also available through uucp? ============================ A dumb joke: From the README file, I quote "The author": >Just a personal note: I want you to know that I create nice things like this >because it pleases the Author of my story. If this bothers you, then your >notion of Authorship needs some revision. But you can use perl anyway. :-) Does this mean I can anonymous ftp the Camel book from somewhere? :-) ============================ My first useful perl program: [If anyone's interested; I'd welcome any comments about my perl coding.] This program is designed to search for a regexp like grep, but also to show context lines around the string containing the regexp. >#!/u9/mayer/bin/sun4/perl > >#based largely on "poor man's grep" from perl man pages. >#actually some form of getargs would be better here; but in the true >#spirit of perl (or at least awk and sed), this routine was a quick hack >#which served it's purpose. ># >#If grep has some option I don't know about which shows context lines >#I feel really stoopid. > >$pre_lines = 3; >$pst_lines = 3; > >$arg = shift; >$* = 1; >while ($lines[$i] = <>) { > if ($lines[$i] =~ /$arg/o) { > print "$ARGV:$.:\n"; > for ($j=($i+1)%$pre_lines; $j!=$i; $j=($j+1)%$pre_lines) { > print " $lines[$j]"; > } > print "***$lines[$i]"; > for ($j=0;($j<$pst_lines) && (!eof) && ($_ = <>);$j++) { > if (/$arg/o) { > print "***$_"; > $j=0; > } else { > print " $_"; > } > } > undef @lines; > } > $i = ($i + 1) % $pre_lines; >} Example: >% cgrep slurp perl.txt >perl.txt:21: > corresponds quite closely to C expression syntax. Unlike > most Unix utilities, perl does not arbitrarily limit the >*** size of your data--if you've got the memory, perl can slurp > in your whole file as a single string. Recursion is of > unlimited depth. And the hash tables used by associative > arrays grow as necessary to prevent degraded performance. >perl.txt:82: > find . -name '*.bak' -print0 | perl -n0e unlink > >*** The special value 00 will cause Perl to slurp files in > paragraph mode. The value 0777 will cause Perl to >*** slurp files whole since there is no legal character > with that value. ====================== Disclaimer: I've only had perl for a couple of days, and so far I'm very impressed. I've only read this group for about a week, so sorry if these questions have already been answered or if this posting is unappropriate for this group. My thoughts probably don't represent my company because I doubt if they know perl exists. Ron Mayer sun!sono!mayer mayer@sono.uucp