[30988] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2233 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Feb 24 21:09:44 2009

Date: Tue, 24 Feb 2009 18:09:11 -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           Tue, 24 Feb 2009     Volume: 11 Number: 2233

Today's topics:
    Re: debugging on stdout <no_th@nks.org>
    Re: debugging on stdout <ben@morrow.me.uk>
        Email Purge <tod@asgweb.net>
    Re: Email Purge <noreply@gunnar.cc>
    Re: Email Purge <tim@burlyhost.com>
    Re: Email Purge <tadmc@seesig.invalid>
    Re: Email Purge <noreply@gunnar.cc>
    Re: Email Purge <tim@burlyhost.com>
        obtaining permission <larry@example.invalid>
    Re: obtaining permission <tim@burlyhost.com>
    Re: Perl Peeves (David Combs)
    Re: Strange behavior with MIME::Lite <smallpond@juno.com>
    Re: Strange behavior with MIME::Lite <edMbj@aes-intl.com>
        Strategy of ++/-- operator in Lists in perl sln@netherlands.com
    Re: utf8 and chomp <jfeit@ics.muni.cz>
    Re: utf8 and chomp <ben@morrow.me.uk>
    Re: XML Simple force array <thesnake_123@-NO-S_P_A_M-hotmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 24 Feb 2009 23:40:49 GMT
From: mike <no_th@nks.org>
Subject: Re: debugging on stdout
Message-Id: <m38wnvz9mn.fsf@localhost.localdomain>


On Tue Feb 24, I was peacefully napping until Tim Greer said:

> mike wrote:
> 
> > 
> > I have a perl script which talks to mplayer and stepping through it in
> > emacs' cperl-db it executes flawlessly; but running "./script video"
> > it does everything except one crucial step and I'm having a hard time
> > visualizing what could be the matter.
> > 
> > so is there a way to invoke the perl script such as can be done in
> > bash or csh (csh -xvf ./script.csh) which would allow me to see what's
> > going on at full speed?
> > 
> > thanks.
> > 
> 
> You're going to need to post the relevant code, and provide some details
> about the issue, instead of just posting an example of how you run it. 
> Also, what is the crucial step it's missing?  What are you trying to
> view/see what's "going on" (what is that?)


   $video = $ARGV[0];
   $vid_resume = vid_position_file($video);
   if (-e $vid_resume) {
      $pid = open2(*reader, *writer, "gmplayer-bin -slave -quiet -ss `cat $vid_resume` $video");


$vid_resume returns a valid filename (with valid contents, in this case
"255.1" and nothing more) and its contents are meant to skip to a specific
position in the video when it starts playing.

a) in emacs cperl-db buffer, I load "./script.pl /path/to/video/x" then
"n" line by line and it kicks off mplayer running $video and advanced to
position $vid_resume (iow everything as I expect). 

b) "% ./script.pl /path/to/video/x" brings up mplayer stopped, with $video
loaded (I hit "play" and the expected video is there and running, and
incidentally starts at time 0).

so two scenarios, same script, same video, same $vid_resume file but
different outcomes.

I looked through some of what Ben refers to but tbh I'm going in circles
and aren't getting any closer to understanding what's the matter.

ideally (in b)) I'd like to see what mplayer is saying (I removed "-quiet"
but ostensibly because of how it's being run its output seems lost since I
don't see it in the terminal window).


-- 


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

Date: Wed, 25 Feb 2009 00:53:41 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: debugging on stdout
Message-Id: <ldhe76-1cp.ln1@osiris.mauzo.dyndns.org>


Quoth mike <no_th@nks.org>:
> 
> On Tue Feb 24, I was peacefully napping until Tim Greer said:
> 
> > mike wrote:
> > > 
> > > I have a perl script which talks to mplayer and stepping through it in
> > > emacs' cperl-db it executes flawlessly; but running "./script video"
> > > it does everything except one crucial step and I'm having a hard time
> > > visualizing what could be the matter.
> > > 
> > > so is there a way to invoke the perl script such as can be done in
> > > bash or csh (csh -xvf ./script.csh) which would allow me to see what's
> > > going on at full speed?
> > 
> > You're going to need to post the relevant code, and provide some details
> > about the issue, instead of just posting an example of how you run it. 
> > Also, what is the crucial step it's missing?  What are you trying to
> > view/see what's "going on" (what is that?)
> 
> 
>    $video = $ARGV[0];
>    $vid_resume = vid_position_file($video);
>    if (-e $vid_resume) {
>       $pid = open2(*reader, *writer, "gmplayer-bin -slave -quiet -ss
> `cat $vid_resume` $video");

First, why are you using cat(1)? Perl is perfectly capable of reading a
file: see the File::Slurp module for a trivial way to do it. If
vid_position_file is creating the temporary file, I'd change it so it
returns the resume position as a string instead.

You are not checking the return value of open2, and you are apparently
not using strictures. You need to fix both of these if you expect to get
much useful help here.

I would also recommend avoiding global filehandles. Due to an
unfortunate design decision this is not terribly easy with IPC::Open2:
you need something like

    use Symbol qw/geniosym/;

    my ($READER, $WRITER) = (geniosym, geniosym);

    my $pid = open2($READER, $WRITER, "...") or die "...";

I would avoid using Open2 directly in favour of IPC::Run, which has a
much cleaner interface and is more portable to systems like Win32.

> $vid_resume returns a valid filename (with valid contents, in this case
> "255.1" and nothing more) and its contents are meant to skip to a specific
> position in the video when it starts playing.
> 
> a) in emacs cperl-db buffer, I load "./script.pl /path/to/video/x" then
> "n" line by line and it kicks off mplayer running $video and advanced to
> position $vid_resume (iow everything as I expect). 
> 
> b) "% ./script.pl /path/to/video/x" brings up mplayer stopped, with $video
> loaded (I hit "play" and the expected video is there and running, and
> incidentally starts at time 0).

This feels like a race condition of some sort, but I can't see where it
might be. What does your script do next? Can you use ps to examine what
arguments actually get passed to the gmplayer-bin process? What happens
if you take the temporary file out of the equation and just supply a
literal string to mplayer? What happens if you use ordinary mplayer
rather than gmplayer-bin? Can you give us a minimal *complete* script
that reliably fails on your machine? Is there anything else different
between the two environments: different environment vars, different
shells, &c.? Does the program still succeed if you single-step in the
debugger (run it with perl -d) rather than using Emacs?

> ideally (in b)) I'd like to see what mplayer is saying (I removed "-quiet"
> but ostensibly because of how it's being run its output seems lost since I
> don't see it in the terminal window).

Most of mplayer's verbose output goes to stdout rather than stderr,
which means it will show up on the *writer handle. You could try adding
something like '| tee /dev/stderr' or '| tee /dev/tty' to the
commandline as a crude way of getting some output. You could also try
using a one-way piped open or a simple system rather than open2, to see
if that makes a difference.

Ben



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

Date: Tue, 24 Feb 2009 13:23:57 -0800 (PST)
From: perl <tod@asgweb.net>
Subject: Email Purge
Message-Id: <60a1e63d-f214-4c5c-a0b3-e7053ee92a39@x9g2000yqk.googlegroups.com>

Cheers and thanks in advance for you help. I have a routine intended
to purge duplicate emails from a list. The code below is just. I'm
sure it needs to be in a %hash but I'm lost. Any help is appreciated.
sub purge
{
open (LIST, "$list") or error("$list  email purge  1");
	while (my $line = <LIST>)
	{
	@emails = split(/\r?\n|\r/, $line);
	@emails2 =  @emails;
	}
close (LIST);
	foreach $email(@emails)
	{

		foreach $email2(@emails2)
		{
			if($email ne $email2)
			{ $newemail .="$email\n"; }
			else{ $purgecnt++; }
		}
	}
open (LIST, ">$list") or error("$list  email purge 2");
flock(LIST, LOCK_EX);
print LIST $newemail;
close (LIST);
&success("$list has been purged of $purgecnt duplicates");
}


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

Date: Tue, 24 Feb 2009 23:23:08 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Email Purge
Message-Id: <70jaa4F9qsifU1@mid.individual.net>

perl wrote:
> I have a routine intended to purge duplicate emails from a list.

This OP posted to the beginners-cgi list, then to the beginners list.

http://www.mail-archive.com/beginners@perl.org/msg99536.html

He does not respond to the comments and suggestions, and should 
consequently be ignored.

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


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

Date: Tue, 24 Feb 2009 14:34:12 -0800
From: Tim Greer <tim@burlyhost.com>
Subject: Re: Email Purge
Message-Id: <GB_ol.28889$pX4.22810@newsfe08.iad>

perl wrote:

> Cheers and thanks in advance for you help. I have a routine intended
> to purge duplicate emails from a list. The code below is just. I'm
> sure it needs to be in a %hash but I'm lost. Any help is appreciated.
> sub purge
> {
> open (LIST, "$list") or error("$list  email purge  1");

Maybe use the preferred method and use $! to tell you why it failed:

open(my $filelist, '<', $list) or error ("Can't open $list $!");

> while (my $line = <LIST>)
> {
> @emails = split(/\r?\n|\r/, $line);
> @emails2 =  @emails;

Why not just use @emails?  Why use the same array twice?

<snip>

Nevermind, I see what you're trying to do.

Yes, use a hash to see if the address already exists.

my %emails = ();
open(my $list, '<', 'filename.here')
 or error("Open failed for filename.here $!");
while (<$list>) {
 chomp; # maybe?
 $emails{$_} = 1 if ! exists $emails{$_};
}
close($list) or warn $!;

Now, %emails contains (hopefully unique) email addresses.

I assume emails are one per line, which means you step through each
line, which you were doing already with "while" (so trying to split on
new lines is redundant).

The above is a very basic example, though I'd also suggest ignoring the
case on matches, so I'd set $_ to lc and compare against the existing
key in lc (lower case) as well.  That's about as simple as it can get.

PS: The above is untested, I literally have 60 seconds from the start of
this to heading out of the office, so it's an example only just off the
top of my head.
-- 
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!


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

Date: Tue, 24 Feb 2009 16:51:19 -0600
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Email Purge
Message-Id: <slrngq8uf7.16e.tadmc@tadmc30.sbcglobal.net>

Gunnar Hjalmarsson <noreply@gunnar.cc> wrote:
> perl wrote:
>> I have a routine intended to purge duplicate emails from a list.
>
> This OP posted to the beginners-cgi list, then to the beginners list.
>
> http://www.mail-archive.com/beginners@perl.org/msg99536.html
>
> He does not respond to the comments and suggestions, and should 
> consequently be ignored.


I've had him killfiled for 5 years, but he changed his posting address
this time (it used to be todd@asgweb.net).

In:
http://groups.google.com/groups/search?as_umsgid=400ED975.E1EB5DFD%40asgweb.net

he said:

    Thanks in advance for any help.
    (please NO rectal discharge) 

Which I took as a big honking red flag that his posts could be ignored.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Wed, 25 Feb 2009 00:20:42 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Email Purge
Message-Id: <70jdlpF9negsU1@mid.individual.net>

Tim Greer wrote:
> 
> while (<$list>) {
>  chomp; # maybe?
>  $emails{$_} = 1 if ! exists $emails{$_};
> }

Why that condition? It's a hash. ;-)

I'd write:

     %emails = map { chomp; $_, 1 } <$list>;

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


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

Date: Tue, 24 Feb 2009 16:30:45 -0800
From: Tim Greer <tim@burlyhost.com>
Subject: Re: Email Purge
Message-Id: <Xi0pl.17894$2O4.1364@newsfe03.iad>

Gunnar Hjalmarsson wrote:

> Tim Greer wrote:
>> 
>> while (<$list>) {
>>  chomp; # maybe?
>>  $emails{$_} = 1 if ! exists $emails{$_};
>> }
> 
> Why that condition? It's a hash. ;-)
> 
> I'd write:
> 
>      %emails = map { chomp; $_, 1 } <$list>;
> 

I was just going with the same basic style the user originally posted,
which should (in theory) help them better understand (instead of
explaining map or something else to them.  I'd do a lot of things
differently, too.
-- 
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!


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

Date: Tue, 24 Feb 2009 13:33:46 -0700
From: Larry Gates <larry@example.invalid>
Subject: obtaining permission
Message-Id: <lxau1es75wez$.wg4z5bls1trk.dlg@40tude.net>


I made a recent original post and received the following response from a
clp.misc regular:

Larry Gates wrote:
>   use strict;
>   use warnings;
>   use LWP::Simple;
> 
>   # load the complete content of the url in question
>   # via LWP::Simple::get(...)
>   
> my $t = get 'http://www.fourmilab.ch/cgi-bin/
> Yoursky?z=1&lat=35.0836&ns=North&lon=106.651&ew=West';
> 
> print "t is $t";
> 
> #  perl scraper2.pl
> 
> C:\MinGW\source>perl scraper2.pl
> Use of uninitialized value in concatenation (.) or string at scraper2.pl
> line 14
> .
> t is
> C:\MinGW\source>
> 
> I would have expected $t to have the whole page.  What gives?

I don't know. It works for me.

However, the site disallows via robots.txt automatic access to their 
cgi-bin, so whatever you are attempting to do, you'd better stop doing it.

#end excerpt

I thought I would be clever and avoid such issues by chooosing the most
small c catholic thing I stumbled upon and ambled instead on Tad's
response:

However, what you appear to be doing violates Google's ToS:

    http://groups.google.com/intl/en/googlegroups/terms_of_service3.html

    you agree that when using the Service, you will not:
    ...
    use any robot, spider, site search/retrieval application, or other 
    device to retrieve or index any portion of the Service or collect 
    information about users for any unauthorized purpose;

# end second excerpt

First of all, I don't think of extensions of my keyboard as a robot.
Robots don't consist of 2 sprained hands and no spares.

Secondly, I send these sites many fewer keystrokes with perl than I do with
my browser.

Thirdly, I've got all the time in the world to obtain explicit legal
permission to do what I want with either of these entities.

How do I do this?
-- 
larry gates

Any false value is gonna be fairly boring in Perl, mathematicians
notwithstanding.
             -- Larry Wall in <199707300650.XAA05515@wall.org>

-- 
larry gates

That's a valid argument.  I just don't think it's valid enough.  :-)
             -- Larry Wall in <199804150050.RAA08093@wall.org>


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

Date: Tue, 24 Feb 2009 14:25:24 -0800
From: Tim Greer <tim@burlyhost.com>
Subject: Re: obtaining permission
Message-Id: <pt_ol.55238$2h5.23333@newsfe11.iad>

Larry Gates wrote:

> 
> I made a recent original post and received the following response from
> a clp.misc regular:
> 
> Larry Gates wrote:
>>   use strict;
>>   use warnings;
>>   use LWP::Simple;
>> 
>>   # load the complete content of the url in question
>>   # via LWP::Simple::get(...)
>>   
>> my $t = get 'http://www.fourmilab.ch/cgi-bin/
>> Yoursky?z=1&lat=35.0836&ns=North&lon=106.651&ew=West';
>> 
>> print "t is $t";
>> 
>> #  perl scraper2.pl
>> 
>> C:\MinGW\source>perl scraper2.pl
>> Use of uninitialized value in concatenation (.) or string at
>> scraper2.pl line 14
>> .
>> t is
>> C:\MinGW\source>
>> 
>> I would have expected $t to have the whole page.  What gives?
> 
> I don't know. It works for me.
> 
> However, the site disallows via robots.txt automatic access to their
> cgi-bin, so whatever you are attempting to do, you'd better stop doing
> it.
> 
> #end excerpt
> 
> I thought I would be clever and avoid such issues by chooosing the
> most small c catholic thing I stumbled upon and ambled instead on
> Tad's response:
> 
> However, what you appear to be doing violates Google's ToS:
> 
>    
http://groups.google.com/intl/en/googlegroups/terms_of_service3.html
> 
>     you agree that when using the Service, you will not:
>     ...
>     use any robot, spider, site search/retrieval application, or other
>     device to retrieve or index any portion of the Service or collect
>     information about users for any unauthorized purpose;
> 
> # end second excerpt
> 
> First of all, I don't think of extensions of my keyboard as a robot.
> Robots don't consist of 2 sprained hands and no spares.
> 
> Secondly, I send these sites many fewer keystrokes with perl than I do
> with my browser.
> 
> Thirdly, I've got all the time in the world to obtain explicit legal
> permission to do what I want with either of these entities.
> 
> How do I do this?

If you automate something to obtain data from a remote site
infrequently, you are likely fine, if it's just a replacement for using
your browser and saving a file.  If you plan to do it frequently or
have a site that enacts the connection/download each time someone
visits a script/area of your site, then it could be (or will be) more
frequent, even creating a load on the remote site.  So, ask that site
admin or webmaster contact.  I don't understand the question about how
you get permission, other than to suggest you contact the appropriate
contact person at said site/company and ask.  Another issue is if they
block any non common user agents in an effort to prevent automated
scripts.  There could be a number of reasons beyond that about why they
forbid it on their site.
-- 
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!


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

Date: Tue, 24 Feb 2009 22:34:12 +0000 (UTC)
From: dkcombs@panix.com (David Combs)
Subject: Re: Perl Peeves
Message-Id: <go1sl4$90n$1@reader1.panix.com>

In article <gllp4p$psn$1@reader1.panix.com>,
Tim McDaniel <tmcd@panix.com> wrote:
>Just venting.
>
>
>(2) That had effect but wasn't documented in "man perlop" in 5.005,
>the version on my ISP's shell accounts.

What luck in getting panix to fix that?

David  (also there)






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

Date: Tue, 24 Feb 2009 11:20:51 -0800 (PST)
From: smallpond <smallpond@juno.com>
Subject: Re: Strange behavior with MIME::Lite
Message-Id: <4582df85-33df-4012-98c0-12036c2966fa@l22g2000vba.googlegroups.com>

On Feb 24, 1:44 pm, Ed Jay <ed...@aes-intl.com> wrote:
> I have an html file residing on my server that I want to attach as an
> email attachment. I'm using MIME::Lite as follows:
>
> use MIME::Lite;
> use Net::SMTP;
>
> $info_email = "info\@myDomain\.com";
> $date2 = '022109';
> my $mail_host = 'mail.myDomain.com';
> $reportPath = "/home/wwwbrea/public_html/reports/";
> $email = '...@edsDomain.com';
> $subject = 'Information You Requested';
>
> # Create container
>         $msg = MIME::Lite->new (
>         From => $info_email,
>         To => $email,
>         Subject => $subject,
>         Type =>'multipart/mixed'
>         ) or die "Error creating multipart container: $!\n";
>
> ### email body text message
>         $msg->attach (
>         Type => 'TEXT',
>         Data => $message_body
>         ) or die "Error adding the text message part: $!\n";
>
> ### Add attachment
>         $ptReportFile = 'TestProcess4'.$date2.'.html';
>         $my_file_path = './'.$ptReportFile;
>         $msg->attach (
>         Type => 'text/html',
>         Path => $my_file_path,
>         Filename => $ptReportFile,
>         Disposition => 'attachment'
>         ) or die "Error adding $ptReport: $!\n";
>
>         MIME::Lite->send('smtp', $mail_host, Timeout=>60);
>         $msg->send;
>
> In the 'add attachment' section, if the mime type is text/html or
> txt/plain, this script doesn't send. But, if I change the mime type to
> 'application/pdf' or any other 'application/XX,' including a fake mime
> type as 'application/html,' the script works. WTF???
>

The send method "Returns whatever the mail-handling routine returns:
this
should be true on success, false/exception on error".
What does send return and what is in errno?



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

Date: Tue, 24 Feb 2009 11:46:17 -0800
From: Ed Jay <edMbj@aes-intl.com>
Subject: Re: Strange behavior with MIME::Lite
Message-Id: <6ij8q4hcpsu4g5ei1u26njpp3cea5h2fsg@4ax.com>

smallpond wrote:

>On Feb 24, 1:44 pm, Ed Jay <ed...@aes-intl.com> wrote:
>> I have an html file residing on my server that I want to attach as an
>> email attachment. I'm using MIME::Lite as follows:
>>
>> use MIME::Lite;
>> use Net::SMTP;
>>
>> $info_email = "info\@myDomain\.com";
>> $date2 = '022109';
>> my $mail_host = 'mail.myDomain.com';
>> $reportPath = "/home/wwwbrea/public_html/reports/";
>> $email = '...@edsDomain.com';
>> $subject = 'Information You Requested';
>>
>> # Create container
>>         $msg = MIME::Lite->new (
>>         From => $info_email,
>>         To => $email,
>>         Subject => $subject,
>>         Type =>'multipart/mixed'
>>         ) or die "Error creating multipart container: $!\n";
>>
>> ### email body text message
>>         $msg->attach (
>>         Type => 'TEXT',
>>         Data => $message_body
>>         ) or die "Error adding the text message part: $!\n";
>>
>> ### Add attachment
>>         $ptReportFile = 'TestProcess4'.$date2.'.html';
>>         $my_file_path = './'.$ptReportFile;
>>         $msg->attach (
>>         Type => 'text/html',
>>         Path => $my_file_path,
>>         Filename => $ptReportFile,
>>         Disposition => 'attachment'
>>         ) or die "Error adding $ptReport: $!\n";
>>
>>         MIME::Lite->send('smtp', $mail_host, Timeout=>60);
>>         $msg->send;
>>
>> In the 'add attachment' section, if the mime type is text/html or
>> txt/plain, this script doesn't send. But, if I change the mime type to
>> 'application/pdf' or any other 'application/XX,' including a fake mime
>> type as 'application/html,' the script works. WTF???
>>
>
>The send method "Returns whatever the mail-handling routine returns:
>this should be true on success, false/exception on error".
>What does send return and what is in errno?

To capture the error code, I changed $msg->send to
    eval { $msg->send };
    die "MIME::Lite->send failed: $@\n" if $@;
And now it works.

-- 
Ed Jay (remove 'M' to reply by email)

Win the War Against Breast Cancer.
Knowing the facts could save your life. 
http://www.breastthermography.info


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

Date: Tue, 24 Feb 2009 22:20:50 GMT
From: sln@netherlands.com
Subject: Strategy of ++/-- operator in Lists in perl
Message-Id: <lar8q4h1r4vk147chu44e8re3364d9op19@4ax.com>

Below is a Perl code sample of using increment operator on variables in lists.
Below that is a C++ equivalent.

The strategies are different for pre/post-increment as far as the list block,
as are the outcomes.

Can anybody explain the logic in this? To me, they both look incorrect. C++
possibly a little more so.

-sln

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

## some_lists.pl
use strict;
use warnings;

my ($p,@ar);

print "\n";
$p = 0;
printf ("%d %d %d %d %d %d \n",      $p++, ++$p, $p++, ++$p, $p++, ++$p);
#                                     +1    +1    +1    +1    +1    +1
#  going this way --->                 0           2           4
#                                            6           6           6     <--- going this way
$p = 0;
printf ("%d %d %d %d %d %d \n",      $p++, $p++, ++$p, ++$p, $p++, $p++);
#                                     +1    +1    +1    +1    +1    +1
#  going this way --->                 0     1                 4     5     <--- going this way
#                                                  6     6
$p = 0;
printf ("%d %d %d %d %d %d %d %d\n", $p++, $p++, ++$p, ++$p, $p++, $p++, ++$p, ++$p);
#                                     +1    +1    +1    +1    +1    +1    +1    +1
#  going this way --->                 0     1                 4     5
#                                                  8     8                 8     8   <--- going this way
$p = 0;
printf ("%d %d %d %d %d\n",          $p++, ++$p, $p++, $p++, ++$p);
#                                     +1    +1    +1    +1    +1   
#  going this way --->                 0           2     3
#                                            5                 5     <--- going this way
$p = 0;
@ar = ( $p++, $p++, ++$p, ++$p, $p++, $p++, ++$p, ++$p ); print "@ar\n";
#         0     1                 4     5
#                     8     8                 8     8

print "\n";
$p = 0;
@ar = ( $p++, $p++, ++$p, $p*=$p, $p++, $p=0, ++$p, ++$p ); print "@ar\n";
$p = 0;
@ar = ( $p++, $p++, ++$p, $p*=$p, $p++, $p++, ++$p, ++$p ); print "@ar\n";


__END__


/** C Equivalent **/


int p = 0;
printf ("%d %d %d %d %d %d \n",      p++, ++p, p++, ++p, p++, ++p);
//                                         +1        +1        +1
//                                    3         2         1           <--- going this way
//   going this way --->                    3         3         3

p = 0;
printf ("%d %d %d %d %d %d \n",      p++, p++, ++p, ++p, p++, p++);
//                                              +1   +1
//                                    2    2              0    0      <--- going this way
//   going this way --->                         2    2

p = 0;
printf ("%d %d %d %d %d %d %d %d\n", p++, p++, ++p, ++p, p++, p++, ++p, ++p);
//                                              +1   +1             +1   +1
//                                     4   4              2    2                <--- going this way
//   going this way --->                         4    4              4    4

p = 0;
printf ("%d %d %d %d %d\n",          p++, ++p, p++, p++, ++p);
//                                         +1             +1
//                                    2         1    1            <--- going this way
//   going this way --->                    2              2
//




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

Date: Tue, 24 Feb 2009 20:08:00 GMT
From: Josef Feit <jfeit@ics.muni.cz>
Subject: Re: utf8 and chomp
Message-Id: <KFL6Bq.7q1@news.muni.cz>

Marc Lucksch napsal(a):
> Eric Pozharski schrieb:
>> I've just gone through your original script with debugger, and found out
>> that after C<$line = <>;> I<$line> is pure byte string.  And then after
>> C<chomp $line;> it automagically decodes into utf8 character(!) string.
>> Should I keep on explaining?  (No, no spoiler this time.)
> 
> Ok now I am confused, do please explain.
> 
> Marc "Maluku" Lucksch

----

Please spoil us...   :-)

Yes, in the docs (encoding) is:
Sets the script encoding to I<ENCNAME>.  And unless ${^UNICODE}
exists and non-zero, PerlIO layers of STDIN and STDOUT are set to
":encoding(I<ENCNAME>)".

Note that STDERR WILL NOT be changed.

Also note that non-STD file handles remain unaffected.  Use C<use
open> or C<binmode> to change layers of those.

---

I tried to use (from Encode):
print "UTFline: ",  utf8::is_utf8($line), "\n";
print "UTFlinech: ",  utf8::is_utf8($linech), "\n";

and really the $linech is utf8, the $line not.

Combination of

use encoding 'utf-8';
use open IO => ':encoding(utf8)';

solves the problem, thank you all.

---
But still:
1. why chomp changes the string to utf8 as side effect?
2. can I tell the <> is utf8 if it is not STDIN?
   (I cannot figure out the syntax - OK, getting the file
   name through @ARGV should be possible).


Thank you
Josef







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

Date: Tue, 24 Feb 2009 21:42:24 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: utf8 and chomp
Message-Id: <076e76-hbo.ln1@osiris.mauzo.dyndns.org>


Quoth Josef Feit <jfeit@ics.muni.cz>:
> But still:
> 1. why chomp changes the string to utf8 as side effect?

The relevant code is

    if (PL_encoding) {
	if (!SvUTF8(sv)) {
	/* XXX, here sv is utf8-ized as a side-effect!
	   If encoding.pm is used properly, almost string-generating
	   operations, including literal strings, chr(), input data, etc.
	   should have been utf8-ized already, right?
	*/
	    sv_recode_to_utf8(sv, PL_encoding);
	}
    }

from doop.c:Perl_do_chomp. It was added as part of a fix for
http://rt.perl.org/rt3/Public/Bug/Display.html?id=24888 , and it means
that if 'use encoding' is in effect chomp on a byte string will upgrade
the string to UTF8 using the specified encoding. I can't follow the
logic behind this, myself. Note that this does *not* happen if 'use
utf8' is in effect.

> 2. can I tell the <> is utf8 if it is not STDIN?
>    (I cannot figure out the syntax - OK, getting the file
>    name through @ARGV should be possible).

Not really. It is possible to binmode the ARGV filehandle, but whenever
it is re-opened on a new file it loses the layers. Since it's not opened
until the first line is read, you cannot arrange for it to be in
utf8-mode at that point, and the same applies each time it opens a new
file. Personally I'd consider this a bug.

Ben



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

Date: Wed, 25 Feb 2009 00:06:11 GMT
From: "-Brad-" <thesnake_123@-NO-S_P_A_M-hotmail.com>
Subject: Re: XML Simple force array
Message-Id: <TX%ol.23285$cu.10471@news-server.bigpond.net.au>

Hi all,

Thanks you very much for your help,
Usging KeyAttr => [], has now given me an array!!!

Cheers

<xhoster@gmail.com> wrote in message 
news:20090223113333.442$Kr@newsreader.com...
"-Brad-" <thesnake_123@-NO-S_P_A_M-hotmail.com> wrote:
> Hi all,
>
> I have an xml file that looks like :
>
> <control_file name="XXXX_99.ctl">
>     <files_ps count="2">
>         <file seq="1" name="file1.gz" size="1107045" />
>         <file seq="2" name="file2.gz" size="1107045" />
>     </files_ps>
> </control_file>
>
> I would like to be able to loop through all the child elements under
> files_ps, and print out their attribute values.
> I was planning on using forcearray on the 'file' node so I can loop
> through all the array elements, but I cant seem to get it to work.

ForceArray only has an effect when there is exactly one element.  If there
is more than one element (like your 'file') then it is already put into
an array.  If less than one, there is nothing there in the first place.

But after it is put into an array, the action of KeyAttr might re-arrange
it into a hash.  The default value for KeyAttr is ['name', 'key', 'id'],
and since all of your "file" elements have a attribute named 'name', it
gets rearranged into a hash using 'name' as the key.

So what you need to do is add KeyAttr=>undef.

Your current example doesn't need the ForceArray, but if you every expect
to get a file_ps that has only one file, then you should keep the
ForceArray as well.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact. 




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

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 2233
***************************************


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