[32087] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3351 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Apr 14 21:09:27 2011

Date: Thu, 14 Apr 2011 18:09:08 -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           Thu, 14 Apr 2011     Volume: 11 Number: 3351

Today's topics:
        C callbacks returning a value <njh@despammed.com>
    Re: C callbacks returning a value <smallpond@juno.com>
    Re: Changing XML tag value in Perl <xhoster@gmail.com>
    Re: Changing XML tag value in Perl <bugbear@trim_papermule.co.uk_trim>
    Re: Changing XML tag value in Perl <cartercc@gmail.com>
    Re: Changing XML tag value in Perl <cartercc@gmail.com>
    Re: dereferencing in perl <marc.girod@gmail.com>
    Re: dereferencing in perl <marc.girod@gmail.com>
    Re: dereferencing in perl <Uno@example.invalid>
    Re: dereferencing in perl <Uno@example.invalid>
    Re: dereferencing in perl <glex_no-spam@qwest-spam-no.invalid>
    Re: dereferencing in perl <Uno@example.invalid>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 14 Apr 2011 14:59:26 -0700 (PDT)
From: Nigel <njh@despammed.com>
Subject: C callbacks returning a value
Message-Id: <9b117221-4761-4de1-b77c-3210331a3979@hg8g2000vbb.googlegroups.com>

I have a callback routine written in C which returns an int.  In all
of the examples of how to write callback rountines in the perlcall
perldoc (http://perldoc.perl.org/perlcall.html), none of the examples
returns a value.  All the callbacks have been defined as 'void'.

Does this mean that it's not possible to return a value from a C
callback called from Perl?  If so, how can it be done and will the
perlcall document be updated.  If not, is this a feature that will be
added in 5.14?

-Nigel


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

Date: Thu, 14 Apr 2011 15:22:05 -0700 (PDT)
From: smallpond <smallpond@juno.com>
Subject: Re: C callbacks returning a value
Message-Id: <108c016c-1a2e-487c-b79d-9c2018d7ff0b@r6g2000vbo.googlegroups.com>

On Apr 14, 5:59=A0pm, Nigel <n...@despammed.com> wrote:
> I have a callback routine written in C which returns an int. =A0In all
> of the examples of how to write callback rountines in the perlcall
> perldoc (http://perldoc.perl.org/perlcall.html), none of the examples
> returns a value. =A0All the callbacks have been defined as 'void'.
>
> Does this mean that it's not possible to return a value from a C
> callback called from Perl? =A0If so, how can it be done and will the
> perlcall document be updated. =A0If not, is this a feature that will be
> added in 5.14?
>

perlxs describes calling C from Perl.
http://perldoc.perl.org/perlxs.html


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

Date: Wed, 13 Apr 2011 21:52:45 -0700
From: Xho Jingleheimerschmidt <xhoster@gmail.com>
Subject: Re: Changing XML tag value in Perl
Message-Id: <4da68645$0$19462$ed362ca5@nr5-q3a.newsreader.com>

Rajpreet wrote:
> Thanks for your inputs. However problem in my case is my production
> environment has just XML::Parser . I am not allowed to download any
> thing. 

But you are allowed to post to usenet?  Do you live in a giant Dilbert 
cartoon?

> So options are either to directly manipulate or use
> XML::Parser. I have not done XML parsing anytime earlier . Would
> appreciate your inputs. Thanks.

I think XML::Twig is almost exactly what you would need.  If you aren't 
allowed to use it, you could always look at its source code and see how 
it does what it does.

Xho


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

Date: Thu, 14 Apr 2011 09:14:24 +0100
From: bugbear <bugbear@trim_papermule.co.uk_trim>
Subject: Re: Changing XML tag value in Perl
Message-Id: <2t6dnRWJdo18MTvQnZ2dnUVZ8jidnZ2d@brightview.co.uk>

Rajpreet wrote:
> my $msgDescRef = $message->MsgDesc;
> my $data = $message->Data;
> my $field_name = GetXMLValue($data, "FIELD");
> if ($field_name eq 'FINANCE GROUP')
> {
>
> $field_name = 'FINANCIAL SYSTEMS';
>
> $msgDescRef->{"FIELD"} = $field_name ;
> }


If  $message->MsgDesc returns a COPY of the $message's hash,
assigning to that hash won't do anything to the $message.

   BugBear


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

Date: Thu, 14 Apr 2011 04:19:20 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: Changing XML tag value in Perl
Message-Id: <dd0ad1d1-34b7-4c94-8915-0a530668829d@i4g2000vbq.googlegroups.com>

On Apr 13, 4:48=A0pm, Rajpreet <rajpreetsi...@gmail.com> wrote:
> Thanks for your inputs. However problem in my case is my production
> environment has just XML::Parser .

Then I suggest that you ASSUME that all your data is regular, and that
you write a script that uses index(), substr() and friends (not
regular expressions), and write bad input values out to a file.

If your manager doesn't trust you to install a Perl module, he
certainly doesn't trust you to change input values. So just collect
the input errors and send them to him.

You should be able to do what you want easily enough if your XML is
well formed.

>> if ($field_name eq 'FINANCE GROUP')
>> {
>>    $field_name =3D 'FINANCIAL SYSTEMS';
>>    $msgDescRef->{"FIELD"} =3D $field_name ;
>> }

might want to do something like this while reading the file:


if ($_ =3D~ /<FINANCE GROUP>/)


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

Date: Thu, 14 Apr 2011 04:30:07 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: Changing XML tag value in Perl
Message-Id: <aab2e84d-cde4-454d-9a47-e5cc1b7df1a3@z27g2000prz.googlegroups.com>

Sorry for the previous mangles post, I hit the wrong key. See below.

On Apr 13, 4:48 pm, Rajpreet <rajpreetsi...@gmail.com> wrote:

> Thanks for your inputs. However problem in my case is my production
> environment has just XML::Parser .

Then I suggest that you ASSUME that all your data is regular, and that
you write a script that uses index(), substr() and friends (not
regular expressions), and write bad input values out to a file.

If your manager doesn't trust you to install a Perl module, he
certainly doesn't trust you to change input values. So just collect
the input errors and send them to him.

You should be able to do what you want easily enough if your XML is
well formed.

>> if ($field_name eq 'FINANCE GROUP')
>> {
>>    $field_name = 'FINANCIAL SYSTEMS';
>>    $msgDescRef->{"FIELD"} = $field_name ;
>> }

might want to do something like this while reading the file (NOT
TESTED):

while (<DATA>
{
  ...
  if ($_ =~ /<FINANCE GROUP>/)
  {
      print OUT "<FINANCIAL SYSTEMS>\n";
      while (<DATA>)
      {
         if ($_ =~ m!</FINANCE GROUPS>!)
         {
            print OUT "</FINANCIAL SYSTEMS>\n";
            last;
         }
         print OUT $_;
      }
   }
   else { print OUT $_; }
   ...
}

CC


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

Date: Thu, 14 Apr 2011 02:08:52 -0700 (PDT)
From: Marc Girod <marc.girod@gmail.com>
Subject: Re: dereferencing in perl
Message-Id: <7247e881-df74-4498-8a35-70e614e82860@p16g2000vbo.googlegroups.com>

On Apr 13, 1:08=A0pm, Marc Girod <marc.gi...@gmail.com> wrote:

> I cannot connect to that server, so that I cannot reproduce.

I found an nntp I didn't know inside Ericsson!

for (my $i =3D ($last -5); $i <=3D $last; $i++) {
  print "$i \n";
  my $subject =3D $c->xhdr("", $i);
  print $_ for @{$subject};
}

gives:

543345   623640
623635
623635 <ivpbj69e36ikrrk7l0912uoaoigm2pjpbn@4ax.com>
623636
623636 <m2qbj611nletsdv6f20msivlbtnji9ff12@4ax.com>
623637
623637 <slrnijc4ih.k4u.hjp-usenet2@hrunkner.hjp.at>
623638
623638 <SXoZo.27312$a66.11012@newsfe09.iad>
623639
623639 <nduZo.70644$v27.45830@newsfe22.iad>
623640
623640 <RuzZo.152$Ng4.34@newsfe04.iad>

Marc


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

Date: Thu, 14 Apr 2011 03:40:18 -0700 (PDT)
From: Marc Girod <marc.girod@gmail.com>
Subject: Re: dereferencing in perl
Message-Id: <aebf4550-a929-44cf-a0c0-55b1fbc9cd2a@u12g2000vbf.googlegroups.com>

On Apr 14, 10:08=A0am, Marc Girod <marc.gi...@gmail.com> wrote:

> I found an nntp I didn't know inside Ericsson!

Oh! And now that I started to read the man page, sorry to say that you
shot yourself in the foot.
The following works (in replacement for you whole loop):

print $c->xhdr("", $last-5, $last);

I.e. print does 'the right thing', as documented.
You just prevent it from happening by forcing a scalar context,
as you assign the result to a variable.

Marc


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

Date: Thu, 14 Apr 2011 14:08:35 -0600
From: Uno <Uno@example.invalid>
Subject: Re: dereferencing in perl
Message-Id: <90p2i4F4fiU1@mid.individual.net>

On 04/14/2011 03:08 AM, Marc Girod wrote:

>    print $_ for @{$subject};

$ perl client6.pl
News::NNTPClient::SOCK1 command: GROUP comp.lang.perl.misc
News::NNTPClient::SOCK1 result(211): 4770 658251 663046 comp.lang.perl.misc
658251   663046
663041
News::NNTPClient::SOCK1 command: XHDR Subject 663041
News::NNTPClient::SOCK1 result(221): Subject fields follow
News::NNTPClient::SOCK1 received 1 lines
663041 Re: dereferencing in perl
663041 Re: dereferencing in perl
Use of uninitialized value $d in concatenation (.) or string at 
client6.pl line 23, <SOCK1> line 7.

 ...

News::NNTPClient::SOCK1 command: QUIT
News::NNTPClient::SOCK1 result(205): .
$ cat client6.pl
#!/usr/bin/perl
use strict;
use warnings;
use Net::NNTP ();
require News::NNTPClient;

my $group = 'comp.lang.perl.misc';
my $server = 'News.Individual.NET';
my $USER = '';
my $PASS = '';

my $c = new News::NNTPClient($server);
$c->authinfo($USER, $PASS);
$c->debug(2);
my ($first, $last) = ($c->group($group));
print "$first   $last  \n";

   for (my $i = ($last -5); $i <= $last; $i++) {
       print "$i \n";
       my $subject = $c->xhdr("Subject", $i);
     print $_ for @{$subject};
my $d = print $_ for @{$subject};
print "$d\n";
   }
__END__

$

So, thx, marc, I'm able to move forward, but what I need is to have the 
line you gave me stored as a variable.
my $d = print $_ for @{$subject};
was my best attempt and looks like it may have worked, but it drew an 
exception and is an obvious kludge.
-- 
Uno


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

Date: Thu, 14 Apr 2011 14:21:52 -0600
From: Uno <Uno@example.invalid>
Subject: Re: dereferencing in perl
Message-Id: <90p3b1Fa8fU1@mid.individual.net>

On 04/14/2011 04:40 AM, Marc Girod wrote:

> Oh! And now that I started to read the man page, sorry to say that you
> shot yourself in the foot.
> The following works (in replacement for you whole loop):
>
> print $c->xhdr("", $last-5, $last);
>
> I.e. print does 'the right thing', as documented.
> You just prevent it from happening by forcing a scalar context,
> as you assign the result to a variable.
>
> Marc

Marc, I think the reason that I'm "forcing" scalar context is that I 
need the entire control to be something I can understand.  Hence the 
traditional for.

Again, I need a variable here, but beyond that I wanted to discuss this 
context, because, from my point of view, scalar context is forcing 
itself on me.

I thought this excerpt from cpan for News::NNTPClient might contain 
something relevant:

xhdr

     Fetch header for a range of articles. First argument is name of 
header to fetch. If omitted or blank, default to Message-ID. Second 
argument is start of article range. If omitted, defaults to 1. Third 
argument is end of range. If omitted, defaults to "". The second 
argument can also be a Message-ID.

     Returns headers as an array of lines terminated by the current EOL.

     In scalar context a reference to the array is returned instead of 
the array itself.

     Examples:

       # Fetch Message-ID of article 1.
       $c->xhdr();

       # Fetch Subject of article 1.
       $c->xhdr("Subject");

       # Fetch Subject of article 3345.
       $c->xhdr("Subject", 3345);

       # Fetch Subjects of articles 3345-9873
       $c->xhdr("Subject", 3345, 9873);

       # Fetch Message-ID of articles 3345-9873
       $c->xhdr("", 3345,9873);

       # Fetch Subject for article with Message-ID
       $c->xhdr("Subject", '<797t0g$25f10@foo.com>');
-- 
Uno


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

Date: Thu, 14 Apr 2011 16:19:09 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: dereferencing in perl
Message-Id: <4da764cd$0$9076$815e3792@news.qwest.net>

Uno wrote:
[...]
> So, thx, marc, I'm able to move forward, but what I need is to have the 
> line you gave me stored as a variable.
> my $d = print $_ for @{$subject};

No.

> was my best attempt and looks like it may have worked, but it drew an 
> exception and is an obvious kludge.

That's because it's wrong.

You either want to 'push' $subject onto an array you can use later,
and iterate over it, as needed, or possibly use join, if you want
to combine multiple elements of the @$subject into a scalar.

Most would probably stick with an array, since you probably want
some sort of order, later.

 >  for (my $i = ($last -5); $i <= $last; $i++) {

      ^^^^ That's pretty ugly.

my @combined;
for my $i ( $last-5 .. $last )
{
	push( @combined, $c->xhdr("Subject", $i) );
}

for my $item ( @$combined )
{
	for my $subitem ( @$item )
	{
		print ">>$subitem\n";
	}
}

For help with data structures, e.g. an AoA, read/study: perldoc perldsc


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

Date: Thu, 14 Apr 2011 17:54:08 -0600
From: Uno <Uno@example.invalid>
Subject: Re: dereferencing in perl
Message-Id: <90pfp1F1s2U1@mid.individual.net>

On 04/14/2011 03:19 PM, J. Gleixner wrote:
> Uno wrote:

> That's because it's wrong.
>
> You either want to 'push' $subject onto an array you can use later,
> and iterate over it, as needed, or possibly use join, if you want
> to combine multiple elements of the @$subject into a scalar.
>
> Most would probably stick with an array, since you probably want
> some sort of order, later.
>
>  > for (my $i = ($last -5); $i <= $last; $i++) {
>
> ^^^^ That's pretty ugly.
>
> my @combined;
> for my $i ( $last-5 .. $last )
> {
> push( @combined, $c->xhdr("Subject", $i) );
> }
>
> for my $item ( @$combined )
> {
> for my $subitem ( @$item )
> {
> print ">>$subitem\n";
> }
> }
>
> For help with data structures, e.g. an AoA, read/study: perldoc perldsc

$ perl client6.pl

 ...
News::NNTPClient::SOCK1 received 1 lines
 >>663047 Re: dereferencing in perl

 >>663048 Re: dereferencing in perl

 >>663049 Re: dereferencing in perl

 >>663050 C callbacks returning a value

 >>663051 FAQ 8.48 How do I add the directory my program lives in to the 
module/library search path?

 >>663052 Re: C callbacks returning a value

News::NNTPClient::SOCK1 command: QUIT
News::NNTPClient::SOCK1 result(205): .
$ cat client6.pl
#!/usr/bin/perl
use strict;
use warnings;
use Net::NNTP ();
require News::NNTPClient;

my $group = 'comp.lang.perl.misc';
my $server = 'News.Individual.NET';
my $USER = '';
my $PASS = '';

my $c = new News::NNTPClient($server);
$c->authinfo($USER, $PASS);
$c->debug(2);
my @combined;
my ($first, $last) = ($c->group($group));
for my $i ( $last-5 .. $last )
{
     push( @combined, $c->xhdr("Subject", $i) );
}

for my $item ( @combined )
{
     for my $subitem ( $item )
     {
         print ">>$subitem\n";
     }
}
__END__

Alright, thx J, this is useful to me, even if it's not the direction I 
want to go.  It occurs to me after reading perldoc perldsc that arrays 
of arrays are not the direction I want to go, that indeed I want to 
force there to be scalar context.

So I'm back at the beginning again.

       my $subject = $c->xhdr("", $i);
      print "$subject\n";

Is there no way shorter way to what I intend other than having to create 
an array and populate it with push?  I frankly still don't understand 
why this original formulation only shows the references. and not the 
contents.
-- 
Uno



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

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:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 3351
***************************************


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