[30103] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1346 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Mar 9 16:09:43 2008

Date: Sun, 9 Mar 2008 13:09:09 -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           Sun, 9 Mar 2008     Volume: 11 Number: 1346

Today's topics:
    Re: FAQ 5.3 How do I count the number of lines in a fil <hjp-usenet2@hjp.at>
        Getting permissions using Win32::TieRegistry <XXjbhuntxx@white-star.com>
    Re: list of day/month names in all locales <myicq2@3gmx.net4>
    Re: OK I want read: IP Address properties of user's Int <uri@stemsystems.com>
    Re: Perl pattern extraction <tadmc@seesig.invalid>
        Setting chmod <John.Smith@invalid.com>
    Re: Setting chmod <joost@zeekat.nl>
    Re: Setting chmod <noreply@gunnar.cc>
    Re: TieRegistry Overlapped Error <XXjbhuntxx@white-star.com>
    Re: try to use "locale" with german and coepage 437 (do <jm@nospam.fr>
        Use of "return" in place of "last" (newbie question)? <mike@nospam.co.uk>
    Re: Use of "return" in place of "last" (newbie question <tadmc@seesig.invalid>
    Re: Use of "return" in place of "last" (newbie question <mike@nospam.co.uk>
    Re: Use of "return" in place of "last" (newbie question <jurgenex@hotmail.com>
    Re: Use of "return" in place of "last" (newbie question <joost@zeekat.nl>
    Re: Use of "return" in place of "last" (newbie question <mike@nospam.co.uk>
    Re: Use of "return" in place of "last" (newbie question <joost@zeekat.nl>
    Re: Use of "return" in place of "last" (newbie question <jurgenex@hotmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sun, 9 Mar 2008 14:28:08 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: FAQ 5.3 How do I count the number of lines in a file?
Message-Id: <slrnft7pf8.9iv.hjp-usenet2@hrunkner.hjp.at>

On 2008-03-07 23:01, jm <jm@nospam.fr> wrote:
> brian d foy a écrit :
>> In article <47d06c07$0$21142$7a628cd7@news.club-internet.fr>, jm
>> <jm@nospam.fr> wrote:
>>> PerlFAQ Server a écrit :
>>>
>>>> 5.3: How do I count the number of lines in a file?
>> 
>>> How does this code handle other unicode new line codages, such as 0x85,
>>> 0x0d 0x0a?
>> 
>> If you have a different idea of the human concept of "line", you'll
>> have to adjust the code to have the right line ending (and probably not
>> use tr///).

For perl, a newline is "\n". Conversion to and from some file encoding
should be done with the appropriate IO layer.


> I do not remember where I read it, but
> specificity of computers standards is there are so many.
>
>
> I assume the following can be used:
>
> «A newline sequence is defined to be any of the following:
>
> \u000A | \u000B | \u000C | \u000D | \u0085 | \u2028 | \u2029 |
> \u000D\u000A »

If all of these were just "newline sequences", they should be turned
into "\n" by the crlf conversion of the encoding(utf-*) layers. But they
aren't. For example, \u000C signifies not just a new line, but a new page.


> However it is not always true, because with some encodings (cp850 for
> instance), 0x85 is plain character.

cp850 doesn't have anything to do with unicode, so that doesn't seem
relevant.

	hp



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

Date: Sun, 09 Mar 2008 19:20:37 GMT
From: Cosmic Cruizer <XXjbhuntxx@white-star.com>
Subject: Getting permissions using Win32::TieRegistry
Message-Id: <Xns9A5C7364DCDFccruizermydejacom@207.115.33.102>

I can get most of the registry info I need by using Win32::TieRegistry, but 
I cannot figure out how to get the permissions for the keys. Is it possbile 
to get the key permissions using TieRegistry? If not, is there a different 
way to use Perl to get the registry key permissions?

Thanks


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

Date: 09 Mar 2008 13:25:47 GMT
From: myicq <myicq2@3gmx.net4>
Subject: Re: list of day/month names in all locales
Message-Id: <Xns9A5C92C6CBB63myicqgmxnet@212.242.40.162>

Ben Morrow <ben@morrow.me.uk> wrote in news:ibmca5-mb92.ln1
@osiris.mauzo.dyndns.org:

> Something like
> 
>     my @locales = qx/locale -a/;
> 
> is probably your best bet.

Except, of course.. that I am using Windows. So that command
is not available.

I also need (a bit) of help on how to extract the month /day names
from each locale.

Sorry for this newbie question.

FYI: 

c:\> perl -V:d_setlocale
d_setlocale='define';

Version 5.8.8 (Activestate)


Thanks.


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

Date: Sun, 09 Mar 2008 13:27:48 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: OK I want read: IP Address properties of user's Internet Connection
Message-Id: <x7ve3w58ob.fsf@mail.sysarch.com>

>>>>> "JS" == Joe Smith <joe@inwap.com> writes:

  JS> Marthan wrote:
  >> OK I want read: IP Address properties of user's Internet Connection

  JS> What kind of internet connection?

  JS> There are different answers, depending on how the connection comes
  JS> in to your server.  HTTP, FTP, TELNET, SSH, BitTorrent, Skype,
  JS> etc, etc, etc.

actually from the tcp/ip perspective those are all the exact same
thing. sockets are sockets and you can get all the IP info you want
about a peer socket. the difference you claim is in the data protocol
that runs on top of the socket layer (including any encryption).

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Architecture, Development, Training, Support, Code Review  ------
-----------  Search or Offer Perl Jobs  ----- http://jobs.perl.org  ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Sat, 8 Mar 2008 19:56:51 -0600
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Perl pattern extraction
Message-Id: <slrnft6gv3.osq.tadmc@tadmc30.sbcglobal.net>

Chris Mattern <syscjm@sumire.gwu.edu> wrote:
> On 2008-03-08, Deepan - M.Sc(SE) - 03MW06 <deepan.17@gmail.com> wrote:
>> Hi,
>>
>>      my $url = "/pages-cell.net/deepan/sony/";
>>
>>      if($url =~ m/\/(.*)\//g)
>>      {
>>     		my @result = $1;
>>             	return @result;
>>      }
>>
>> What i need is that i should be able to get anything that is between /
>> and /. Here i should be able to get pages-cell.net,deepan,sony into
>> @result but something is wrong somewhere. Please help me to solve
>> this?
>>
> No.  m\/(.*)\//g only returns one string; m *always* only returns one
> string.  


No it doesn't.

The m// operator always returns one string when in scalar context.

(in fact, *every* operator can return only one thing in scalar context.)

m// in list context can potentially evaluate to more than one string.

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

$_ = 'foo bar';

my @matches = m/(\w+)\s+(\w+)/;

foreach (@matches) {
   print "$_\n";
}
---------------


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


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

Date: Sun, 09 Mar 2008 18:26:35 +0200
From: John <John.Smith@invalid.com>
Subject: Setting chmod
Message-Id: <qm38t319i84g8sol8r75iqebmmh0la2e9a@4ax.com>


$sec=open(TEKST,">test.txt");
chmod 0600,$sec;
binmode(TEKST);
print TEKST ""hello world";
close TEKST;


Why is the test.txt not chmodded to 0600 but to 0755


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

Date: Sun, 09 Mar 2008 17:36:43 +0100
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: Setting chmod
Message-Id: <87d4q3am78.fsf@zeekat.nl>

John <John.Smith@invalid.com> writes:

> Why is the test.txt not chmodded to 0600 but to 0755

Because $sec is not a file name or a file handle. It's a good idea to
check if any file operation succeeded:

open(TEKST,">test.txt") or die $!;
chmod 0600,*TEKST or die $!;
binmode(TEKST);
print TEKST "hello world";
close TEKST;


-- 
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/


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

Date: Sun, 09 Mar 2008 18:51:36 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Setting chmod
Message-Id: <63imhvF27t9e2U1@mid.individual.net>

Joost Diepenmaat wrote:
> John <John.Smith@invalid.com> writes:
>> Why is the test.txt not chmodded to 0600 but to 0755
> 
> Because $sec is not a file name or a file handle. It's a good idea to
> check if any file operation succeeded:
> 
> open(TEKST,">test.txt") or die $!;
> chmod 0600,*TEKST or die $!;

With sysopen() you can do it in one step:

     sysopen TEKST, 'test.txt', O_WRONLY|O_CREAT, 0600 or die $!;

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


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

Date: Sun, 09 Mar 2008 18:44:16 GMT
From: Cosmic Cruizer <XXjbhuntxx@white-star.com>
Subject: Re: TieRegistry Overlapped Error
Message-Id: <Xns9A5C6D3B090C2ccruizermydejacom@207.115.33.102>

Took quite a bit of hacking, but I finally got it figured out:

$reg_test = $Registry->Connect( "$server", "$key_name",{ Access=>$KEY_READ 
} )  || die "Could not access remote machine: $^E\n";


Cosmic Cruizer <XXjbhuntxx@white-star.com> wrote in
news:Xns9A5B588DAC39ccruizermydejacom@207.115.17.102: 

> I'm trying to read the reg values on a remote server using 
> Win32::TieRegistry. I can remotely connect from my computer using
> regedit. When I run the following, I get an error.
> 
> use diagnostics;
> use Win32::TieRegistry(Delimiter=>"/");
> 
> use strict;
> 
> my ($reg_test, $reg_val);
> my $server = 'some_server_name';
> my $key_name = 
> 'HKEY_LOCAL_MACHINE/SYSTEM/ControlSet001/Control/ServiceCurrent';
> 
> $reg_test = $Registry->{"//$server/$key_name"} || die "Could not
> access remote machine: $^E\n";
> 
> $reg_val = $reg_test->GetValue("Content Type");
> print "REMOTE: $reg_test\t$reg_val\n";
> 
> exit 0;
> 
> 
> This is the error I'm getting:
> 
> C:\test\reg_audit>reg_audit.pl
> Uncaught exception from user code:
>         Could not access remote machine: Overlapped I/O operation is
>         in  
> progress
> 
>  at C:\test\reg_audit\reg_audit.pl line 10 
> 
> 
> Any suggestions?
> 
> 



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

Date: Sun, 09 Mar 2008 20:08:02 +0100
From: jm <jm@nospam.fr>
Subject: Re: try to use "locale" with german and coepage 437 (dos)
Message-Id: <47d43592$0$21142$7a628cd7@news.club-internet.fr>

mathias wündisch a écrit :
> Hi,
> 
> i look for informations about the use of the perl pragma "locale". the
> reason why: i have old input data coded with codepage 437. and i want
> to use the normal matchingcharacter \b (non alphabetic char) and \w
> (alphabetic char).
> i found a lot of site about posix under linux but i work under windows
> xp. am i the first with this problem?
> i want to tell perl what byte between 32 and 255 is a allphabetical
> character (\w) and what is not (\b).
> thanks for ideas.
> 
> bye, mathias
> 
What's about using Encode module, with decode function to transform your
IBM/CP 437 data to utf-8, and then using utf-8 (transparently) behaviour
of perl regex?


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

Date: Sun, 09 Mar 2008 14:34:32 +0000
From: Michael <mike@nospam.co.uk>
Subject: Use of "return" in place of "last" (newbie question)?
Message-Id: <1205073272.11311.0@proxy00.news.clara.net>

I am new to perl, and I am wondering whether it is a bad thing to jump out 
of a foreach loop with return.  I have a function which searches for a 
value in one of several arrays.  If the value is found in any of them, 
then the whole function may as well terminate.  At the moment I am setting 
a flag when the value is found, which stops execution of the later loops. 
Would it be safe / stylistically sound to do away with the flag by using 
"return" rather than "last".  Example code follows.

TIA

sub example{

my $found;
$found = 0;

foreach $item (@list)
	{
	 if ($value eq $item)
		{
		print ("found $value\n");
		$found = 1;
		last;
		}	
	}

 ... several other foreach loops follow in which the only thing which 
differs is the name of the array which is "@list" above, and the test for 
the flag.

return
}


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

Date: Sun, 9 Mar 2008 10:16:08 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Use of "return" in place of "last" (newbie question)?
Message-Id: <slrnft7vpo.ton.tadmc@tadmc30.sbcglobal.net>

Michael <mike@nospam.co.uk> wrote:

> I am new to perl, and I am wondering whether it is a bad thing to jump out 
> of a foreach loop with return.  


Yes it is a bad thing, because it will not work.  :-)


> I have a function which searches for a 
> value in one of several arrays.  If the value is found in any of them, 
> then the whole function may as well terminate.  


Oh.

Using a return() to return from a subroutine is perfectly fine
(even if the return() is within a loop).


> Would it be safe / stylistically sound to do away with the flag by using 
> "return" rather than "last".


Yes.


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


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

Date: Sun, 09 Mar 2008 16:58:05 +0000
From: Michael <mike@nospam.co.uk>
Subject: Re: Use of "return" in place of "last" (newbie question)?
Message-Id: <1205081885.22616.0@proxy02.news.clara.net>

On Sun, 09 Mar 2008 10:16:08 -0500, Tad J McClellan wrote:
<snip>
>
> Using a return() to return from a subroutine is perfectly fine (even if
> the return() is within a loop).
> 
 >> Would it be safe / stylistically sound to do away with the flag by
>> using "return" rather than "last".
> 
> Yes.

Many thanks for the prompt and helpful response Tad.

/M



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

Date: Sun, 09 Mar 2008 17:14:19 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Use of "return" in place of "last" (newbie question)?
Message-Id: <bg68t3hfvcm7vprg85f0075t6ij5l6e73l@4ax.com>

Michael <mike@nospam.co.uk> wrote:
>I am new to perl, and I am wondering whether it is a bad thing to jump out 
>of a foreach loop with return.

Technically it doesn't make a difference.

Stylistically it is a matter of which programming philosophy you are
following. For GOTOists it is standard operating procedure, for functional
programmers it is a sacrilege. Others are somewhere in between.

jue


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

Date: Sun, 09 Mar 2008 18:23:44 +0100
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: Use of "return" in place of "last" (newbie question)?
Message-Id: <871w6jx13z.fsf@zeekat.nl>

Jürgen Exner <jurgenex@hotmail.com> writes:

> Michael <mike@nospam.co.uk> wrote:
>>I am new to perl, and I am wondering whether it is a bad thing to jump out 
>>of a foreach loop with return.
>
> Technically it doesn't make a difference.
>
> Stylistically it is a matter of which programming philosophy you are
> following. For GOTOists it is standard operating procedure, for functional
> programmers it is a sacrilege. Others are somewhere in between.

Functional programmers don't use foreach either :-)

Personally, I think using a return statement generally leads to less
convoluted, clearer code.

-- 
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/


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

Date: Sun, 09 Mar 2008 17:30:49 +0000
From: Michael <mike@nospam.co.uk>
Subject: Re: Use of "return" in place of "last" (newbie question)?
Message-Id: <1205083849.22616.1@proxy02.news.clara.net>

On Sun, 09 Mar 2008 17:14:19 +0000, Jürgen Exner wrote:

> Michael <mike@nospam.co.uk> wrote:
>>I am new to perl, and I am wondering whether it is a bad thing to jump
>>out of a foreach loop with return.
> 
> Technically it doesn't make a difference.
> 
> Stylistically it is a matter of which programming philosophy you are
> following. For GOTOists it is standard operating procedure, for
> functional programmers it is a sacrilege. Others are somewhere in
> between.

Thank-you.

Just as a matter of interest, so far as I could see, my technique of 
setting a flag was the simplest way of maintaining the integrity of the 
foreach (terminating it with "last" rather than jumping out of it with 
"return"), whilst not wasting processor cycles performing irrelevant loops 
later in the function.  I am a newbie and don't know much - is there a 
more elegant alternative which would satisfy the functional programmers?



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

Date: Sun, 09 Mar 2008 18:56:51 +0100
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: Use of "return" in place of "last" (newbie question)?
Message-Id: <87wsobvl0c.fsf@zeekat.nl>

Michael <mike@nospam.co.uk> writes:

> On Sun, 09 Mar 2008 17:14:19 +0000, Jürgen Exner wrote:
>
>> Michael <mike@nospam.co.uk> wrote:
>>>I am new to perl, and I am wondering whether it is a bad thing to jump
>>>out of a foreach loop with return.
>> 
>> Technically it doesn't make a difference.
>> 
>> Stylistically it is a matter of which programming philosophy you are
>> following. For GOTOists it is standard operating procedure, for
>> functional programmers it is a sacrilege. Others are somewhere in
>> between.
>
> Thank-you.
>
> Just as a matter of interest, so far as I could see, my technique of 
> setting a flag was the simplest way of maintaining the integrity of the 
> foreach (terminating it with "last" rather than jumping out of it with 
> "return"), whilst not wasting processor cycles performing irrelevant loops 
> later in the function.  I am a newbie and don't know much - is there a 
> more elegant alternative which would satisfy the functional programmers?

There are more "elegant" (for some value of elegant) functional
alternatives , but perl's constructs don't really allow you to write
them as nicely as more functional programming languages do. For one,
perl's if { ... } else { ... } constructs aren't expressions, and also,
perl uses arrays instead of linked lists:

example in common lisp

(defun find-value (value list)
   (and list 
      (if (string= value (first list))
          value
          (find-value (rest list)))))

(defun example
   (or (find-value my-value my-list)
       (find-value my-other-value my-other-list)))

Using these kind of recursive calls almost automatically
force you to break up your sample problem into 2 subs.

Also note that you can then re-use the find-value sub to reduce the
total amount of code.


same algorithm in perl.

sub find_value {
  my ($value,@list) = @_;
  if (@list) {
    my $first = shift @list;  
    if ($first eq $value) {
      return $first;
    }
    return find_value($value,@list);
  }
  return;  # return false
}

sub example {
  find_value($value,@list) or 
  find_value($other_value,@other_list);
}

note that you probably still want to use plenty of return()s
here.

also note: is is pretty inefficient if @list is long. you'd
generally write the find_value function using foreach(), and pass
the lists as array references.

ps: none of this code has been tested. YMMV.

-- 
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/


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

Date: Sun, 09 Mar 2008 19:49:41 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Use of "return" in place of "last" (newbie question)?
Message-Id: <s7d8t35pm53hsrs7peieekharohn06ckoc@4ax.com>

Michael <mike@nospam.co.uk> wrote:
>On Sun, 09 Mar 2008 17:14:19 +0000, Jürgen Exner wrote:
>
>> Michael <mike@nospam.co.uk> wrote:
>>>I am new to perl, and I am wondering whether it is a bad thing to jump
>>>out of a foreach loop with return.
>> 
>> Technically it doesn't make a difference.
>> 
>> Stylistically it is a matter of which programming philosophy you are
>> following. For GOTOists it is standard operating procedure, for
>> functional programmers it is a sacrilege. Others are somewhere in
>> between.

>Just as a matter of interest, so far as I could see, my technique of 
>setting a flag was the simplest way of maintaining the integrity of the 
>foreach (terminating it with "last" rather than jumping out of it with 
>"return"), whilst not wasting processor cycles performing irrelevant loops 
>later in the function.  I am a newbie and don't know much - is there a 
>more elegant alternative which would satisfy the functional programmers?

Well, doing strict functional programming in Perl is extremely hard anyway
because that't not how the language was designed.
But if you follow structured programming/formal programming/program
transformations/...  then the condition under which the loop terminiates
should be defined in the condition of the loop. Neither last() nor return()
fits that bill because they exit the loop unexpectedly from somewhere in the
middle which is a nightmare for e.g. formal program verification.

Using tons of flags is the typical workaround by inexperienced programmers.
I have seen that literally hundreds of times in our university classes.
Unfortunately flags can easily lead to to convoluted and difficult to
maintain code because it can become very difficult to follow their logic and
to analyse where which flag is being set under what conditions.

Very often it takes just a little bit of restructuring of the loop to get a
clean and easy to understand single exit condition without bail-outs in the
middle. One approach that works often is to rearrange the exection sequence
in the loop and/or reduce the loop by one iteration by moving some
initialization code before the loop:

Generic example:
	while (cond_x) {
		do_some_work();
		if (cond_y) {last};
		do_more_work();
	}

can usually be transformed into something like
	do__prep_work(); #includes do_some_work() for first iteration
	while (cond_x and not cond_y) {
		do_more_work();
		do_some_work();
	}
		
jue
		


IMO the better app


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

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


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