[31117] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2362 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Apr 23 16:09:44 2009

Date: Thu, 23 Apr 2009 13: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, 23 Apr 2009     Volume: 11 Number: 2362

Today's topics:
    Re: Encode::decode() clears scalar being decoded? <helmut@wollmersdorfer.at>
    Re: Encode::decode() clears scalar being decoded? <hjp-usenet2@hjp.at>
    Re: Exiting given via next skendric@fhcrc.org
    Re: Exiting given via next skendric@fhcrc.org
    Re: Exiting given via next <uri@stemsystems.com>
    Re: Exiting given via next sln@netherlands.com
    Re: Exiting given via next sln@netherlands.com
    Re: Exiting given via next sln@netherlands.com
    Re: Exiting given via next <ben@morrow.me.uk>
    Re: Exiting given via next skendric@fhcrc.org
    Re: Exiting given via next <uri@stemsystems.com>
    Re: Is there a better way to convert foreign characters (Tim McDaniel)
    Re: Is there a better way to convert foreign characters <noreply@gunnar.cc>
        totally out of my area (but not OT) - Windows updates s <cartercc@gmail.com>
    Re: totally out of my area (but not OT) - Windows updat <devnull4711@web.de>
    Re: totally out of my area (but not OT) - Windows updat <jurgenex@hotmail.com>
    Re: totally out of my area (but not OT) - Windows updat <ben@morrow.me.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 23 Apr 2009 09:33:33 +0200
From: Helmut Wollmersdorfer <helmut@wollmersdorfer.at>
Subject: Re: Encode::decode() clears scalar being decoded?
Message-Id: <gsp5ne$29hs$1@geiz-ist-geil.priv.at>

Robert Urban wrote:

> my $tmp = decode('utf8', $string, 1);

[...]

> What happened to $string?  There is no mention of side-effects in the Encode
> manpage... This only happens when CHECK is set to 1.

Oh, thx, I didn't know.

----perldoc Encoding----

Encode::LEAVE_SRC
   If the "Encode::LEAVE_SRC" bit is not set, but CHECK is, then the
   second argument to "encode()" or "decode()" may be assigned to by the
   functions. If you’re not interested in this, then bitwise-or the
   bitmask with it.

----Quit---------------

this works:

my $tmp = decode('utf8', $string, 9);

HTH

Helmut Wollmersdorfer


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

Date: Thu, 23 Apr 2009 21:41:55 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Encode::decode() clears scalar being decoded?
Message-Id: <slrngv1h44.9e.hjp-usenet2@hrunkner.hjp.at>

On 2009-04-23 00:08, Robert Urban <urban@tru64.org> wrote:
> my $tmp = decode('utf8', $string, 1);

[sets $string to ""]

> What happened to $string?  There is no mention of side-effects in the Encode
> manpage...

There is, but only for CHECK == FB_QUIET or FB_WARN, not for FB_CROAK
(1). 

> This only happens when CHECK is set to 1.

Nope, it also happens if CHECK is set to 4 (FB_QUIET) or 6 (FB_WARN).
But there it is documented.

I'm not sure if this is a bug or an (undocumented) feature.

	hp



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

Date: Thu, 23 Apr 2009 06:22:25 -0700 (PDT)
From: skendric@fhcrc.org
Subject: Re: Exiting given via next
Message-Id: <614706b0-3fed-43f0-a5f8-38364bc029ab@b7g2000pre.googlegroups.com>

hi uri,

i was being too terse ... in fact, the second case is the same as the
first (wrapped in a while loop) ... is there some way to suppress the
'Exiting via given/when' complaints?

use feature 'switch';
while (my $line = <$fh>) {
    LINE:
    given ($line) {
        when (/Shrill and clear he crowed/) {
            say 'shrill';
            $count++;
            next LINE;
        }
        when (/Recking nothing of wizardry/) {
            say 'recking';
            $count++;
            next LINE;
        }
       default {
            next LINE;
       }
    }
}

--sk

> but that was using the Switch module (which is evil since it does source
> filtering). his second case using the 5.10 switch feature (built in and
> not a source filter) didn't have a loop. he was wondering why next
> didn't work inside the given. the answer (which is correct) is that
> given is not a loop construct. if it was wrapped in a while as in the
> first example, next would work.


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

Date: Thu, 23 Apr 2009 07:35:30 -0700 (PDT)
From: skendric@fhcrc.org
Subject: Re: Exiting given via next
Message-Id: <edf886a6-5ca0-461d-b122-58e43b0509b7@d2g2000pra.googlegroups.com>

or am i doing something evil by trying to exit given and when clauses
using 'next'?

--sk


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

Date: Thu, 23 Apr 2009 11:14:17 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Exiting given via next
Message-Id: <87vdovqu5y.fsf@quad.sysarch.com>

>>>>> "s" == skendric  <skendric@fhcrc.org> writes:

  s> i was being too terse ... in fact, the second case is the same as the
  s> first (wrapped in a while loop) ... is there some way to suppress the
  s> 'Exiting via given/when' complaints?

you have several mistakes and easy ways to fix this.

  s> use feature 'switch';
  s> while (my $line = <$fh>) {
  s>     LINE:

that label should be before the while. but that isn't the main bug.

  s>     given ($line) {
  s>         when (/Shrill and clear he crowed/) {
  s>             say 'shrill';
  s>             $count++;
  s>             next LINE;


from perldoc perlsyn:

  Breaking out

       You can use the "break" keyword to break out of the enclosing "given"
       block.  Every "when" block is implicitly ended with a "break".


but since you will exit the given block anyway in all cases, just fall
through and you will do the loop anyway. there is no need for the next
calls. i tested that. about the only time you will need to break from a
given is when you have to deeper inside a when clause.

  s>         }
  s>         when (/Recking nothing of wizardry/) {
  s>             say 'recking';
  s>             $count++;
  s>             next LINE;

delete that (and the previous) next and it works fine and quietly.

  s>         }
  s>        default {
  s>             next LINE;
  s>        }

that whole default clause isn't needed. you will exit the given block if
nothing is matched by a when and then you will loop as expected.

you need to read up more about given/when and see how it works by
default. you were trying too hard to exit when it exits for you!

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Thu, 23 Apr 2009 08:15:52 -0700
From: sln@netherlands.com
Subject: Re: Exiting given via next
Message-Id: <2b01v4dondqg6ku3iqlumirri82e21v8hq@4ax.com>

On Thu, 23 Apr 2009 06:22:25 -0700 (PDT), skendric@fhcrc.org wrote:

>hi uri,
>
>i was being too terse ... in fact, the second case is the same as the
>first (wrapped in a while loop) ... is there some way to suppress the
>'Exiting via given/when' complaints?
>
>use feature 'switch';
>while (my $line = <$fh>) {
>    LINE:
>    given ($line) {
>        when (/Shrill and clear he crowed/) {
>            say 'shrill';
>            $count++;
>            next LINE;
>        }

With 5.8.6 I get "Label not found..." with your block structure.
while (my $line = <DATA>)
{
LABEL:
	@ar = map {
		next LABEL if (++$i > 10); $_
	} split (//, $line);
	print "$i";
	getc(STDIN);
	next;
}
print "end\n";

But with label up here, I don't get that error.
LABEL:
while (my $line = <DATA>)
{
LABEL:
	@ar = map {
          ...

-sln



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

Date: Thu, 23 Apr 2009 09:18:29 -0700
From: sln@netherlands.com
Subject: Re: Exiting given via next
Message-Id: <gu11v49chru5d7c19c9n8n5a6bj1ss4c0s@4ax.com>

On Thu, 23 Apr 2009 11:14:17 -0400, Uri Guttman <uri@stemsystems.com> wrote:

>>>>>> "s" == skendric  <skendric@fhcrc.org> writes:
>
[snip]
>from perldoc perlsyn:
>
>  Breaking out
>
>       You can use the "break" keyword to break out of the enclosing "given"
>       block.  Every "when" block is implicitly ended with a "break".
>
>
>but since you will exit the given block anyway in all cases, just fall
>through and you will do the loop anyway. there is no need for the next
>calls.

Unfortunately, you have to specify "continue" inside a when() to "fall through" to the next
when block, otherwise a "break" is implicit from a given(), like what you said, or a "next"
is implicit if the given is called from a loop block.

This is opposite of how the C switch/case works. There is no implicit "break" within a case block,
only fall through behavior, to the next case. A break is explicit from the switch.
In Perl now, to fall through, you have to explicitly continue, otherwise the break is implicit.

More confusion.

-sln



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

Date: Thu, 23 Apr 2009 09:20:35 -0700
From: sln@netherlands.com
Subject: Re: Exiting given via next
Message-Id: <a951v45c2htos79rajmaftql4bii6u4vnc@4ax.com>

On Thu, 23 Apr 2009 09:18:29 -0700, sln@netherlands.com wrote:

>On Thu, 23 Apr 2009 11:14:17 -0400, Uri Guttman <uri@stemsystems.com> wrote:
>
>>>>>>> "s" == skendric  <skendric@fhcrc.org> writes:
>>
>[snip]
>>from perldoc perlsyn:
>>
>>  Breaking out
>>
>>       You can use the "break" keyword to break out of the enclosing "given"
>>       block.  Every "when" block is implicitly ended with a "break".
>>
>>
>>but since you will exit the given block anyway in all cases, just fall
>>through and you will do the loop anyway. there is no need for the next
>>calls.
>
>Unfortunately, you have to specify "continue" inside a when() to "fall through" to the next
>when block, otherwise a "break" is implicit from a given(), like what you said, or a "next"
>is implicit if the given is called from a loop block.
                    ^^^^^
                  when
>
>This is opposite of how the C switch/case works. There is no implicit "break" within a case block,
>only fall through behavior, to the next case. A break is explicit from the switch.
>In Perl now, to fall through, you have to explicitly continue, otherwise the break is implicit.
>
>More confusion.
>
>-sln



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

Date: Thu, 23 Apr 2009 17:25:47 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Exiting given via next
Message-Id: <bdh6c6-mju.ln1@osiris.mauzo.dyndns.org>


Quoth skendric@fhcrc.org:
> i'm fond of constructs like the following:
> 
> use Switch;
> LINE:
> while (my $line = <$fh>) {
>     switch ($line) {
>         case /Shrill and clear he crowed/ {
>             say 'shrill';
>             $count++;
>             next LINE;
>         }
>         case /Recking nothing of wizardry/ {
>             say 'recking';
>             $count++;
>             next LINE;
>         }
>        else {
>             next LINE;
>         }
>     }
> }
> 
> but, switch has a memory leak (http://rt.cpan.org/Public/Bug/
> Display.html?id=45232), which makes it unsuitable for sitting inside
> loops which iterate many times
> 
> the new switch statement in perl-5.10.0 does not have this memory
> leak:
> 
> use feature 'switch';
> LINE:
> given ($line) {
>     when (/Shrill and clear he crowed/) {
>         say 'shrill';
>         $count++;
>         next LINE;
>     }
>     when (/Recking nothing of wizardry/) {
>         say 'recking';
>         $count++;
>         next LINE;
>     }
>    default {
>         next LINE;
>     }
> }
> 
> however, the 'switch' feature doesn't like seeing 'next':
> 
> gnat> ./daily-syslog-extracts
> Parsing logfile...
> Exiting when via next at ./daily-syslog-extracts line 179, <GEN0> line
> 7.

This is bizarre. The warning only happens when you use next LABEL. Plain
'next' doesn't warn:

    ~% perl -wE'LINE: for my $x (1..3) {
            say $x;
            given ($x) {
                when (1) { next LINE }
            }
        }'
    1
    Exiting when via next at -e line 4.
    Exiting given via next at -e line 4.
    2
    3
    ~% perl -wE'LINE: for my $x (1..3) {
            say $x;
            given ($x) {
                when (1) { next }
            }
        }'
    1
    2
    3
    ~%

I don't see the justiication for the warning at all. For the other cases
that warn (subs, evals) it makes sense, as the flow of control is doing
odd and unexpected things, but given/when are more like if than they are
like a sub call.

Ben



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

Date: Thu, 23 Apr 2009 10:03:55 -0700 (PDT)
From: skendric@fhcrc.org
Subject: Re: Exiting given via next
Message-Id: <79a98665-be5c-40f4-8b5b-c9fabc355d7a@q33g2000pra.googlegroups.com>

ok, i've made a classic error here:  i over-simplified

here's something a little closer to what i want to do (not guaranteed
to work; i'm trying to stay conceptual here.  conceptually, i'm
filtering a log file ... some lines are interesting, some are not, and
i'm taking a tiered approach to skipping the uninteresting lines,
throwing some away immediately, tossing others only after more
detailed inspection)

use feature 'switch;'
use List::MoreUtils qw(any);
[...]
LINE:
while (my $line = <$fh>) {
  next LINE if $line =~ /various patterns/;  # Skip obviously
uninteresting lines

  # This line might be interesting; if it is, save it; otherwise, skip
to the next line
  given ($line) {
    when /\-fw/ {     # Firewalls
      next LINE if any { $line =~ /$_/ } @{$ignore{'Firewall'}};  #
Actually, not interesting
      push @{$keep{'Firewall'}}, $line;
    }
    when /\-rtr/ {    # Routers
      next LINE if any { $line =~ /$_/ } @{$ignore{'Firewall'}};  #
Actually, not interesting
      push @{$keep{'Router'}}, $line;
    }
    [...]
    default {
      next LINE;   # Turns out that this line isn't interesting after
all, skip to the next one
    }
  }

  # OK, if I've made it this far, this line is interesting to me in
various ways
  # Do more processing here
  [...]

}


so, if i use 'break', then i escape the 'when' stanza, fall through to
the end of the 'given' ... but i don't want to fall through to the end
of 'given':  i don't want to reach the 'Do more processing here'
section, because i've determined that this line isn't interesting
after all

(a) i could replace 'next LINE' with 'break' and then stuff all my 'do
more processing' code into a subroutine and call it from each and
every 'when' clause.  repetitious, but it would work

(b) i could go back to using cascaded if/elsif

(c) i could refactor my approach entirely ...

but here's my specific question:  what is it about the perl-5.10.0
switch construct (what is it about 'given/when') that makes the use of
'next' worthy of the 'Exitting given via next' warning?

--sk

stuart kendrick
fhcrc


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

Date: Thu, 23 Apr 2009 13:47:53 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Exiting given via next
Message-Id: <87mya7p8hi.fsf@quad.sysarch.com>

>>>>> "s" == skendric  <skendric@fhcrc.org> writes:
  s> LINE:
  s> while (my $line = <$fh>) {
  s>   next LINE if $line =~ /various patterns/;  # Skip obviously
  s> uninteresting lines


as i said before, there is no need for the label and LINE, plain next
should work. and my test showed that there is no warning if the given
block is inside the while. loop labels are needed if you are breaking
out of nested loops from deeper inside.

  s> (a) i could replace 'next LINE' with 'break' and then stuff all my 'do
  s> more processing' code into a subroutine and call it from each and
  s> every 'when' clause.  repetitious, but it would work

  s> (b) i could go back to using cascaded if/elsif

this is fine for what you want. but there is no need for the elsif if
you call next. that should clean it up a bit. you do need explicit use
of $line instead of the cleaner when().

  s> (c) i could refactor my approach entirely ...

that is another idea. 

  s> but here's my specific question:  what is it about the perl-5.10.0
  s> switch construct (what is it about 'given/when') that makes the use of
  s> 'next' worthy of the 'Exitting given via next' warning?

well, given isn't a loop construct so breaking from it with next is
'wrong' and should be warned. ben shows that it doesn't warn if there is
no label used so that confuses it even more. i would skip the label as
it isn't needed and it quiets the warning. this smells of a bug and
should be reported to p5p.

and in the worst case you could silence that particular warning with a
no warnings statement with the right arg.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Thu, 23 Apr 2009 16:13:42 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: Is there a better way to convert foreign characters?
Message-Id: <gsq43m$h8p$1@reader1.panix.com>

In article <751lqvF1647sbU1@mid.individual.net>,
Gunnar Hjalmarsson  <noreply@gunnar.cc> wrote:
>( $word = lc $value ) =~ tr/àâÀéèëêÉÊçÇîïôÔùû/aaaeeeeeecciioouu/;

I don't combine s///, tr///, or chomp with assignments -- personal
idiom and I'm not familiar with the Perl effects.  The above assigns
the lowercase translation of $value to $word, and then does a tr/// on
$word, right?  Then there should be no need for the capitalized
characters in the tr///, because there shouldn't be any to match.

I agree with the other posters who suggest using standard modules,
like Undiacritical or whatever it was.

-- 
Tim McDaniel, tmcd@panix.com


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

Date: Thu, 23 Apr 2009 21:38:09 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Is there a better way to convert foreign characters?
Message-Id: <75buddF17np90U1@mid.individual.net>

Tim McDaniel wrote:
> In article <751lqvF1647sbU1@mid.individual.net>, Gunnar Hjalmarsson
> <noreply@gunnar.cc> wrote:
>> ( $word = lc $value ) =~ tr/àâÀéèëêÉÊçÇîïôÔùû/aaaeeeeeecciioouu/;
> 
> I don't combine s///, tr///, or chomp with assignments -- personal 
> idiom and I'm not familiar with the Perl effects.

Finding out the effects is trivial, isn't it?

> The above assigns the lowercase translation of $value to $word, and 
> then does a tr/// on $word, right?

Yes. So you do know about the effects, after all. ;-)

> Then there should be no need for the capitalized characters in the 
> tr///, because there shouldn't be any to match.

That's true only if a suitable locale is enabled. If a programmer wants 
to do that kind of transliteration, there is a great chance that s/he 
doesn't care about any kind of i18n or l10n.

> I agree with the other posters who suggest using standard modules, 
> like Undiacritical or whatever it was.

Those are not standard modules. I for one suggest to avoid that kind of 
transliteration. I simply answered the OP's question.

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


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

Date: Thu, 23 Apr 2009 07:50:57 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: totally out of my area (but not OT) - Windows updates script
Message-Id: <cac19129-fa38-4821-a443-795b5294b56e@t21g2000yqi.googlegroups.com>

This is totally out of my area, but not OT.

I have an urgent request from a Windows sysadmin to write a script to
install Windows updates from Microsoft. I frankly have no f!@#king
idea how to do it or how to even start thinking about doing it.

Can this be done with Perl?

If so, does anyone have a script to share? Even if it might not work,
just looking at it would be helpful.

TIA, CC.



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

Date: Thu, 23 Apr 2009 18:14:12 +0200
From: Frank Seitz <devnull4711@web.de>
Subject: Re: totally out of my area (but not OT) - Windows updates script
Message-Id: <75biegF17hklfU1@mid.individual.net>

ccc31807 wrote:
> 
> I have an urgent request from a Windows sysadmin to write a script to
> install Windows updates from Microsoft. I frankly have no f!@#king
> idea how to do it or how to even start thinking about doing it.

Google? CPAN?

Frank
-- 
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel


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

Date: Thu, 23 Apr 2009 09:17:59 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: totally out of my area (but not OT) - Windows updates script
Message-Id: <uj41v4hkiec6c2ip6q6uauk9nrd78tgn7n@4ax.com>

ccc31807 <cartercc@gmail.com> wrote:
>This is totally out of my area, but not OT.
>
>I have an urgent request from a Windows sysadmin to write a script to
>install Windows updates from Microsoft. 

You need to be very specific here. 
Windows Update (upper case "U") is a mostly ActiveX-based client side
application together with a server-side database, which together
determine which updates (lower case "u") are appropriate for a given
machine, download them, and install them if so configured.

Windows updates (lower case "u") are simply self-extracting EXEs or CABs
or ZIPs or sometimes other self-installing packages which you can simply
download from the Microsoft download server and execute/call. 
Now, the trick would be to find out which one of the gazillions
different versions you will need for a specific computer considering
OS-version, language, market, previous updates, .....  

Your sysadmin would probably be far better of to use Microsoft's site
admin tools, which will download the updates once to a central local
server and then distribute them locally.

jue


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

Date: Thu, 23 Apr 2009 17:32:16 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: totally out of my area (but not OT) - Windows updates script
Message-Id: <gph6c6-mju.ln1@osiris.mauzo.dyndns.org>


Quoth ccc31807 <cartercc@gmail.com>:
> This is totally out of my area, but not OT.
> 
> I have an urgent request from a Windows sysadmin to write a script to
> install Windows updates from Microsoft. I frankly have no f!@#king
> idea how to do it or how to even start thinking about doing it.
> 
> Can this be done with Perl?
> 
> If so, does anyone have a script to share? Even if it might not work,
> just looking at it would be helpful.

You should be able to use Win32::OLE to talk to the WUA objects. See
e.g. http://msdn.microsoft.com/en-us/library/aa387102(VS.85).aspx for
some example VBS: translating to Perl is left as an exercise :).

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


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