[22273] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4494 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jan 31 11:07:09 2003

Date: Fri, 31 Jan 2003 08:05:10 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 31 Jan 2003     Volume: 10 Number: 4494

Today's topics:
        ANNOUNCE: Tk-MatchEntry 0.1 <wolf@code-wizards.com>
    Re: Attitude to Perl in academia (=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=)
    Re: Attitude to Perl in academia (Rat Tank)
    Re: Attitude to Perl in academia (Peter Scott)
    Re: Checking for a file Andrew Lee
    Re: Checking for a file <flavell@mail.cern.ch>
    Re: Comparing files with difference percentage <krahnj@acm.org>
    Re: creating a binary file with desired size Andrew Lee
    Re: Creating my own Guestbook. Andrew Lee
    Re: Creating my own Guestbook. <usenet@dwall.fastmail.fm>
    Re: Email attachment with IO::Socket <mememe@meme.com>
    Re: Email attachment with IO::Socket <noreply@gunnar.cc>
    Re: How to find @INC in a mod_perl compiled httpd binar <twhu@lucent.com>
        inconsistent handling of undefined values? <ra.jones@NO_UCE*cwcom.net>
    Re: inconsistent handling of undefined values? <pinyaj@rpi.edu>
    Re: inconsistent handling of undefined values? (Tad McClellan)
        mailbox saving <CalinG@cfgroup.ca>
        Multi-line (with \n's) regex search (Chris)
    Re: Multi-line (with \n's) regex search <espenmyr@start.no.cretinfilter>
    Re: Multi-line (with \n's) regex search (Tad McClellan)
        newbie EASY problem :-) <n_joeller@sharblbaziilyar.com>
    Re: newbie EASY problem :-) (Tad McClellan)
    Re: newbie EASY problem :-) <glex_nospam@qwest.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 31 Jan 2003 10:52:28 +0100
From: "Wolfgang Hommel" <wolf@code-wizards.com>
Subject: ANNOUNCE: Tk-MatchEntry 0.1
Message-Id: <3e3a8ffc$1_6@news.teranews.com>

Tk::MatchEntry is yet another combination of an entry widget and a listbox
attempting to provide user-friendly auto-completion.

This one is modelled after the "recipient" entry widgets in modern email
clients and differs from other currently well-known widgets (BrowseEntry,
HistEntry, JBrowseEntry, SelectionEntry) as follows:

- no "arrow button" attached. Instead, the listbox with auto-completion
choices pops up automatically if there are at least 2 different choices
which match. If you need a button to popup the listbox, you can use the
popup() method.

- the listbox with auto-completion choices only shows those entries which
still can match what the user is entering. Thus, the number of choices
shown to the user usually decreases with increasing length of manually
entered text.

- advanced handling of case-insensitive auto-completion. Example: you
provide an auto-completion choice "John Doe", and the user starts with a
lower-case "j". Auto-completion will make "john Doe" out of it, however if
the MatchEntry widget is left with accepting this auto-completion choice,
"john Doe" will be auto-converted to "John Doe". On the other hand, the
user could enter all-lowercase "jane doe" without auto-completion messing
up anything.

- additional callbacks, triggered when the manually entered text is 0 or 1
characters in length, to easily change auto-completion choices. In the
email client example, if the user has not entered any text yet, a
MatchEntry widget could be used to provide a list of the 10 most recent
addresses the user has sent a mail to, then, after he entered the first
character, the auto-completion choices could be his address book entries
instead.

Available at: $CPAN/modules/by-module/Tk/Tk-MatchEntry-0.1.tar.gz




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

Date: 31 Jan 2003 11:13:12 +0100
From: mru@users.sourceforge.net (=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=)
Subject: Re: Attitude to Perl in academia
Message-Id: <yw1x7kclk4p3.fsf@manganonaujakasit.e.kth.se>

rat_tank@yahoo.com (Rat Tank) writes:

> I've programmed for some time in Perl, but at no time has this been
> anything to do with the CompSci degree I'm studying; no course even
> mentions it. Why is this? Perl doesn't seem to have much respect in
> educated programming circles, and I think this is why;
>     Its type system is not entirely sound. Inference upon the typing
> rules (which aren't formally stated anywhere; I had to derive them
> from the sourcecode) can lead to propositional contradictions.

I don't think this is the reason.

>     It is most certainly _not_ Turing complete (trivially provable);
> hence not all algorithms can be implemented in it that you could with
> a Turing complete language like Java or C(++).

BS

>     Perl's reference counting system of garbage collection can
> sometimes result in memory leaks, as opposed to the more thorough
> graph traversal employed in other languages.
>     Despite all this, it's frustrating that I can't get course credit
> for the various projects I've done in my own time (that would be
> perfectly acceptable as coursework if implemented in other languages).

I fully agree with that.

> I know that academia is not quite the 'real world', but I'd have hoped
> that could be offset by just how damn useful Perl is (and I've
> demonstrated this to my profs on more than one occasion). What do you
> think?

Perl is so useful that it is often banned as language for homework in
CS classes.  If Perl was allowed, the task would be too easy.  In some
cases this can be justified, but most of the time it's just pure
non-sense.

I'm also irritated when they require you to do stupid artificial
programming tasks, rather than doing equivalent things that are
actually useful.

-- 
Måns Rullgård
mru@users.sf.net


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

Date: 31 Jan 2003 07:14:00 -0800
From: rat_tank@yahoo.com (Rat Tank)
Subject: Re: Attitude to Perl in academia
Message-Id: <ae14ba57.0301310714.1e4f5e27@posting.google.com>

Uri Guttman <uri@stemsystems.com> wrote in message news:<x73cna33lm.fsf@mail.sysarch.com>...
> >>>>> "RT" == Rat Tank <rat_tank@yahoo.com> writes:
> 
> huh? perl has no typing system as do most other languages. so how can
> you dreive one?
    Actually, I think you'll find that Perl does have a typing system,
but it's of the form of languages like ML or Smalltalk; it's an
implicit typing system. The programmer may well not know the exact
type, but the underlying compiler does have a concept of the type of
each symbol, and infers it automatically at runtime from context.
Because the type is hidden, that makes it even more severe that the
Perl typer has PROVABLE BUGS. Such un-typesafeness can result in
garbage casts, and worse yet, regular buffer overflows.

>   RT>     It is most certainly _not_ Turing complete (trivially provable);
>   RT> hence not all algorithms can be implemented in it that you could with
>   RT> a Turing complete language like Java or C(++).
> 
> that is total crap. i don't know where you learned about state and
> turing machines but it is hard to find a common programming language
> that isn't turing complete. all you need is a single array, a few state
> variables and some simple boolean logic.
    I don't think you quite understand the subtle concept of Turing
completeness. Perhaps you'd care to post a simple program (in Perl)
that simulates a Turing machine (plus the other requirements for
Turing completeness). I think you'll find that Perl doesn't implement
the recursive structures required.

>   RT>     Perl's reference counting system of garbage collection can
>   RT> sometimes result in memory leaks, as opposed to the more thorough
>   RT> graph traversal employed in other languages.
> 
> that is a known thing and can be handled if you know how to deal with
> circular refs. and if you emply them, you should know how to destroy them.
    The point of garbage collection is that one shouldn't have to
destroy things manually. This isn't a rare occurence either; Perl
regularly uses circular refs in such common structures as hashes and
arrays, and memory leaks are therefore quite frequent.
 
RT


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

Date: Fri, 31 Jan 2003 15:30:04 GMT
From: peter@PSDT.com (Peter Scott)
Subject: Re: Attitude to Perl in academia
Message-Id: <0Ew_9.149264$Yo4.7108940@news1.calgary.shaw.ca>

In article <ae14ba57.0301310714.1e4f5e27@posting.google.com>,
 rat_tank@yahoo.com (Rat Tank) writes:
>Uri Guttman <uri@stemsystems.com> wrote in message news:<x73cna33lm.fsf@mail.sysarch.com>...
>> >>>>> "RT" == Rat Tank <rat_tank@yahoo.com> writes:
>>   RT>     It is most certainly _not_ Turing complete (trivially provable);
>>   RT> hence not all algorithms can be implemented in it that you could with
>>   RT> a Turing complete language like Java or C(++).
>> 
>> that is total crap. i don't know where you learned about state and
>> turing machines but it is hard to find a common programming language
>> that isn't turing complete. all you need is a single array, a few state
>> variables and some simple boolean logic.
>    I don't think you quite understand the subtle concept of Turing
>completeness. Perhaps you'd care to post a simple program (in Perl)
>that simulates a Turing machine (plus the other requirements for
>Turing completeness). I think you'll find that Perl doesn't implement
>the recursive structures required.

http://search.cpan.org/author/GROMMEL/Acme-Turing-0.01/Turing.pm

Uri was being generous.

-- 
Peter Scott
http://www.perldebugged.com


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

Date: Fri, 31 Jan 2003 09:39:02 -0500
From: Andrew Lee
Subject: Re: Checking for a file
Message-Id: <492l3vknpel1vcehop51n1a5mq3pd5vcgs@4ax.com>

On 30 Jan 2003 17:54:26 -0800, mechman991@hotmail.com (Brandon Reddish)
wrote:

>Andrew Lee wrote in message news:<7tvi3vcu76thd2t5tqm3tjdvp9t708542s@4ax.com>...
>> On Thu, 30 Jan 2003 16:37:29 GMT, "TBN" <ihave@noemail.com> wrote:
>> 
>> >I'm still pretty new to Perl, so bear with me on this probably simple
>> >question.
>> >
>> >I know that I can open a file using the following...
>> >open (CODE, $filename) || &OtherStuff;
>> >
>> >But I'm not really interested in opening this file. 
>> 
>> perldoc perlfunc
>> 
>> Look for -X FILEHANDLE ... the list of filetest operators.
>
>Hello,
>The easiest way to check to see if a file exists would be;
>
>if (-e file.html)
>{
>Insert whatever here.
>}

-e being a filetest operator.
:-)

>
>This checks to see if the file simply exists.  Check this page for more information:
>http://pageresource.com/cgirec/ptut17.htm

No need, perldoc perlfunc lists all the filetest operators (though it
has a certain Unix prejudice).

But -- it is the documentation that comes with Perl. and is usually more
up to date and accurate than any online tutorial.


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

Date: Fri, 31 Jan 2003 16:35:24 +0100
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: Checking for a file
Message-Id: <Pine.LNX.4.53.0301311619320.16181@lxplus070.cern.ch>

On Jan 31, Andrew inscribed on the eternal scroll:

> >http://pageresource.com/cgirec/ptut17.htm
>
> No need, perldoc perlfunc lists all the filetest operators

yup

> (though it has a certain Unix prejudice).

But how does that rate in comparison to something predicated on Perl
being hierarchically below CGI?  (Quite apart from the detailed issues
already raised against it on this thread).

Anyway, the Perl documentation seems to be getting steadily migrated
from those who think Unix is in need of being supported by prejudice,
to those who are comfortable with the idea of it standing on its own
superior feet without the need for such special pleading ;-}

> But -- it is the documentation that comes with Perl. and is usually more
> up to date and accurate than any online tutorial.

That can cut both ways.  If the locally-installed version of Perl
isn't the latest, then at least the documentation there will be the
version which was appropriate to it (i.e it won't be up to date), so
it should be the most accurate as far as it relates to versioning
issues; but other kinds of documentation bug may still be present even
though the latest version has corrected the problem.

As a compromise, I would recommend using the documentation appropriate
to the Perl version that one is using, but reading the FAQs from the
latest version.


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

Date: Fri, 31 Jan 2003 09:34:25 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Comparing files with difference percentage
Message-Id: <3E3A4317.5FB9D9F9@acm.org>

Vinicio Tavares wrote:
> 
> It compares the lines, if line 2 (file 1) has 1 character that doesn't
> match with the character on line 2 (file 2) the lines doesn't match.
> I will give a better example now. :-)
> 
> Text1.txt                 Text2.txt
> ----------------          -----------------
> ab c                      ab c
> a b d                     a b c
> ab cd ef                  ab cd  ef
> abc                       abc
> ab                        a b
> abc
> 
> match: Lines 1, 4
> mismatch: Lines 2, 3, 5
> 
> The difference percentage will be:
> 
>    (# of mismaches + # of extra lines in one of the files) / # of
> lines of file 1.
> 
>    In the example:
>    3+1/6 = 0.66 = 66.6%


This should work:

#!/usr/bin/perl
use warnings;
use strict;

open my $f0, $ARGV[0] or die $!;
open my $f1, $ARGV[1] or die $!;

my ( $total, $diff );
do  {
    my $l0 = <$f0>;
    my $l1 = <$f1>;
    $total++;
    {   no warnings 'uninitialized';
        $diff++ if $l0 ne $l1 }
    } until eof $f0 and eof $f1;

print "Diff: ", $diff / $total, "\n";

__END__



John
-- 
use Perl;
program
fulfillment


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

Date: Fri, 31 Jan 2003 09:49:01 -0500
From: Andrew Lee
Subject: Re: creating a binary file with desired size
Message-Id: <633l3volevqe2v14if4dd8junf69c0ag0e@4ax.com>

On 30 Jan 2003 16:46:44 -0800, csejl@yahoo.com (James Lee) wrote:

>hi,
>
>i have a script that takes file name and size and creates a file of
>the size specified.  this is currently done with texts and i was
>wondering if i can do this with binary files.  thanks for your help in
>advance.
>

write N nulls (0x00) to a file :-)


see binmode, print and the 'x' operator.


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

Date: Fri, 31 Jan 2003 09:51:39 -0500
From: Andrew Lee
Subject: Re: Creating my own Guestbook.
Message-Id: <v73l3vsov3rargs510hqa88ht17pg02dcn@4ax.com>

On Fri, 31 Jan 2003 03:38:35 GMT, Martien Verbruggen
<mgjv@tradingpost.com.au> wrote:

>On 30 Jan 2003 18:01:59 -0800,
>	Brandon Reddish <mechman991@hotmail.com> wrote:
>> I have the following form mail codes .. scroll to the bottom for my
>> question:
>
>Next time, don't include so much stuff. If you have this much code,
>put it on your web site, not here.
>
>[SNIP of large amount of non-Perl related stuff]

Is it me or has there been an attack of "Here's ALL of my code, please
debug it" lately ??




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

Date: Fri, 31 Jan 2003 15:09:49 -0000
From: "David K. Wall" <usenet@dwall.fastmail.fm>
Subject: Re: Creating my own Guestbook.
Message-Id: <Xns93146763A67E6dkwwashere@216.168.3.30>

Brandon Reddish <mechman991@hotmail.com> wrote on 30 Jan 2003:
[snip]
> 
> #!/usr/bin/perl

As previously mentioned, you should put 

    use strict;
    use warnings;

at the beginning of every program.  If you have an older version of Perl, 
'use warnings' will produce an error, so in that case you enable it on the 
shebang line:

    #!/usr/bin/perl -w



> require "cgi-lib.pl";

In addition to what Jay and Martien said, please use CGI.pm instead of 
cgi-lib.pl.  cgi-lib.pl is old and not being updated any more.

(Rest of code snipped, and the FAQ has already been pointed out.)

-- 
David K. Wall - usenet@dwall.fastmail.fm
"Oook."


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

Date: Fri, 31 Jan 2003 08:20:06 -0700
From: "ColdCathoids" <mememe@meme.com>
Subject: Re: Email attachment with IO::Socket
Message-Id: <b1e47g$11rpf5$1@ID-158028.news.dfncis.de>


"Mina Naguib" <spam@thecouch.homeip.net> wrote in message
news:jSj_9.10470$F75.81246@wagner.videotron.net...
> -----BEGIN xxx SIGNED MESSAGE-----
> Hash: SHA1
>
>
>
> ColdCathoids wrote:
> | Hello all,
> |
> | First off thanks to everyone who helped me with my more random then
random
> | question earlier this week! Works exactly how I wanted. Now I have
another
> | question.
> |
> | I am writing a small app that sends out an email when a co-worker moves
> | files. The box this runs on is somewhat limited with modules so I was
forced
> | to use IO::Socket to send email. It's working fine but I was wondering
if
> | anyone knew of a way I could send email attachments using the sub below.
I
> | am not able to install any modules on this box so I have to work with
what I
> | got. Any thoughts?
>
> I'm not going to even bother with the snippet of code you posted.  Sending
> attachments through raw socket calls it too much re-inventing the wheel.
>
> Instead I'll focus on why you can't install modules on the box.  Is it
because
> you do not have root access ?  There are several clear documents out there
> describing how you can install and use perl modules from your home
directory
> without requiring root access.
>
> Once that point's resolved, your question can be answered with 1-3 lines
using
> the appropriate module...

Yeah I don't have root access on the box and because my project is of low
priority it would take quite awhile for the sysadmin to get around to
installing any modules. I always forget that I can run certain modules from
a local directory.

Is there anyway of telling which ones will and won't run from a local
directory? Also how would dependencies work? Say I want to use module A
which depends on Module B. Neither are installed. If both A and B are in a
local directory and I use:

 use lib '/path/to/lib';
use ModuleB;
use ModuleA;

will dependencies be satisfied?

Thanks




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

Date: Fri, 31 Jan 2003 15:59:05 GMT
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Email attachment with IO::Socket
Message-Id: <d3x_9.8953$LY2.528542@newsc.telia.net>

ColdCathoids wrote:
> Is there anyway of telling which ones will and won't run from a local
> directory?

Not sure what you mean. Any module will run from a local library as long
as it's not dependent on resources that are not installed.

> Also how would dependencies work? Say I want to use module A which 
> depends on Module B. Neither are installed. If both A and B are in a
> local directory and I use:
> 
> use lib '/path/to/lib';
> use ModuleB;
> use ModuleA;
> 
> will dependencies be satisfied?

Yes. It works as if they had been installed in one of the ordinary
directories. The "use lib" statement just adds another path where Perl
will look for modules that are loaded via "use" or "require".

/ Gunnar

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



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

Date: Fri, 31 Jan 2003 08:46:17 -0500
From: "Tulan W. Hu" <twhu@lucent.com>
Subject: Re: How to find @INC in a mod_perl compiled httpd binary?
Message-Id: <b1dunf$nhb@netnews.proxy.lucent.com>

"Kyndig" <kyndig@knife-fighting.com> wrote in message ..
> Could someone please help me find the @INC path of mod_perl which is
> already statically compiled into an Apache httpd binary?
>
> I'm upgrading an Apache server, but my newly compiled binary gives me
> a "Can't locate foo.pm in @INC" because the hard coded @INC path is
> aparently different than that of the original binary.
>
> I know the problem can be fixed by setting @INC or perl5libs or "use
> lib ...", but I want to change as little as possible; thus I need to
> know how to find the @INC of the old binary and how to set it(during
> compilation) on the new one to match.  As I said, mod_perl is compiled
> in statically.  Any help is greatly appreciated !

in your http.conf file add the following
<Location /per-status>
    SetHandler perl-script
    PerHandler Apache::Status
</Location>
restart your apache and look
all mod_status via http://your.host/perl-status




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

Date: Fri, 31 Jan 2003 14:51:48 +0000
From: RA Jones <ra.jones@NO_UCE*cwcom.net>
Subject: inconsistent handling of undefined values?
Message-Id: <EzIMemAE2oO+Ewz$@nildram.co.uk>

It's like this:

my @db_cols consists of the column names from a MySQL database.
my @vals consists of values extracted from a row of the database. Some 
are defined, others are NULL (ie undefined).

The values in @vals are then taken into a hash by 'marrying' them up 
with their corresponding db_col names like so:

my ($i,%results); $results{$_} = $vals[$i++] foreach(@db_cols);

 From there on I do some further manipulations on %results, but the 
essence of the problem is that if I try to print the contents of 
%results either via CGI to the browser or to a debug text-file, the 
system hangs indefinitely on my PC test-bed, but works fine on the UNIX 
production server.

The script runs under 'use strict' and '-w' flags, and does not complain 
when tested at the command line with the '-cw' switch, but no errors 
appear in the server (Apache) error log.

I have isolated and corrected the problem by 'defining' all values in 
@vals like so:

$_ ||= '' foreach(@vals);

but the question is a) is this the correct way to handle undefined 
values in an array, and b) why the difference between the PC running 
ActiveState v5.6.1 and the Unix system running v5.6.1 for sun4-solaris? 
It's not a performance issue as the PC normally benchmarks faster than 
the server.
-- 
RA Jones
remove NO_UCE* from 'reply-to' address
ra(dot)jones(at)see-double-you-com(dot)net



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

Date: Fri, 31 Jan 2003 10:55:44 -0500
From: Jeff 'japhy' Pinyan <pinyaj@rpi.edu>
To: RA Jones <ra.jones@cwcom.net>
Subject: Re: inconsistent handling of undefined values?
Message-Id: <Pine.SGI.3.96.1030131105435.77025A-100000@vcmr-64.server.rpi.edu>

[posted & mailed]

On Fri, 31 Jan 2003, RA Jones wrote:

>my @db_cols consists of the column names from a MySQL database.
>my @vals consists of values extracted from a row of the database. Some 
>are defined, others are NULL (ie undefined).
>
>The values in @vals are then taken into a hash by 'marrying' them up 
>with their corresponding db_col names like so:
>
>my ($i,%results); $results{$_} = $vals[$i++] foreach(@db_cols);

You can just say:

  my %results;
  @results{@db_cols} = @vals;

As for the hanging problem, I don't know what to tell you.  I don't
believe I've encountered it before.

-- 
Jeff Pinyan            RPI Acacia Brother #734            2003 Rush Chairman
"And I vos head of Gestapo for ten     | Michael Palin (as Heinrich Bimmler)
 years.  Ah!  Five years!  Nein!  No!  | in: The North Minehead Bye-Election
 Oh.  Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)



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

Date: Fri, 31 Jan 2003 09:38:58 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: inconsistent handling of undefined values?
Message-Id: <slrnb3l64i.o3d.tadmc@magna.augustmail.com>

RA Jones <ra.jones@NO_UCE*cwcom.net> wrote:

> I have isolated and corrected the problem by 'defining' all values in 
> @vals like so:
> 
> $_ ||= '' foreach(@vals);
> 
> but the question is a) is this the correct way to handle undefined 
> values in an array


No.

Your code does NOT test for "definededness", it tests for "truth".

If any element in @vals contains 0 or '0', your code above will
change it to the empty string!

Use the defined() operator to test for definededness.

   foreach ( @vals ) {
      $_ = '' unless defined;
   }

or

   $_ = defined() ? $_ : '' for @vals;


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Fri, 31 Jan 2003 09:13:49 -0500
From: "Tester" <CalinG@cfgroup.ca>
Subject: mailbox saving
Message-Id: <6pv_9.249696$C8.854118@nnrp1.uunet.ca>

Hi there,

I was wondering in email how could I save body of emails by sender and by
date? Like:
#! /usr/bin/perl -w
use strict;
open(OUTFILE, ">test") or die "Cannot open file test:$!\n";
while(<>){
if ((/^Sender:John Doe/) && (/*2003/) {
print OUTFILE $_;
}
}
close OUTFILE;
Thanks, Calin



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

Date: 31 Jan 2003 05:23:18 -0800
From: c_campise@hotmail.com (Chris)
Subject: Multi-line (with \n's) regex search
Message-Id: <cf545555.0301310523.e9401a8@posting.google.com>

Hello all-

I'm attempting to scan a C header file (.h) looking for typedef struct
declarations.  When found, I want to read each element of the struct
saving the type and element name into a hash respectively.

i.e :

/* myheader.h */
 ...
 ...
typedef struct MyStruct
{
   int  iVar1;
   char cVar2;
   long lVar3;
}MYSTRUCT, *PMYSTRUCT;
 ...
 ...
/* end myheader.h */

So I wish to build a hash like the following :
%hTypeToVar = ();
$hTypeToVar{int}  = iVar1;
$hTypeToVar{char} = iVar2;
$hTypeToVar{long} = lVar3;
 ...etc...

Short of reading each line of the struct as encountered while scanning
the file via something like : while(<HEADER_FILE>), is there a regexp
that I could use to read it all in one fell swoop (possibly by using
backreferences $1, $2, etc)?

I love Perl, but I must admit my regexp expertise (or lack thereof)
leaves something to be desired.

Thanks in advance!

-Chris


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

Date: 31 Jan 2003 14:39:25 +0100
From: Espen Myrland <espenmyr@start.no.cretinfilter>
Subject: Re: Multi-line (with \n's) regex search
Message-Id: <87d6mdl9pu.fsf@chello.no>

c_campise@hotmail.com (Chris) writes:

> Hello all-
> 
> I'm attempting to scan a C header file (.h) looking for typedef struct
> declarations.  When found, I want to read each element of the struct
> saving the type and element name into a hash respectively.
> 
> i.e :
> 
> /* myheader.h */
> ...
> ...
> typedef struct MyStruct
> {
>    int  iVar1;
>    char cVar2;
>    long lVar3;
> }MYSTRUCT, *PMYSTRUCT;
> ...
> ...
> /* end myheader.h */
> 
> So I wish to build a hash like the following :
> %hTypeToVar = ();
> $hTypeToVar{int}  = iVar1;
> $hTypeToVar{char} = iVar2;
> $hTypeToVar{long} = lVar3;


This is not an answer to your quistion, but what will you do if your 
structure looks like this:

 typedef struct MyStruct
 {
    int  iVar1;
    char cVar2;
    long lVar3;
    long lVar4;
 }MYSTRUCT, *PMYSTRUCT;

?


Look up the utility h2ph 'perldoc h2ph'




-- 
espen


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

Date: Fri, 31 Jan 2003 08:53:27 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Multi-line (with \n's) regex search
Message-Id: <slrnb3l3f7.nte.tadmc@magna.augustmail.com>

Chris <c_campise@hotmail.com> wrote:


> I'm attempting to scan a C header file (.h) looking for typedef struct
> declarations. 


That is not possible using only regular expressions, you need
a Real Parser for the C language.

You should consider rethinking your approach.


> $hTypeToVar{int}  = iVar1;


What will you do if there is more than one "int" in the structure?


> Short of reading each line of the struct as encountered while scanning
> the file via something like : while(<HEADER_FILE>), is there a regexp
> that I could use to read it all in one fell swoop (possibly by using
> backreferences $1, $2, etc)?


You seem to have 2 unrelated things there.

First is how to do the input, second is how to match the 
struct declaration.

You can enable "slurp mode" by setting the $/ variable to undef, 
that will handle the input part.

C allows lots and lots of stuff that is not included in your
sample data. It is futile to attempt to handle arbitrary C
code with simple pattern matching.

Here is some profoundly unreliable code that will handle a severely
limited set of possible structs, such as what you've shown in
your example data:

-------------------------------------
#!/usr/bin/perl
use strict;
use warnings;

$_ = '/* myheader.h */
 ...
 ...
typedef struct MyStruct
{
   int  iVar1;
   char cVar2;
   long lVar3;
}MYSTRUCT, *PMYSTRUCT;
 ...
 ...
typedef struct MyStruct
{
   int  iVar10;
   char cVar20;
   long lVar30;
}MYSTRUCT, *PMYSTRUCT;

/* end myheader.h */
';


while ( /typedef struct.*?{(.*?)}/sg ) {
   my $block = $1;
   while ( $block =~ /(\w+)\s+(\w+);/g ) {
      print "$1  ==> $2\n";
   }
   print "-----\n";
}
-------------------------------------

output:

int  ==> iVar1
char  ==> cVar2
long  ==> lVar3
-----
int  ==> iVar10
char  ==> cVar20
long  ==> lVar30


> I love Perl, but I must admit my regexp expertise (or lack thereof)
> leaves something to be desired.


That's fine, seeing as regexes alone are not going to 
solve your problem anyway.  :-)


You haven't thought about your problem enough yet. It is nowhere
near as easy to do robustly as you seem to think it is...


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Fri, 31 Jan 2003 15:09:27 GMT
From: Noerd <n_joeller@sharblbaziilyar.com>
Subject: newbie EASY problem :-)
Message-Id: <3E3A9196.2BFFD2BC@sharblbaziilyar.com>

Sorry for such a naive question... But, here goes:

To find if my item description has any of the following in it:

(W/extra item)
(W/accompaniment)
(W/anything for that matter)
(W/blah blah blah)
 ...for example:
"Super fast processor (W/backside cache)"

I use the following perl code:
$itemdescription[2] =~ s+\(W\\/(.*?)\)+

However, if my item description has any of the following:

(W/25 Pages)
(W/222 Pages)
(W/636 Pages)
(W/288 Pages)
 ....for example:
"Owners manual (W/383 Pages)"

Where would I put the word, "Pages," in my perl code

I tried these, but they don't work:
$itemdescription[2] =~ s+\(W\\/(.*?)Pages\)+
$itemdescription[2] =~ s+\(W\\/(.*Pages)\)+

Thank you!!!

Oh, by the way, I need to leave all the backslashes "\" in there because
the previous portion of my script has to escape all kinds of characters
(I want to leave my code format, etc., intact....I just what to find out
where in the dickens to put the word, "Pages."  :-)



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

Date: Fri, 31 Jan 2003 09:51:27 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: newbie EASY problem :-)
Message-Id: <slrnb3l6rv.o3d.tadmc@magna.augustmail.com>

Noerd <n_joeller@sharblbaziilyar.com> wrote:

> I use the following perl code:
> $itemdescription[2] =~ s+\(W\\/(.*?)\)+


You don't use it for much then, as it will not even compile.

    Do not re-type Perl code
        Use copy/paste or your editor's "import" function rather than
        attempting to type in your code. If you make a typo you will get
        followups about your typos instead of about the question you are
        trying to get answered.


> However, if my item description has any of the following:

> "Owners manual (W/383 Pages)"
> 
> Where would I put the word, "Pages," in my perl code


You do not need to put the word "Pages" in your Perl code.


If you get the code to compile, and if the pattern matches the
string, then the .*? part will match "Pages" just fine.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Fri, 31 Jan 2003 10:01:00 -0600
From: Jeff D Gleixner <glex_nospam@qwest.net>
Subject: Re: newbie EASY problem :-)
Message-Id: <j3x_9.19$63.21671@news.uswest.net>

[...]
> "Owners manual (W/383 Pages)"
> 
> Where would I put the word, "Pages," in my perl code
> 
> I tried these, but they don't work:
> $itemdescription[2] =~ s+\(W\\/(.*?)Pages\)+
> $itemdescription[2] =~ s+\(W\\/(.*Pages)\)+
> 
> Thank you!!!
> 
> Oh, by the way, I need to leave all the backslashes "\" in there because
> the previous portion of my script has to escape all kinds of characters
> (I want to leave my code format, etc., intact....I just what to find out
> where in the dickens to put the word, "Pages."  :-)

Why are you using  the substitute (=~ s)?

Provide a snippet of  $itemdescription[2] 's value, without that we can only
guess what your string actually looks like.

Where to put "Pages" depends on what you're trying to "save" (using ()) in you
expression.

#!/usr/local/bin/perl
my $str="Owners manual (W/383 Pages)";
if ($str=~m+\(W/(.*?)Pages\)+)
{
	print "$1\n";
}
if ($str=~m+\(W/(.*Pages)\)+)
{
	print "$1\n";
}

383
383 Pages






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

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

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 V10 Issue 4494
***************************************


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