[32176] in Perl-Users-Digest
Perl-Users Digest, Issue: 3441 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Jul 10 21:09:24 2011
Date: Sun, 10 Jul 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 Sun, 10 Jul 2011 Volume: 11 Number: 3441
Today's topics:
Can I set default encoding for filename in open()? <gypark@gmail.com>
Re: Can I set default encoding for filename in open()? <*@eli.users.panix.com>
Re: Perl hashes and structs question <rvtol+usenet@xs4all.nl>
remove undefined values witout warnings <nospam.gravitalsun@hotmail.com.nospam>
Re: remove undefined values witout warnings <willem@turtle.stack.nl>
Re: remove undefined values witout warnings <jurgenex@hotmail.com>
Re: remove undefined values witout warnings <nospam.gravitalsun@hotmail.com.nospam>
Re: remove undefined values witout warnings <nospam.gravitalsun@hotmail.com.nospam>
Re: remove undefined values witout warnings <nospam.gravitalsun@hotmail.com.nospam>
Re: remove undefined values witout warnings <hjp-usenet2@hjp.at>
Re: remove undefined values witout warnings <jurgenex@hotmail.com>
Re: sort scientific notation value after alphabet <source@netcom.com>
Re: sort scientific notation value after alphabet <rweikusat@mssgmbh.com>
Re: sort scientific notation value after alphabet <willem@turtle.stack.nl>
Re: test two hash(refs) for equality <news@lawshouse.org>
Re: test two hash(refs) for equality <papa.pearl@suomi24.fi>
Re: test two hash(refs) for equality <uri@StemSystems.com>
Re: test two hash(refs) for equality <rweikusat@mssgmbh.com>
Re: test two hash(refs) for equality <rweikusat@mssgmbh.com>
Re: test two hash(refs) for equality <rweikusat@mssgmbh.com>
Re: test two hash(refs) for equality <jwkrahn@example.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 9 Jul 2011 23:09:05 -0700 (PDT)
From: Raymundo <gypark@gmail.com>
Subject: Can I set default encoding for filename in open()?
Message-Id: <1391d88b-e4ea-4d09-a83b-abe81c225247@17g2000prr.googlegroups.com>
Hello,
I'm using MS Windows for Korean. It uses cp949 encoding as default
encoding for file system.
I know I can set default encoding for reading from or writing to a
file, using several methods:
- open my $fh, "<:encoding(cp949)", $filename;
- binmode STDOUT, ":encoding(cp949)";
- use open ":encoding(cp949)";
- etc.
However, those are for the contents of files. If the "filename"
includes non-Latin characters, I have to encode it explicitly
everytime I pass it to open():
-----
use utf8; # my script is stored in a file using UTF-8 format
use Encode;
use autodie;
my $filename = "OO.txt"; # let's assume "OO" is Korean character,
such as "\x{AC00}"
open my $fh, "<", encode("cp949", $filename);
-----
Can I set default encoding for the third parameter of open(), so that
I don't need to encode it?
open my $fh, "<", $filename; # like this
Any advice would be appreciated.
------------------------------
Date: Sun, 10 Jul 2011 18:33:07 +0000 (UTC)
From: Eli the Bearded <*@eli.users.panix.com>
Subject: Re: Can I set default encoding for filename in open()?
Message-Id: <eli$1107101433@qz.little-neck.ny.us>
In comp.lang.perl.misc, Raymundo <gypark@gmail.com> wrote:
> Can I set default encoding for the third parameter of open(), so that
> I don't need to encode it?
> open my $fh, "<", $filename; # like this
> Any advice would be appreciated.
Use a function.
# with "use autodie" no need to worry about open failures.
sub cpopen ($$) {
my $mode = shift;
my $file = shift;
my $glob;
open $glob, "$mode:encoding(cp949)", encode('cp949', $file);
return $glob;
}
my $fh = cpopen('<', $filename );
Elijah
------
use a function generator to create families of these for different encodings
------------------------------
Date: Sun, 10 Jul 2011 08:26:27 +0200
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Perl hashes and structs question
Message-Id: <4e194613$0$21790$e4fe514c@news2.news.xs4all.nl>
On 2011-07-08 16:10, ccc31807 wrote:
> (1) Do Perl hashes of hashes serve the same purpose as structs? I
> think they do.
Depends.
> (2) Does Perl have a data structure similar to a struct?
You can pack() in a string whatever you like.
--
Ruud
------------------------------
Date: Sun, 10 Jul 2011 19:02:23 +0300
From: George Mpouras <nospam.gravitalsun@hotmail.com.nospam>
Subject: remove undefined values witout warnings
Message-Id: <ivcief$2bd1$1@news.ntua.gr>
# I want to remove whitespace or undef values from an array without
warnings. No luck so far
use strict;
use warnings;
my @array;
$array[0] = 'A';
$array[1] = '';
$array[2] = "\t";
$array[5] = 'B';
$array[8] = ' ';
$array[9] = 'C';
for (my $i=0; $i<=$#array; $i++) {
splice @array, $i--, 1 if $array[$i] =~/^\s*$/
}
$,=", "; print @array;
------------------------------
Date: Sun, 10 Jul 2011 16:32:39 +0000 (UTC)
From: Willem <willem@turtle.stack.nl>
Subject: Re: remove undefined values witout warnings
Message-Id: <slrnj1jl17.1ami.willem@turtle.stack.nl>
George Mpouras wrote:
) # I want to remove whitespace or undef values from an array without
) warnings. No luck so far
)
) use strict;
) use warnings;
)
) my @array;
) $array[0] = 'A';
) $array[1] = '';
) $array[2] = "\t";
) $array[5] = 'B';
) $array[8] = ' ';
) $array[9] = 'C';
)
) for (my $i=0; $i<=$#array; $i++) {
) splice @array, $i--, 1 if $array[$i] =~/^\s*$/
) }
)
)
) $,=", "; print @array;
Use the grep function. That's what it's made for.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
------------------------------
Date: Sun, 10 Jul 2011 09:40:07 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: remove undefined values witout warnings
Message-Id: <5ukj17tr7jk2p03rpph5jd5mlaio6eivjf@4ax.com>
George Mpouras <nospam.gravitalsun@hotmail.com.nospam> wrote:
># I want to remove whitespace or undef values from an array without
>warnings. No luck so far
>
>use strict;
>use warnings;
>
>my @array;
>$array[0] = 'A';
>$array[1] = '';
>$array[2] = "\t";
>$array[5] = 'B';
>$array[8] = ' ';
>$array[9] = 'C';
>
>for (my $i=0; $i<=$#array; $i++) {
>splice @array, $i--, 1 if $array[$i] =~/^\s*$/
>}
Please, please see 'perldoc -f grep'.
And to check if a value is defined please see 'perldoc -f defined'.
jue
------------------------------
Date: Sun, 10 Jul 2011 19:42:50 +0300
From: George Mpouras <nospam.gravitalsun@hotmail.com.nospam>
Subject: Re: remove undefined values witout warnings
Message-Id: <ivckqa$2lli$1@news.ntua.gr>
I assume you did not test your proposition !
------------------------------
Date: Sun, 10 Jul 2011 19:43:21 +0300
From: George Mpouras <nospam.gravitalsun@hotmail.com.nospam>
Subject: Re: remove undefined values witout warnings
Message-Id: <ivckr9$2lli$2@news.ntua.gr>
do not work
------------------------------
Date: Sun, 10 Jul 2011 19:52:19 +0300
From: George Mpouras <nospam.gravitalsun@hotmail.com.nospam>
Subject: Re: remove undefined values witout warnings
Message-Id: <ivclc3$2nco$1@news.ntua.gr>
sorry, is working !
------------------------------
Date: Sun, 10 Jul 2011 18:50:32 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: remove undefined values witout warnings
Message-Id: <slrnj1jm2s.qlt.hjp-usenet2@hrunkner.hjp.at>
On 2011-07-10 16:02, George Mpouras <nospam.gravitalsun@hotmail.com.nospam> wrote:
> # I want to remove whitespace or undef values from an array without
> warnings. No luck so far
>
[...]
> for (my $i=0; $i<=$#array; $i++) {
> splice @array, $i--, 1 if $array[$i] =~/^\s*$/
> }
@array = grep { defined && ! /^\s*$/ } @array;
------------------------------
Date: Sun, 10 Jul 2011 10:02:37 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: remove undefined values witout warnings
Message-Id: <nkmj175tao0tlnnr75t5hmbivcssleoehu@4ax.com>
George Mpouras <nospam.gravitalsun@hotmail.com.nospam> wrote:
>do not work
That is a nice suggestion, thank you. Unfortunately most of us are not
wealthy enough to not work and therefore we must work to earn some
money.
If you meant something else maybe you could include what you meant.
Because those three words don't deliver much context.
jue
------------------------------
Date: Sat, 09 Jul 2011 17:15:57 -0700
From: David Harmon <source@netcom.com>
Subject: Re: sort scientific notation value after alphabet
Message-Id: <joudnT7FuKnzcoXTnZ2dnUVZ_h2dnZ2d@earthlink.com>
On Wed, 06 Jul 2011 16:32:55 -0400 in comp.lang.perl.misc, "Uri
Guttman" <uri@StemSystems.com> wrote,
>nope. it is misleading as they may look for the variables later on and
>not find them.
They were declared lexically in a block that is three lines long.
If someone is looking for them later on, he is hopeless anyway.
------------------------------
Date: Sun, 10 Jul 2011 21:21:48 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: sort scientific notation value after alphabet
Message-Id: <8739idantv.fsf@sapphire.mobileactivedefense.com>
Willem <willem@turtle.stack.nl> writes:
> Rainer Weikusat wrote:
> ) Willem <willem@turtle.stack.nl> writes:
> )> Why would a reader need to expect that a variable is used or not?
> )
> ) Even a reader who assumed that declarations communicate exactly
> ) nothing about anything except themselves will be misled except nothing
> ) which is actually used is ever declared.
>
> Why?
Exercise for the reader.
>
> ) Not that this would be a
> ) particularly sensible assumption, given that it is the purpose of
> ) declarations to provide information about what will be used by the
> ) code.
>
> That is not a given, that is your opinion.
That it is not given happens to be your opinion.
[...]
> ) A circular argument would be one where the conclusion and at least one
> ) of the premises are identical. But you didn't claim this. You were
> ) disputing the second premise. Consequently, you counterstatetment does
> ) not demonstrate that the argument is circular.
>
> The second premise is that readers expect that a variable is used
This statement is wrong.
------------------------------
Date: Sun, 10 Jul 2011 22:17:38 +0000 (UTC)
From: Willem <willem@turtle.stack.nl>
Subject: Re: sort scientific notation value after alphabet
Message-Id: <slrnj1k982.1i23.willem@turtle.stack.nl>
Rainer Weikusat wrote:
) Exercise for the reader.
)
...
)
) That it is not given happens to be your opinion.
)
...
)
) This statement is wrong.
I see you didn't bother to give any arguments for your assertions and you
snipped my arguments without replying to them. I find that despicable and
refuse to argue this further.
Declaring unused variables is not misleading. Period. Have a day.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
------------------------------
Date: Sun, 10 Jul 2011 14:45:16 +0100
From: Henry Law <news@lawshouse.org>
Subject: Re: test two hash(refs) for equality
Message-Id: <puSdnUW37tV2MYTTnZ2dnUVZ8u2dnZ2d@giganews.com>
On 07/07/11 21:24, Rainer Weikusat wrote:
> They are not reserved. The sort routine uses two variables with names
> $a and $b in the symbol table of the module sort is invoked in (as far
> as I understand the documentation). These $a and $b therefore don't
> collide with lexical variables and they also don't collided with other
> 'package global' variables because sort localizes them (as it shoud do)
You are right in every respect except one: your assertion that because
you are right then it's right to continue doing what has been proved to
be undesirable. "Everything that is not forbidden is permitted, but not
everything that is permitted is right".
$a and $b are a special case inasmuch as they may be used in a program
which uses "strict" and "warnings" (as all should). Look:
$ cat tryout; ./tryout
#! /usr/bin/perl
use strict;
use warnings;
$a = 1;
$c = 1;
Global symbol "$c" requires explicit package name at ./tryout line 7.
Execution of ./tryout aborted due to compilation errors.
So using $a or $b in "ordinary" code will sooner or later cause some
programmer somewhere to spend a day chasing an obscure programming error.
That is why you are wrong.
--
Henry Law Manchester, England
------------------------------
Date: Sun, 10 Jul 2011 19:14:45 +0300
From: pepa <papa.pearl@suomi24.fi>
Subject: Re: test two hash(refs) for equality
Message-Id: <VdkSp.58285$mX5.22736@uutiset.elisa.fi>
9.7.2011 5:56, John W. Krahn kirjoitti:
>
> But I passed an array containing two elements. Why doesn't it work?
You have the answer glaring at you right in your question: "I passed
an array containing two elements." You passed an array. Your prototype
declares two elements, but instead of passing two elements, you pass
an array.
You have given a wonderful example of why perl prototypes are
deprecared.
------------------------------
Date: Sun, 10 Jul 2011 12:43:03 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: test two hash(refs) for equality
Message-Id: <87k4bqozmw.fsf@quad.sysarch.com>
>>>>> "p" == pepa <papa.pearl@suomi24.fi> writes:
p> 9.7.2011 5:56, John W. Krahn kirjoitti:
>>
>> But I passed an array containing two elements. Why doesn't it work?
p> You have the answer glaring at you right in your question: "I passed
p> an array containing two elements." You passed an array. Your prototype
p> declares two elements, but instead of passing two elements, you pass
p> an array.
p> You have given a wonderful example of why perl prototypes are
p> deprecared.
you seem to think john didn't understand the issue. he was showing an
example of why prototypes are deprecated so the other poster who likes
them might actually learn something.
uri
--
Uri Guttman -- uri AT perlhunter DOT com --- http://www.perlhunter.com --
------------ Perl Developer Recruiting and Placement Services -------------
----- Perl Code Review, Architecture, Development, Training, Support -------
------------------------------
Date: Sun, 10 Jul 2011 21:04:44 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: test two hash(refs) for equality
Message-Id: <87ipr9aomb.fsf@sapphire.mobileactivedefense.com>
"John W. Krahn" <jwkrahn@example.com> writes:
> Rainer Weikusat wrote:
>> "Uri Guttman"<uri@StemSystems.com> writes:
>>>>>>>> "RW" == Rainer Weikusat<rweikusat@mssgmbh.com> writes:
>>>
>>> RW> "Uri Guttman"<uri@StemSystems.com> writes:
>>> >>>>>>> "RW" == Rainer Weikusat<rweikusat@mssgmbh.com> writes:
>>> >>
>>> RW> sub cmp_href_0($$)
>>> >>
>>> >> that prototype is useless. it doesn't do anything worth the
>>> >> bother.
>>>
>>> RW> It provides at least some sort of compile-time checking of function
>>> RW> calls and that's something I decidedly want to have ...
>>>
>>> barely and it can be bypassed with calls like&foo.
>>
>> It will cause the compiler to make noises when a function is called
>> with less arguments than the prototype said it should have. I found
>> this to be useful to me.
>
>
> $ perl -le'
> my %x = "a" .. "z";
> my %y = "A" .. "Z";
> my @refs = ( \%x, \%y );
> sub cmp_href_0 ($$) {
> my ( $a, $b ) = @_;
> print %$a, %$b;
> }
> cmp_href_0( @refs );
> '
> Not enough arguments for main::cmp_href_0 at -e line 9, near "@refs )"
> Execution of -e aborted due to compilation errors.
>
>
>
> But I passed an array containing two elements. Why doesn't it work?
Why do you believe that an array evaluated in a scalar context would
be equivalent to passing two scalar values when the evaluation returns
only one?
------------------------------
Date: Sun, 10 Jul 2011 21:10:53 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: test two hash(refs) for equality
Message-Id: <87box1aoc2.fsf@sapphire.mobileactivedefense.com>
Henry Law <news@lawshouse.org> writes:
> On 07/07/11 21:24, Rainer Weikusat wrote:
>> They are not reserved. The sort routine uses two variables with names
>> $a and $b in the symbol table of the module sort is invoked in (as far
>> as I understand the documentation). These $a and $b therefore don't
>> collide with lexical variables and they also don't collided with other
>> 'package global' variables because sort localizes them (as it shoud do)
>
> You are right in every respect except one: your assertion that because
> you are right then it's right to continue doing what has been proved
> to be undesirable. "Everything that is not forbidden is permitted,
> but not everything that is permitted is right".
>
> $a and $b are a special case inasmuch as they may be used in a program
> which uses "strict" and "warnings" (as all should). Look:
>
> $ cat tryout; ./tryout
> #! /usr/bin/perl
>
> use strict;
> use warnings;
>
> $a = 1;
> $c = 1;
>
> Global symbol "$c" requires explicit package name at ./tryout line 7.
> Execution of ./tryout aborted due to compilation errors.
>
> So using $a or $b in "ordinary" code will sooner or later cause some
> programmer somewhere to spend a day chasing an obscure programming
> error.
I don't see how "the Perl compiler suppresses "stricture" for package
globals named $a or $b" (so that the traditional sort interface
continues to work) would lend itself to the conclusion that 'using a
properly declared lexical variable named $a or $b' will 'sooner or
later cause someone to spend a day on chasing an obscure programming
error'. Shouldn't there at least be a programming error before someone
needs to chase one?
------------------------------
Date: Sun, 10 Jul 2011 21:19:34 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: test two hash(refs) for equality
Message-Id: <877h7panxl.fsf@sapphire.mobileactivedefense.com>
"Uri Guttman" <uri@StemSystems.com> writes:
>>>>>> "RW" == Rainer Weikusat <rweikusat@mssgmbh.com> writes:
> RW> "Uri Guttman" <uri@StemSystems.com> writes:
> >>>>>>> "RW" == Rainer Weikusat <rweikusat@mssgmbh.com> writes:
> >>
> RW> "Uri Guttman" <uri@StemSystems.com> writes:
> >> >>>>>>> "RW" == Rainer Weikusat <rweikusat@mssgmbh.com> writes:
> >> >>
> RW> sub cmp_href_0($$)
> >> >>
> >> >> that prototype is useless. it doesn't do anything worth the
> >> >> bother.
> >>
> RW> It provides at least some sort of compile-time checking of function
> RW> calls and that's something I decidedly want to have ...
> >>
> >> barely and it can be bypassed with calls like &foo.
>
> RW> It will cause the compiler to make noises when a function is called
> RW> with less arguments than the prototype said it should have. I found
> RW> this to be useful to me.
>
> and to few other people. also it doesn't work at all with methods and
> most perl code is OO these days.
'Methods in Perl' can indeed be a PITA because there is no compiler
support for any kind of 'method call checks'. But that's not exactly a
reason for not trying to exploit compiler support for catching simple
errors in areas where it does exist.
> RW> You didn't bother to provide an explanation despite you now suggest
> RW> that you could have done so and I was (as I wrote in the text you have
> RW> chosen to delete) under the impression of having some experimentally
> RW> acquired data demonstrating that this pretty obvious check was
> RW> actually a bad idea.
> >>
> >> no, i didn't need to provide one. i said it was a better first pass
> >> check and i was right. you ignored it.
>
> RW> Trying to distinguish between gods who don't speak and real stones is
> RW> a waste of time. Feel free to behave like a stone and be ignored like
> RW> one.
>
> heh. ignorance is your area it seems.
It is somewhat stupid to accuse others of not being aware of facts
known to you you weren't willing to share with them. Especially after
those others have acquired knowledge of these facts nevertheless.
------------------------------
Date: Sun, 10 Jul 2011 14:54:56 -0700
From: "John W. Krahn" <jwkrahn@example.com>
Subject: Re: test two hash(refs) for equality
Message-Id: <RcpSp.38600$Md1.21030@newsfe19.iad>
Rainer Weikusat wrote:
> "John W. Krahn"<jwkrahn@example.com> writes:
>> Rainer Weikusat wrote:
>>> "Uri Guttman"<uri@StemSystems.com> writes:
>>>>>>>>> "RW" == Rainer Weikusat<rweikusat@mssgmbh.com> writes:
>>>>
>>>> RW> "Uri Guttman"<uri@StemSystems.com> writes:
>>>> >>>>>>> "RW" == Rainer Weikusat<rweikusat@mssgmbh.com> writes:
>>>> >>
>>>> RW> sub cmp_href_0($$)
>>>> >>
>>>> >> that prototype is useless. it doesn't do anything worth the
>>>> >> bother.
>>>>
>>>> RW> It provides at least some sort of compile-time checking of function
>>>> RW> calls and that's something I decidedly want to have ...
>>>>
>>>> barely and it can be bypassed with calls like&foo.
>>>
>>> It will cause the compiler to make noises when a function is called
>>> with less arguments than the prototype said it should have. I found
>>> this to be useful to me.
>>
>>
>> $ perl -le'
>> my %x = "a" .. "z";
>> my %y = "A" .. "Z";
>> my @refs = ( \%x, \%y );
>> sub cmp_href_0 ($$) {
>> my ( $a, $b ) = @_;
>> print %$a, %$b;
>> }
>> cmp_href_0( @refs );
>> '
>> Not enough arguments for main::cmp_href_0 at -e line 9, near "@refs )"
>> Execution of -e aborted due to compilation errors.
>>
>>
>>
>> But I passed an array containing two elements. Why doesn't it work?
>
> Why do you believe that an array evaluated in a scalar context would
> be equivalent to passing two scalar values when the evaluation returns
> only one?
But it works correctly without the prototype:
$ perl -le'
my %x = "a" .. "z";
my %y = "A" .. "Z";
my @refs = ( \%x, \%y );
sub cmp_href_0 {
my ( $a, $b ) = @_;
print %$a, %$b;
}
cmp_href_0( @refs );
'
wxefabmnstyzuvcdklqrghijopSTABOPWXKLYZEFQRMNCDIJGHUV
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
------------------------------
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 3441
***************************************