[comp.lang.perl] First impressions of new perl user

mayer@sono.uucp (Ronald &) (03/22/91)

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

rbj@uunet.UU.NET (Root Boy Jim) (03/23/91)

In article <MAYER.91Mar21141900@porky.sono.uucp> mayer@sono.uucp (Ronald &) writes:
? 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 .....

If I wanted to do case insensitive matching, I would first
translate both my pattern and string into lower case, then
do regular matching. Sed can do this. Use y/A-Z/a-z/.
Actually I think you have to spell out the whole string.

?============================
?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.

There is a way to get your defined variables. Look at dumpvar.pl.
As for opened files, keep track of them yourself.

?============================
?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?

Obviously not.

?If not, are the differences documented (not in my man page).

To be honest, I couldn't quote you the exact differences,
chapter and verse. I don't care. I will figure it out
when I need to, and you should do the same. Hey kids, it's
time to play, "Ask the Computer".

?============================
?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?

Yes. More precisely, they live in ~ftp/nutshell/perl/perl.tar.Z.
This is true whether anonymous or not, FTP or UUCP. We do support
anonymous UUCP, but it costs you 40 cents per minute.

Please do your FTP's during *DAYTIME* hours when our load is low.
-- 
		[rbj@uunet 1] stty sane
		unknown mode: sane

bush@ecs.ox.ac.uk (Mark Bush) (03/26/91)

In article <126310@uunet.UU.NET> rbj@uunet.UU.NET (Root Boy Jim) writes:
>In article <MAYER.91Mar21141900@porky.sono.uucp> mayer@sono.uucp (Ronald &) writes:
>?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?
>
>Obviously not.
>
>?If not, are the differences documented (not in my man page).

But the man page *does* document the difference (and a very useful
difference it is, too!

>To be honest, I couldn't quote you the exact differences,
>chapter and verse.

From the man page:

"    pack(TEMPLATE,LIST)
             Takes an array or list of values and packs it into a
             binary  structure,  returning  the string containing
             the structure.  The TEMPLATE is a sequence of  char-
             acters  that  give  the order and type of values, as
             follows:

                  A    An ascii string, will be space padded.
                  a    An ascii string, will be null padded."


so:

$a = pack("a4", "ab");
$b = pack("A4", "ab");
print "a is <$a>, b is <$b>\n";
printf "length(a) = %d, length(b) = %d\n", length($a), length($b);

will produce:

a is <ab>, b is <ab  >
length(a) = 4, length(b) = 4

print pack("A5", "Just"), pack("A8", "another"), pack("A5", "Perl"),
pack("A7", "hacker,");

Mark

rbj@uunet.UU.NET (Root Boy Jim) (03/28/91)

In article <1495@culhua.prg.ox.ac.uk> bush@ecs.ox.ac.uk (Mark Bush) writes:
?In article <126310@uunet.UU.NET> rbj@uunet.UU.NET (Root Boy Jim) writes:
?>In article <MAYER.91Mar21141900@porky.sono.uucp> mayer@sono.uucp (Ronald &) writes:
?>?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?
?>
?>Obviously not.

Because there are no synonyms.

?>?If not, are the differences documented (not in my man page).
?
?But the man page *does* document the difference (and a very useful
?difference it is, too!
?
?>To be honest, I couldn't quote you the exact differences,
?>chapter and verse.

I also said that I don't care, and that when I do I'll look it up.

?From the man page:

Please be careful who you're quoting.
You made me look bad, a task I don't need any help at. 

Since I'm here, I might as well talk perl. And man pages seem to
be a topic mentioned here, so...

How about a separate man page for each perl operator?
We could put them in section 9. Then we could say:
RTFM: man 9 split, man 9 join, man 9 from-outer-space :-)

Oh yeah. I would also like to see the flow control commands
stuffed in there too. Put `if' between `hex' and `index'.
-- 
		[rbj@uunet 1] stty sane
		unknown mode: sane

bush@ecs.ox.ac.uk (Mark Bush) (03/29/91)

In article <126612@uunet.UU.NET> rbj@uunet.UU.NET (Root Boy Jim) writes:
>In article <1495@culhua.prg.ox.ac.uk> bush@ecs.ox.ac.uk (Mark Bush) writes:
>Please be careful who you're quoting.
>You made me look bad, a task I don't need any help at. 

Guhh!  Meant to follow up to the original article!  Public appology time!

Mark