[31711] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2974 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Jun 6 18:09:25 2010

Date: Sun, 6 Jun 2010 15:09:06 -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, 6 Jun 2010     Volume: 11 Number: 2974

Today's topics:
        [Tk] Get hexa code color from name <genomart@gmail.com>
        How to get the composite type information? <pengyu.ut@gmail.com>
    Re: How to get the composite type information? <cwilbur@chromatico.net>
    Re: How to get the composite type information? <pengyu.ut@gmail.com>
    Re: How to get the composite type information? <cwilbur@chromatico.net>
    Re: How to switch floating decimal number char from "." <ben@morrow.me.uk>
    Re: ignoring namespaces? <peter.nosp@m.silmaril.ie>
        reference to anonymous scalar <pengyu.ut@gmail.com>
    Re: reference to anonymous scalar <tadmc@seesig.invalid>
    Re: reference to anonymous scalar <derykus@gmail.com>
    Re: The usage of $_[0] <hjp-usenet2@hjp.at>
        using bash process substitution in perl pipe? <pengyu.ut@gmail.com>
    Re: using bash process substitution in perl pipe? (Alan Curry)
    Re: using bash process substitution in perl pipe? <ben@morrow.me.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sun, 6 Jun 2010 11:03:49 -0700 (PDT)
From: perlgenome <genomart@gmail.com>
Subject: [Tk] Get hexa code color from name
Message-Id: <a2f0d45e-3abb-4ece-bc54-d98a36ebfde4@i28g2000yqa.googlegroups.com>

Dear,

In a widget Tk, How can I can hexa code color from a color name ?

For example: which method can give the hexa code of  red name
print $widget->METHOD('red'); #  #FF0000

Best Regards,

Djibril


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

Date: Sat, 5 Jun 2010 07:52:32 -0700 (PDT)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: How to get the composite type information?
Message-Id: <be710270-1546-4f62-8903-0d24bd5951ce@t10g2000yqg.googlegroups.com>

ref is not capable to getting composite type information. In the
following example, I actually want to print that the type is array of
array (I would be better if it can print the first a few element in an
array). Is there a function for this in perl?

$ cat arrary_of_array.pl
#!/usr/bin/env perl

use strict;
use warnings;

my $aref=[
[1, 2, 3]
, [4, 5, 6]
, [7, 8, 9]
];
print ref($aref), "\n";

$ ./arrary_of_array.pl
ARRAY


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

Date: Sat, 05 Jun 2010 14:26:03 -0400
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: How to get the composite type information?
Message-Id: <86vd9x9w04.fsf@mithril.chromatico.net>

>>>>> "PY" == Peng Yu <pengyu.ut@gmail.com> writes:

    PY> ref is not capable to getting composite type information. In the
    PY> following example, I actually want to print that the type is
    PY> array of array (I would be better if it can print the first a
    PY> few element in an array). 

That's because the Perl interpreter does not care about composite types.
There is nothing constraining an array to only contain only arrayrefs
except the programmer, so for the interpreter to call it an array of
arrayrefs would be inaccurate - the interpreter cannot tell if it is
intentionally an array of only arrayrefs, or it just happened that way
by accident.

    PY> Is there a function for this in perl?

Not in core Perl.  But it should be trivial for you to write one, if you
need it.

Charlton


-- 
Charlton Wilbur
cwilbur@chromatico.net


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

Date: Sat, 5 Jun 2010 13:05:42 -0700 (PDT)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: Re: How to get the composite type information?
Message-Id: <e519f2be-4f23-4b1e-b413-29e543353647@z33g2000vbb.googlegroups.com>

On Jun 5, 1:26=A0pm, Charlton Wilbur <cwil...@chromatico.net> wrote:
> >>>>> "PY" =3D=3D Peng Yu <pengyu...@gmail.com> writes:
>
> =A0 =A0 PY> ref is not capable to getting composite type information. In =
the
> =A0 =A0 PY> following example, I actually want to print that the type is
> =A0 =A0 PY> array of array (I would be better if it can print the first a
> =A0 =A0 PY> few element in an array).
>
> That's because the Perl interpreter does not care about composite types.
> There is nothing constraining an array to only contain only arrayrefs
> except the programmer, so for the interpreter to call it an array of
> arrayrefs would be inaccurate - the interpreter cannot tell if it is
> intentionally an array of only arrayrefs, or it just happened that way
> by accident.
>
> =A0 =A0 PY> Is there a function for this in perl?
>
> Not in core Perl. =A0But it should be trivial for you to write one, if yo=
u
> need it.

Does any add-on package have this function? I don't know what you mean
by 'trivial'. But I think it would take some effort to make one
function to robustly handle arbitrarily complex cases.


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

Date: Sat, 05 Jun 2010 16:13:55 -0400
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: How to get the composite type information?
Message-Id: <86631x9r0c.fsf@mithril.chromatico.net>

>>>>> "PY" == Peng Yu <pengyu.ut@gmail.com> writes:

    PY> I don't know what you mean by 'trivial'. But I think it would
    PY> take some effort to make one function to robustly handle
    PY> arbitrarily complex cases.

"Trivial" means that once you have figured out what you want it to do,
it is largely a simple matter of typing the code; it should not require
much thought.

Charlton


-- 
Charlton Wilbur
cwilbur@chromatico.net


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

Date: Sat, 5 Jun 2010 21:33:20 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: How to switch floating decimal number char from "." to "," ?
Message-Id: <gtoqd7-9gn.ln1@osiris.mauzo.dyndns.org>


Quoth Tad McClellan <tadmc@seesig.invalid>:
> Pete Sammet <petesammet@mail.org> wrote:
> 
> > However in Europe another format is used:
> >
> > 123456,78
> 
> "Europe" is many many locales. I will assume czechoslovakia as it
> is one of those that use comma as the decimal point.
> 
> > How exactly can I switch from the first to the second format?
> 
>     perldoc perllocale
> 
> ----------------------------
> #!/usr/bin/perl
> use warnings;
> use strict;
> 
> use locale;
> use POSIX qw(locale_h LC_NUMERIC);
> setlocale(LC_NUMERIC, "cs_CZ");

This is a bad plan. Locales (specifically, the 'locale' pragma) and
Unicode don't play nicely together in Perl, and if you're processing
international text you will probably end up with Unicode strings. A
better answer would be to use a CPAN module for number formatting, such
as Number::Format.

Ben



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

Date: Sun, 06 Jun 2010 14:15:54 +0100
From: Peter Flynn <peter.nosp@m.silmaril.ie>
Subject: Re: ignoring namespaces?
Message-Id: <871lcaFcogU1@mid.individual.net>

bugbear wrote:
[...]
> I also considered walking the entire tree REMOVING namespaces,
> but that doesn't sound like a high performance solution.

sed -e "s+<\([/]*\)\([^:]*:\)+<\1+g" ?

///Peter


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

Date: Sat, 5 Jun 2010 07:46:32 -0700 (PDT)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: reference to anonymous scalar
Message-Id: <d5990483-50d0-40fb-9b70-aa92bfe206f6@k39g2000yqb.googlegroups.com>

I could use [] to define a reference to an anonymous.

$aref=[1, 2, 3];

Is there a way to define a reference to anonymous scalar instead of
having to give the scalar a name first ('x' in the following example)?

$x=10;
$y=\$x;


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

Date: Sat, 05 Jun 2010 11:15:07 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: reference to anonymous scalar
Message-Id: <slrni0ktkd.fl2.tadmc@tadbox.sbcglobal.net>

Peng Yu <pengyu.ut@gmail.com> wrote:
> I could use [] to define a reference to an anonymous.
>
> $aref=[1, 2, 3];
>
> Is there a way to define a reference to anonymous scalar instead of
> having to give the scalar a name first ('x' in the following example)?
>
> $x=10;
> $y=\$x;


    my $y = \10;


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


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

Date: Sat, 5 Jun 2010 12:00:41 -0700 (PDT)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: reference to anonymous scalar
Message-Id: <dd6c3fda-baaa-41bd-ac6d-e7c1ed88e7fd@r5g2000prf.googlegroups.com>

On Jun 5, 7:46=A0am, Peng Yu <pengyu...@gmail.com> wrote:
> I could use [] to define a reference to an anonymous.
>
> $aref=3D[1, 2, 3];
>
> Is there a way to define a reference to anonymous scalar instead of
> having to give the scalar a name first ('x' in the following example)?
>
> $x=3D10;
> $y=3D\$x;

As shown earlier: my $y =3D \10 will create a
"reference to a constant". But that differs
from a "reference to an anonymous scalar":

 my $y =3D \10;
 $$y =3D 15; # error: mod. of read-only...

A "reference to an anonymous scalar" though
won't produce an error:

my $y =3D \ do { my $tmp };
$$y =3D 10;
$$y =3D 15;


--
Charles DeRykus


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

Date: Sun, 6 Jun 2010 00:38:05 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: The usage of $_[0]
Message-Id: <slrni0lked.h7o.hjp-usenet2@hrunkner.hjp.at>

On 2010-06-04 15:20, Peng Yu <pengyu.ut@gmail.com> wrote:
> I think that $_[0] should give me 'a' in the following example. But it
> doesn't. Could you help understand why? How to get the first argument?
>
> $ ./main.pl
> ab
> b
>

That's not true. Your program prints

ab <newline>
b <newline>
a1 <no newline here>

(The shell prompt may obscure the last line due to the missing newline,
but in any case there is no empty line after "b")

As an exercise, figure out why 

>   print $_[0], print $_[1], "\n";
> }

prints "b\na1".

	hp


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

Date: Sat, 5 Jun 2010 15:30:06 -0700 (PDT)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: using bash process substitution in perl pipe?
Message-Id: <f3faff34-3471-4628-9b7e-6c713b91ce6a@a20g2000vbc.googlegroups.com>

I want to use bash instead of sh because I need the process
substitution. But by default perl use sh instead of bash. Is there a
way to let perl use bash?

#!/usr/bin/perl

use warnings;
use strict;

open(IN, 'ls <(echo main.txt) |');
foreach (<IN>) {
  print
}


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

Date: Sat, 5 Jun 2010 23:24:01 +0000 (UTC)
From: pacman@kosh.dhis.org (Alan Curry)
Subject: Re: using bash process substitution in perl pipe?
Message-Id: <huemah$dua$1@speranza.aioe.org>

In article <f3faff34-3471-4628-9b7e-6c713b91ce6a@a20g2000vbc.googlegroups.com>,
Peng Yu  <pengyu.ut@gmail.com> wrote:
>I want to use bash instead of sh because I need the process
>substitution. But by default perl use sh instead of bash. Is there a
>way to let perl use bash?
>
>#!/usr/bin/perl
>
>use warnings;
>use strict;
>
>open(IN, 'ls <(echo main.txt) |');
>foreach (<IN>) {
>  print
>}

call bash -c 'your command' explicitly, like this:

  open IN, '-|', $shell, '-c', $cmd

Where $shell is 'bash' or '/usr/bin/bash' or something else that names the
shell you want, and $cmd is the 'ls <(echo main.txt)' string

-- 
Alan Curry


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

Date: Sun, 6 Jun 2010 01:15:36 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: using bash process substitution in perl pipe?
Message-Id: <8u5rd7-f5p.ln1@osiris.mauzo.dyndns.org>


Quoth Peng Yu <pengyu.ut@gmail.com>:
> I want to use bash instead of sh because I need the process
> substitution. But by default perl use sh instead of bash. Is there a
> way to let perl use bash?
> 
> #!/usr/bin/perl
> 
> use warnings;
> use strict;
> 
> open(IN, 'ls <(echo main.txt) |');

This is probably not what you want here. This will give output like (on
my system, for some reason, bash doesn't use /dev/fd)

    /var/tmp//sh-np-1275780961

In this particular case I suspect you just want backticks:

    open my $IN, 'ls $(echo main.txt) |';

You should also be checking the return value of open, or using autodie.
Yes, even in examples, as otherwise people will assume you don't know
that (unless you say something like 'error checking omitted').

> foreach (<IN>) {
>   print
> }

Alan Curry has already posted the simple answer (use bash explicitly,
instead of allowing two-arg open to default to sh). A more portable
solution that doesn't require bash (but does require a /dev/fd
directory) is to do the conversion yourself:

    use autodie;    # easier than checking every call
    use Fcntl;

    sub devfd {
        open my $FD, @_;

        # clear the close-on-exec flag, so the file stays open in the
        # child process
        my $fl = fcntl $FD, F_GETFD, 0;
        fcntl $FD, F_SETFD, ($fl & ~FD_CLOEXEC);

        # the files in /dev/fd (on systems that have that directory) are
        # magic, and opening them will return a dup of an existing file
        # descriptor
        return "/dev/fd/" . fileno $FD;
    }

    my $echo = devfd "-|", qw/echo main.txt/;
    open my $IN, "-|", "ls", $echo;

It's also possible to do this with named pipes (which is what bash
apparently does on my system, even though I've got a perfectly good
/dev/fd directory) but this is a little harder. Apart from creating the
pipe with mkfifo, you have to manually fork/exec the first child so you
can point its stdout at the pipe, and you need a SIGCHILD handler to
clean up the fifo when you've finished with it.

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:

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


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