[31170] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2415 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu May 14 00:14:23 2009

Date: Wed, 13 May 2009 21:14:12 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Wed, 13 May 2009     Volume: 11 Number: 2415

Today's topics:
    Re: writing get_script as an external routine callable  <frank@example.invalid>
    Re: writing get_script as an external routine callable  <ben@morrow.me.uk>
    Re: writing get_script as an external routine callable  <jurgenex@hotmail.com>
    Re: writing get_script as an external routine callable  <jurgenex@hotmail.com>
    Re: writing get_script as an external routine callable  <frank@example.invalid>
    Re: writing get_script as an external routine callable  <frank@example.invalid>
    Re: writing get_script as an external routine callable  <ben@morrow.me.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 13 May 2009 17:08:15 -0700
From: Franken Sense <frank@example.invalid>
Subject: Re: writing get_script as an external routine callable by C
Message-Id: <12cmzuo5v3u37$.1dv5zul0b0f9d$.dlg@40tude.net>

In Dread Ink, the Grave Hand of Ben Morrow Did Inscribe:

> Quoth frank@example.invalid:

[snipped and reordered]
>> The reference here is §21 of the camel book, p. 540.
>> 
>> Does any of this look close?
> 
> Reasonably so. You should be using perldoc perlembed as your reference
> rather than the Camel book: the Camel was published shortly before the
> release of perl 5.6.0, and a lot has changed since then. Most
> importantly, you are missing PERL_SYS_INIT3 and PERL_SYS_TERM which must
> be called first and last respectively.
> 
> You should also be aware that there are a number of undocumented
> initializations that may or may not be necessary depending on your
> platform. You can print a C file which ought to reproduce your current
> perl binary by running
> 
>     perl -MExtUtils::Miniperl -ewritemain
> 
> Perl embedding is quite subtle, and I would not really recommend it
> until you are familiar with writing XS. In order to do anything useful
> with the embedded interpreter, you will need to know how to use the perl
> API to get at and interpret the Perl values in the program.
> 
> You are passing main's argc/v to perl_parse, which means your program
> will need to be called with the same command-line arguments as perl
> would be. If you want your C program to run a particular Perl program,
> you will need to create your own argv array to pass to perl_parse. Note
> that you must still pass main's argc/v/env to PERL_SYS_INIT3.
> 
> If you really want to try this, you need to start by reading perlembed
> and perlcall, and then probably perlapi and perlguts.

With that amount of reading in order to embed perl into C, I'll put that on
the reading list for my next injury.  Thank you.

> You haven't yet explained why you're trying to do this. Given your
> inexperience with perl, it's almost certainly going to be easier to
> stick with writing in one language at a time for now.
> 

I've been reading §6.  The semantics section does not include an example
with inputs.  I think the best example is in Tricks with Parameter Lists
with something like:

sub get_script {
  my($verse, $script) =@_;
 ...

I was thinking that I would try to--thereafter--get these data in a hash,
which is close as I'm going to able to come to getting them into a tree.  I
suppose a hash is a tree, why not?

Anyways, below I see 
sub configuration {
  my %options = @_;
 ...

> If you actually want to pass a ref, your sub needs to look more like
> 
>     sub get_script {
>         my ($verse, $sref) = @_;
>         my $script = join " ", @$sref;

Ok, so a little more of the same terminology with @_ .  How does one
pronounce this symbol?

If main were going to open my_file and get the $verse and $script that
we've been using and calls get_script with

getscript($verse, $script);

and instead have:

sub get_script {
  my %options = @_;

, what will %options look like?  Could you print the hash in alphabetic
order?  Can you search in blindingly-fast time for a particular chapter and
verse, indicated by the obvious interpretation of $verse?
-- 
Frank

When you encounter seemingly good advice that contradicts other seemingly
good advice, ignore them both.
~~ Al Franken,


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

Date: Thu, 14 May 2009 00:54:43 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: writing get_script as an external routine callable by C
Message-Id: <373sd6-ple.ln1@osiris.mauzo.dyndns.org>


Quoth frank@example.invalid:
> In Dread Ink, the Grave Hand of Ben Morrow Did Inscribe:
> 
> I've been reading §6.  The semantics section does not include an example
> with inputs.  I think the best example is in Tricks with Parameter Lists
> with something like:
> 
> sub get_script {
>   my($verse, $script) =@_;
> ...
> 
> I was thinking that I would try to--thereafter--get these data in a hash,
> which is close as I'm going to able to come to getting them into a tree.  I
> suppose a hash is a tree, why not?

Well, a hash has some of the properties of a binary tree: fast lookup by
string and uniqueness of keys. A tree is also ordered, which a hash is
not.

> Anyways, below I see 
> sub configuration {
>   my %options = @_;
> ...
> 
> > If you actually want to pass a ref, your sub needs to look more like
> > 
> >     sub get_script {
> >         my ($verse, $sref) = @_;
> >         my $script = join " ", @$sref;
> 
> Ok, so a little more of the same terminology with @_ .  How does one
> pronounce this symbol?

Personally, I don't :).

> If main were going to open my_file and get the $verse and $script that
> we've been using and calls get_script with
> 
> getscript($verse, $script);
> 
> and instead have:
> 
> sub get_script {
>   my %options = @_;

Umm, I don't think I'm understanding what you're doing here, or you're
not. In general it's best to post complete (but short) programs that you
have actually tested, as it makes things clearer.

If you have 

    sub get_script {
        my %options = @_;

then it expects to be called like

    get_script(verse => $verse, script => $script);

and you will then end up with

    $options{verse}  = $verse;
    $options{script} = $script;

If you call it as you have above, you will have a single key which is
the string value of $verse, and its corresponding value will be $script.
I suspect this isn't what you want.

If you are trying to build a hash up over successive calls to
get_script, you will need to either declare the hash outside the sub
(making it (more-or-less) equivalent to a file-level static in C) or use
the new 'state' keyword in perl 5.10 (which is equivalent to a
function-level static in C). In any case, you will want to assign the
provided key and value to different variables, and then insert them into
the hash: assigning to a hash like that replaces everything in it. You
might want something like

    my %Scripts;

    sub get_script {
        my ($verse, $script) = @_;

        $Scripts{$verse} = $script;

or

    # this will only work with perl 5.10
    use feature "state";

    sub get_script {
        my ($verse, $script) = @_;

        state %scripts;
        $scripts{$verse} = $script;

(I have a rule that file-scoped variables start with an uppercase
letter). Of course, you would then probably want to call the sub
'set_script' rather than 'get_script'.

> , what will %options look like?  Could you print the hash in alphabetic
> order?  Can you search in blindingly-fast time for a particular chapter and
> verse, indicated by the obvious interpretation of $verse?

Lookup (by exact key only) is fast: that's the point of hashes. Printing
the hash in alphabetic order will require the 'keys' and 'sort'
functions: see perldoc perlfunc.

Ben



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

Date: Wed, 13 May 2009 17:53:07 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: writing get_script as an external routine callable by C
Message-Id: <j5qm05pc2vig8g2n2beuv4lmj8mq09r1g0@4ax.com>

Franken Sense <frank@example.invalid> wrote:
>If main were going to open my_file and get the $verse and $script that
>we've been using and calls get_script with
>
>getscript($verse, $script);
>
>and instead have:
>
>sub get_script {
>  my %options = @_;
>
>, what will %options look like?  

You are passing a list of two scalars to the sub, these are copied into
a hash, which would mean you got a hash with one element, the value of
$verse being the hash key and the value of $script being the hash value
for this element.

>Could you print the hash in alphabetic order?

You need to be more specific. Do you want to print the keys or the
values sorted by the alphabetical order of the keys or by the
alpabetical order of the values? Either one is easy enough.

>Can you search in blindingly-fast time for a particular chapter and
>verse, indicated by the obvious interpretation of $verse?

Can you reconstruct $verse (i.e. the hash key) from that particular
chapter and verse? If yes, then access will be O(1) in the general case.
You can't get any faster than that.

If you cannot reconstruct the hash key based on chapter and verse and
searching for that is _the_ critical operation in your program, then I
would suggest to redesign the datastructure such that a combination of
chapter and verse can be used as the key.

Or use a database system instead.

jue


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

Date: Wed, 13 May 2009 18:06:45 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: writing get_script as an external routine callable by C
Message-Id: <27rm05l7ie6haqd3hiqljhrq037sb7qn3e@4ax.com>

Ben Morrow <ben@morrow.me.uk> wrote:
>Quoth frank@example.invalid:
>> getscript($verse, $script);
>> 
>> and instead have:
>> 
>> sub get_script {
>>   my %options = @_;
>
>Umm, I don't think I'm understanding what you're doing here, or you're
>not. In general it's best to post complete (but short) programs that you
>have actually tested, as it makes things clearer.
>
>If you have 
>
>    sub get_script {
>        my %options = @_;
>
>then it expects to be called like
>
>    get_script(verse => $verse, script => $script);
>
>and you will then end up with
>
>    $options{verse}  = $verse;
>    $options{script} = $script;
>
>If you call it as you have above, you will have a single key which is
>the string value of $verse, and its corresponding value will be $script.
>I suspect this isn't what you want.

Actually, I think (but I may be wrong) that's exactly what he's looking
for.

>If you are trying to build a hash up over successive calls to
>get_script, you will need to either declare the hash outside the sub

Good point, missed that one.
And if he makes %options global, then there is little point in calling
that sub, he could just as well do a direct 
	%options = ($verse -> $script);
instead of the sub call.

> In any case, you will want to assign the
>provided key and value to different variables, and then insert them into
>the hash: assigning to a hash like that replaces everything in it. 

Another good catch! So do a 
	$options{$verse} = $script;
instead of that sub call.

jue


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

Date: Wed, 13 May 2009 20:38:45 -0700
From: Franken Sense <frank@example.invalid>
Subject: Re: writing get_script as an external routine callable by C
Message-Id: <tx81yz0q6cc.qezks3iaes03$.dlg@40tude.net>

In Dread Ink, the Grave Hand of Jürgen Exner Did Inscribe:

> Ben Morrow <ben@morrow.me.uk> wrote:
>>Quoth frank@example.invalid:
>>> getscript($verse, $script);
>>> 
>>> and instead have:
>>> 
>>> sub get_script {
>>>   my %options = @_;
>>
>>Umm, I don't think I'm understanding what you're doing here, or you're
>>not. In general it's best to post complete (but short) programs that you
>>have actually tested, as it makes things clearer.

Testing now.
>>
>>If you have 
>>
>>    sub get_script {
>>        my %options = @_;
>>
>>then it expects to be called like
>>
>>    get_script(verse => $verse, script => $script);
>>
>>and you will then end up with
>>
>>    $options{verse}  = $verse;
>>    $options{script} = $script;
>>
>>If you call it as you have above, you will have a single key which is
>>the string value of $verse, and its corresponding value will be $script.
>>I suspect this isn't what you want.
> 
> Actually, I think (but I may be wrong) that's exactly what he's looking
> for.
> 
>>If you are trying to build a hash up over successive calls to
>>get_script, you will need to either declare the hash outside the sub
> 
> Good point, missed that one.
> And if he makes %options global, then there is little point in calling
> that sub, he could just as well do a direct 
> 	%options = ($verse -> $script);
> instead of the sub call.
> 
>> In any case, you will want to assign the
>>provided key and value to different variables, and then insert them into
>>the hash: assigning to a hash like that replaces everything in it. 
> 
> Another good catch! So do a 
> 	$options{$verse} = $script;
> instead of that sub call.
> 
> jue

I'll see if I can put that all together; I think I can.

I switched data sets to the second part of the old testament that you might
find at gutenberg.org.  I think this is a solid choice, as I didn't want to
get to hung up on the specific form I ran into on my first sortie with
computer exegetics, namely Acts.

I really like the look of these new data:

Canticle of Canticles Chapter 4


Christ sets forth the graces of his spouse:  and declares his love for
her.

4:1. How beautiful art thou, my love, how beautiful art thou! thy eyes
are doves' eyes, besides what is hid within.  Thy hair is as flocks of
goats, which come up from mount Galaad.

How beautiful art thou. . .Christ again praises the beauties of his
church, which through the whole of this chapter are exemplified by a
variety of metaphors, setting forth her purity, her simplicity, and her
stability.

 ...

Gosh, I'm gonna have to look at nuns in a different light now.  Anyways,
the form here is basically the same: if a line is a scripture, it begins
with a number that will serve as a key for searches.

This is the latest version:

#!/usr/bin/perl
# perl bb2.pl  
use warnings;
use strict;

# open input file
my $filename = 'ot4.txt';
open(my $fh, '<', $filename) or 
  die "cannot open $filename for reading: $!";

# open output file
my $filename2 = 'outfile16.txt';
open(my $gh, '>', $filename2) or 
  die "cannot open $filename2 for writing: $!";

local $/="";

while ( <$fh> ) 
{
  my ( $verse, @s ) = split;
  my $script = join ' ', @s;
  print  "$verse $script\n"; 
  print $gh "$verse $script\n";
}


# close input and output files
close($gh) or die("Error closing $filename2: $!");
close($fh) or die("Error closing $filename: $!");

# abridged output:

C:\MinGW\source>perl bb1.pl
Canticle of Canticles Chapter 2
Christ caresses his spouse: he invites her to him.
2:1. I am the flower of the field, and the lily of the valleys.
I am the flower of the field. . .Christ professes himself the flower of
mankind,
 yea, the Lord of all creatures: and, ver. 2, declares the excellence of
his spo

 ...
 bite and destroy the vines.
2:16. My beloved to me, and I to him who feedeth among the lilies,
2:17. Till the day break, and the shadows retire. Return: be like, my
beloved, t
o a roe, or to a young hart upon the mountains of Bether.

C:\MinGW\source>
-- 
Frank

The biases the media has are much bigger than conservative or liberal.
They're about getting ratings, about making money, about doing stories that
are easy to cover.
~~ Al Franken,


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

Date: Wed, 13 May 2009 21:10:01 -0700
From: Franken Sense <frank@example.invalid>
Subject: Re: writing get_script as an external routine callable by C
Message-Id: <ynqpswoleo4k$.b1ta8ovg3hrv.dlg@40tude.net>

In Dread Ink, the Grave Hand of Franken Sense Did Inscribe:

> Gosh, I'm gonna have to look at nuns in a different light now. 

http://lomas-assault.net/usenet/(!!hail%20mary).jpg 


> Anyways,
> the form here is basically the same: if a line is a scripture, it begins
> with a number that will serve as a key for searches.

I'm bombing out here on something I think is easy.  How do I populate
@books with book1.txt that looks like

  Book of Psalms
  Book of Proverbs
  Ecclesiastes
  Solomon's Canticle of Canticles
  Book of Wisdom
  ...
  Prophecy of Zacharias
  Prophecy of Malachias
  First Book of Machabees
  Second Book of Machabees

#!/usr/bin/perl
# perl bb3.pl  
use warnings;
use strict;

# open input file
my $filename = 'book1.txt';
open(my $fh, '<', $filename) or 
  die "cannot open $filename for reading: $!";

# open output file
my $filename2 = 'outfile16.txt';
open(my $gh, '>', $filename2) or 
  die "cannot open $filename2 for writing: $!";

my @books;

while ( <$fh> ) 
{
  chomp;



}


# close input and output files
close($gh) or die("Error closing $filename2: $!");
close($fh) or die("Error closing $filename: $!");
-- 
Frank

I said that Sean Hannity took residence up Newt Gingrich's butt from 94 to
98. I got that from British intelligence. It turns out he only took up
residence in 95.
~~ Al Franken


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

Date: Thu, 14 May 2009 04:41:50 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: writing get_script as an external routine callable by C
Message-Id: <uggsd6-s1k.ln1@osiris.mauzo.dyndns.org>


Quoth frank@example.invalid:
> 
> I'm bombing out here on something I think is easy.  How do I populate
> @books with book1.txt that looks like
> 
>   Book of Psalms
>   Book of Proverbs
>   Ecclesiastes
>   Solomon's Canticle of Canticles
>   Book of Wisdom
>   ...
>   Prophecy of Zacharias
>   Prophecy of Malachias
>   First Book of Machabees
>   Second Book of Machabees
> 
> #!/usr/bin/perl
> # perl bb3.pl  
> use warnings;
> use strict;
> 
> # open input file
> my $filename = 'book1.txt';
> open(my $fh, '<', $filename) or 
>   die "cannot open $filename for reading: $!";
> 
> # open output file
> my $filename2 = 'outfile16.txt';
> open(my $gh, '>', $filename2) or 
>   die "cannot open $filename2 for writing: $!";
> 
> my @books;

    chomp( my @books = <$fh> );

If you prefer, you can expand that to three statements:

    my @books;
    @books = <$fh>;
    chomp @books;

See perldoc perlop, section "I/O Operators", and perldoc -f chomp.

You can also use File::Slurp::read_file, which will handle opening the
file, chomping the lines and closing the file for you.

Ben



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

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.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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 V11 Issue 2415
***************************************


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