[32546] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3811 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Nov 5 18:09:23 2012

Date: Mon, 5 Nov 2012 15:09:09 -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           Mon, 5 Nov 2012     Volume: 11 Number: 3811

Today's topics:
    Re: array <ben@morrow.me.uk>
    Re: array <ben@morrow.me.uk>
    Re: array <ben@morrow.me.uk>
    Re: array <hjp-usenet2@hjp.at>
    Re: basic perl question <ben@morrow.me.uk>
    Re: Clear the "Wide character in print" warning and lea <ben@morrow.me.uk>
    Re: Copy array into input read process? <ben@morrow.me.uk>
    Re: Differential pattern match <graham.stow@stowassocs.co.uk>
    Re: lerning perl <ben@morrow.me.uk>
    Re: lerning perl <hjp-usenet2@hjp.at>
    Re: Parallel execution framework? <ben@morrow.me.uk>
    Re: perl and indent <ben@morrow.me.uk>
    Re: Regex question, limit repeats UNLESS within specifi <ben@morrow.me.uk>
    Re: Trampoline sub <rweikusat@mssgmbh.com>
    Re: Trampoline sub <derykus@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 5 Nov 2012 20:04:44 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: array
Message-Id: <snilm9-5q41.ln1@anubis.morrow.me.uk>


Quoth Henry Law <news@lawshouse.org>:
> On 29/10/12 21:46, Bill Cunningham wrote:
> >
> >      I have to use man perlintro on my linux. I'm not even through reading
> > it. I must've forgot how to use lists.
> 
> I've been through some of what you're going through and I'm happy to 
> help.  Mail me direct (address is valid) if you like.

Questions from beginners who are willing to learn are always welcome
here. Just don't be put off by the tendency of many here, including me
from time to time, to dive off into technicalities we find more
interesting. Noone is intending to intimidate you, so feel free to only
read the replies which you find actually helpful.

Ben



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

Date: Mon, 5 Nov 2012 20:18:45 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: array
Message-Id: <5ijlm9-5q41.ln1@anubis.morrow.me.uk>


Quoth "Peter J. Holzer" <hjp-usenet2@hjp.at>:
> 
> I would argue that a C pointer is *less* primitive than a Perl
> reference. A perl reference can only be dereferenced, assigned, and
> compared for equality. A C pointer has a much richer set on operations
> defined on it: In addition to the operations possible on a Perl
> reference (or a Pascal pointer), you can add or subtract an integer, you
> can subtract one pointer from another and you can order them.

Perl references can also be converted to string or integer, and thus can
be ordered. The stringification is not particularly meaningful, but is
unique among all live refs; the numification is consistent for a given
ref regardless of type changes, and is in fact more primitive in Perl
than comparison for equality. If you consider Scalar::Util part of the
core language (and I do, since you don't even get reliable equality
without it) refs can also be queried for internal and user-defined type.

You can also, of course, use C pointers in Perl, with un/pack "P", but
we don't like to talk about that :).

Ben



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

Date: Mon, 5 Nov 2012 20:26:20 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: array
Message-Id: <c0klm9-5q41.ln1@anubis.morrow.me.uk>


Quoth "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>:
> On 10/29/12 15:56, Bill Cunningham wrote:
> >      What's wrong in this code?
> >
> > use strict;
> > use warnings;
> >
> > my @cats=["striper","snowball"];
>             ^(                   ^)
> >
> > print $cats[0];
> > print $cats[1];
> 
> 
> [] creates a reference to an anonymous array reference.
> () creates a list.

Careful. [] creates a reference to an anonymous *array* (a reference to
an anonymous array reference would be \[1,2]). *Comma* creates a list.
() is for precedence only (with a very few rather special exceptions).

Ben



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

Date: Mon, 5 Nov 2012 23:51:49 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: array
Message-Id: <slrnk9ggo5.84i.hjp-usenet2@hrunkner.hjp.at>

On 2012-11-05 20:18, Ben Morrow <ben@morrow.me.uk> wrote:
> Quoth "Peter J. Holzer" <hjp-usenet2@hjp.at>:
>> I would argue that a C pointer is *less* primitive than a Perl
>> reference. A perl reference can only be dereferenced, assigned, and
>> compared for equality. A C pointer has a much richer set on operations
>> defined on it: In addition to the operations possible on a Perl
>> reference (or a Pascal pointer), you can add or subtract an integer, you
>> can subtract one pointer from another and you can order them.
>
> Perl references can also be converted to string or integer, and thus can
> be ordered.

Yes, but the order isn't meaningful. In C the the order is only defined
for pointers which point into the same object, but for those it is
meaningful.

> If you consider Scalar::Util part of the core language (and I do,
> since you don't even get reliable equality without it) refs can also
> be queried for internal and user-defined type.

Yes. And even without that you can use ref. Although I would argue that
this isn't a feature of the reference but of the referenced object.

	hp


-- 
   _  | Peter J. Holzer    | Fluch der elektronischen Textverarbeitung:
|_|_) | Sysadmin WSR       | Man feilt solange an seinen Text um, bis
| |   | hjp@hjp.at         | die Satzbestandteile des Satzes nicht mehr
__/   | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel


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

Date: Mon, 5 Nov 2012 19:59:25 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: basic perl question
Message-Id: <tdilm9-5q41.ln1@anubis.morrow.me.uk>


Quoth vis29 <dviswa@gmail.com>:
> I am going through a perl program, i have following lines which I dont
> understand, please explain what those lines does and why.
> 
> if (/^$hash{server}/) {
>    $name="not accessible";
>   }

On top of what others have said, this code is probably wrong. The
pattern match should probably be /^\Q$hash{server}/.

(Oh, and %hash should be called something more sensible, unless this is
a marijuana quality monitoring system.)

Ben



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

Date: Mon, 5 Nov 2012 21:04:00 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Clear the "Wide character in print" warning and leave the output unmangled
Message-Id: <07mlm9-5q41.ln1@anubis.morrow.me.uk>


Quoth "Peter J. Holzer" <hjp-usenet2@hjp.at>:
>
> To make it even more confusing, the string is marked as a character
> string (the UTF8 bit is on) instead of a byte string. This is definitely
> a bug in WebService::GData.

No, it isn't. It's the programmer's resposnsibility to track whether a
string represents bytes or characters; the SvUTF8 flag is none of your
business.

Ben



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

Date: Mon, 5 Nov 2012 20:41:58 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Copy array into input read process?
Message-Id: <mtklm9-5q41.ln1@anubis.morrow.me.uk>


Quoth "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>:
> On 11/01/12 15:43, Tuxedo wrote:
> > The below procedure, using lynx, simply dumps the source of a URL when
> > calling it with a single argument and prints each line:
> >
> > open(my $fh, '-|',
> > '/usr/bin/lynx','-source','-nonumbers','-cache=0','-nolist', $ARGV[0]) or
> > die $!;
> > while (my $line =<$fh>) {
> > print $line;
> > }
> >
> > Placed in a lynx.pl file, it can be run as
> > ./lynx.pl example.com
> >
> > Following the '-|' bit some fixed options are passed:
> > '/usr/bin/lynx','-source','-nonumbers','-cache=0','-nolist'
> > ... and thereafter the URL.
> >
> > How can this be better done in allowing arguments to be passed like:
> > ./lynx.pl example.com -source -nonumbers -cache=0 -nolist
> >
> > ... then capturing the arguments somehow like follows:
> > open(my $fh, '-|', '/usr/bin/lynx', @flags $ARGV[0]) or die $!;
> >
> > In the end it's not much different from running lynx directly but I would
> > like some fixed and some optional arguments passed, and I'm not sure how to
> > construct and push additional arguments into a list, then copy the final
> > array into the file handle reading process between the -| bit and the
> > $ARGV[0] to allow for a flexible number of command line options.
> 
> 
> my $cmd = q{ /usr/bin/lynx -source -nonumbers -cache=0 -nolist };
> 
> print `$cmd @ARGV`;

No. What happens if I run

    ./lynx.pl "; rm -rf /"

? (Not to mention the futility of capturing the output into a buffer
only to print it out again immediately.)

Try

    my @lynx = qw{ /usr/bin/lynx -source -nonumbers -cache=0 -nolist };
    open(my $fh, "-|", @lynx, @ARGV) or ...;

or maybe

    exec @lynx, @ARGV;

or, better, learn how to use shell.

Ben



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

Date: Mon, 5 Nov 2012 14:58:00 -0000
From: "Graham" <graham.stow@stowassocs.co.uk>
Subject: Re: Differential pattern match
Message-Id: <xYKdnacT875iTgrNnZ2dnUVZ7t6dnZ2d@bt.com>

>> If I retain the cluster parentheses, I get a similar result.
>>
>> String = [(?: (?: (?:(?<=[Xx])|\b) [12][0-9] (?:(?=[Xx])|\b) | [xX0-9] )
>> [ ]{0,2} )]
>> Chord = [(?: (?: (?:(?<=[Xx])|\b) [12][0-9] (?:(?=[Xx])|\b) | [xX0-9] )
>> [ ]{0,2} )(?: (?: (?:(?<=[Xx])|\b) [12][0-9] (?:(?=[Xx])|\b) | [xX0-9] )
>> [ ]{0,2} )(?: (?: (?:(?<=[Xx])|\b) [12][0-9] (?:(?=[Xx])|\b) | [xX0-9] )
>> [ ]{0,2} )(?: (?: (?:(?<=[Xx])|\b) [12][0-9] (?:(?=[Xx])|\b) | [xX0-9] )
>> [ ]{0,2} )(?: (?: (?:(?<=[Xx])|\b) [12][0-9] (?:(?=[Xx])|\b) | [xX0-9] )
>> [ ]{0,2} )(?: (?: (?:(?<=[Xx])|\b) [12][0-9] (?:(?=[Xx])|\b) | [xX0-9] )
>> [ ]{0,2} )]
>
> That's a bit more like it. This will at least match the same text as the
> (?:...){0,6} pattern above. However, it's still not capturing anything:
> you need to add some plain () capturing parens around the bit of the
> pattern you care about.
>
> Ben
>
A belated thanks to all who helped with this, but particularly Ben. I 
particularly appreciated Ben hinting at the way forward but not giving me 
the answer on a plate. The final capturing pattern match I eventually came 
up with was
/ ((?: (?: (?:(?<=[Xx])|\b) [12][0-9] (?:(?=[Xx])|\b) | [xX0-9] ) 
[ ]{0,2} ){6}) /x
which works a treat (see http://guitartabs2notes.cleverpages.co.uk ) I may 
expand this to include letters a-e (and A-E) which sometimes represent 
guitar frets 10to- 14 - any more than this and I'll be matching a whole host 
of 6 letter words as well as guitar chords. Cheers again everybody. 




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

Date: Mon, 5 Nov 2012 20:34:47 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: lerning perl
Message-Id: <7gklm9-5q41.ln1@anubis.morrow.me.uk>


Quoth "Peter J. Holzer" <hjp-usenet2@hjp.at>:
> 
> I don't think you can learn programming without a language (that would
> be like learning to write novels without a language).

Turing, Church and so on did; but most of us aren't in that category.

> (I'm not even sure if everybody can learn to program: Some people just
> don't seem to have the knack)

No. It's just like being a musician, or an artist: some people can do
it, some can't, and if you can't you might be able (with a lot of work)
to learn enough of the basics to bang out something approximately
reasonable, but you'll never do it well.

Ben



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

Date: Mon, 5 Nov 2012 23:55:18 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: lerning perl
Message-Id: <slrnk9ggum.84i.hjp-usenet2@hrunkner.hjp.at>

On 2012-11-05 20:34, Ben Morrow <ben@morrow.me.uk> wrote:
> Quoth "Peter J. Holzer" <hjp-usenet2@hjp.at>:
>> I don't think you can learn programming without a language (that would
>> be like learning to write novels without a language).
>
> Turing, Church and so on did; but most of us aren't in that category.

I would say they just invented their languages as they went along.

	hp


-- 
   _  | Peter J. Holzer    | Fluch der elektronischen Textverarbeitung:
|_|_) | Sysadmin WSR       | Man feilt solange an seinen Text um, bis
| |   | hjp@hjp.at         | die Satzbestandteile des Satzes nicht mehr
__/   | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel


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

Date: Mon, 5 Nov 2012 18:09:39 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Parallel execution framework?
Message-Id: <30clm9-3e31.ln1@anubis.morrow.me.uk>


Quoth Ignoramus13803 <ignoramus13803@NOSPAM.13803.invalid>:
> I have some tasks that are best done in parallel (because they involve
> a lot of waiting for the remote servers and networks). 
> 
> For the last 7 years or so, I have been using
> Parallel::ForkManager. It is nice because it is so simple, however, it
> has its limitations. 
> 
> I would like to see if there are any suggestions for a parallel
> framework that stays within one perl machine, but uses multithreading.

Do you mean threading? Perl ithreads are best kept away from IME, though
I know there are people who've done useful things with them.

My first recommendation would be POE.

Ben



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

Date: Mon, 5 Nov 2012 19:52:23 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: perl and indent
Message-Id: <n0ilm9-5q41.ln1@anubis.morrow.me.uk>

[Sorry for replying to the wrong post, but I've been away and my news
server has expired Henry's article/]

Quoth "Bill Cunningham" <billcun@suddenlink.net>:
> Henry Law wrote:
> > I suspect there may be a precise technical meaning of "I use indent";

Yes. The OP was talking about indent(1), which is a standard Unix
program for re-indenting C source. The equivalent for Perl is perltidy,
though as you say most editors can do a reasonable job.

> > * I find two characters of indentation plenty; three at most.  You may
> > need to customise your editor to get that.

[ObReligiousWar: Ohh, ick! I can't read 2-char indents, they make my
eyes start swimming... (and this is with a terminal font probably twice
as big as most people use).]

> > * I advocate using "soft tabs" (spaces) rather than hard tabs.
> > Occasionally I'll "cat" (Linux) or "type" (Windows) a Perl file just
> > to have a look at it and the built-in tab is usually far too big
> > (like 8).

Using hard tabs to represent anything other than 8-stop increments is
stupid. Many editors have a mode where they represent initial multiples
of 8 columns as hard tabs, with spaces for the rest; personally I don't
see the point, but at least the files don't become unreadable on someone
else's machine. (expand(1) and unexpand(1) convert from this
representation to all-spaces and back.)

Ben



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

Date: Mon, 5 Nov 2012 20:59:07 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Regex question, limit repeats UNLESS within specified tags
Message-Id: <rtllm9-5q41.ln1@anubis.morrow.me.uk>


Quoth Jason C <jwcarlton@gmail.com>:
> I'm currently limiting repeated characters like so:
> 
> $text =~ s#(.)\1{6,}#$1$1$1$1$1$1#gsi;

    $text =~ s#((.)\1{5})\2*#\1#gsi;

or use \K:

    $text =~ s#(.)\1{5}\K\1*##gsi;

> I'm wanting to modify it to only limit repeated characters if they're
> not within <img...> or <a href=...></a> tags.

Use an HTML parser.

> Neither of these are going to be perfect, though, because:
> 
> 1. in the first one, I need to test for both an opening <img and an
> ending >; otherwise, I think it would not catch something like "<img
> src='aaa.jpg'> bbbbbbbbbb" (since the repeated "b" comes after "<img").
> 
> 2. in the second one, I also need to test for the ending >, but also for
> the closing </a>. Even if I fixed the ending >, I could still end up
> with a confusing "<a
> href='http://www.aaaaaaaaaa.com'>http://www.aaaaaa.com</a>"

Yes. Exactly.

Ben



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

Date: Mon, 05 Nov 2012 14:45:43 +0000
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Trampoline sub
Message-Id: <87zk2wqfag.fsf@sapphire.mobileactivedefense.com>

Rainer Weikusat <rweikusat@mssgmbh.com> writes:

[...]

> And you usage of 'aliasing' is - at best - confusing (I'm tempted to
> call it 'outright wrong').

Practical example of that: When building state machines in C, I
usually use a function pointer as 'state variable' because this
implies there's no need to explicitly written, state-dependent control
transfer code. The first time I did this in Perl, it occurred to me
that it should be possible to simplify the 'obvious' implementation,
using a module-global scalar variable holding a reference to the
subroutine to be executed next, to one which just invokes the
subroutine by name and change what this name points to. A contrived
example showing my original approach: Assuming the code included below
exists in some location where Perl finds it with the name FlipFlop.pm,

------------
package FlipFlop;

require Exporter;

our @ISA = qw(Exporter);
our @EXPORT = qw(v);

sub v0 {
    *v = \&v1;
    return 0;
}

*v = \&v0;

sub v1 {
    *v = \&v0;
    return 1;
}

1;
------------

I expected that using it like this:

------------
use FlipFlop;

while (1) {
    print(v(), "\n");
    sleep 1;
}
------------

would result in printing a sequence of alternating 0s and 1s, based on
knowing the 'aliasing' statements in perlmod but nothing about the
actual implementation (and without thinking very much about it). This
is, of course, wrong because the assignment done as part of the import
happens once and future changes of the supposedly 'imported' symbol
won't propagate to modules which supposedly 'imported' it.

In hindsight, the documentation on this seems clear enough, but 'in
foresight', it wasn't.


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

Date: Mon, 5 Nov 2012 12:11:32 -0800 (PST)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: Trampoline sub
Message-Id: <9b98a488-7d37-4c9d-a4ae-901a9022356b@googlegroups.com>

On Monday, November 5, 2012 6:45:45 AM UTC-8, Rainer Weikusat wrote:
> Rainer Weikusat <rweikusat@mssgmbh.com> writes:
> 
> 
> 
> [...]
> 
> 
> 
> > And you usage of 'aliasing' is - at best - confusing (I'm tempted to
> 
> > call it 'outright wrong').
> 
> 
> 
> Practical example of that: When building state machines in C, I
> 
> usually use a function pointer as 'state variable' because this
> 
> implies there's no need to explicitly written, state-dependent control
> 
> transfer code. The first time I did this in Perl, it occurred to me
> 
> that it should be possible to simplify the 'obvious' implementation,
> 
> using a module-global scalar variable holding a reference to the
> 
> subroutine to be executed next, to one which just invokes the
> 
> subroutine by name and change what this name points to. A contrived
> 
> example showing my original approach: Assuming the code included below
> 
> exists in some location where Perl finds it with the name FlipFlop.pm,
> 
> 
> 
> ------------
> 
> package FlipFlop;
> 
> 
> 
> require Exporter;
> 
> 
> 
> our @ISA = qw(Exporter);
> 
> our @EXPORT = qw(v);
                -----
                qw(*v);   # <---------- 

Ran into this by accident.  Exporting the actual glob 
makes your flipflop work:

   our @EXPORT = qw( *v );

$ perl -MFlipFlop -le 'while(1) { print v();sleep 1}'
0
1
 ...


Here's the relevant bit from perlmod:


   What makes all of this important is that the 
   Exporter module uses glob aliasing as the import/
   export mechanism. Whether or not you can properly
   localize a variable that has been exported from a
   module depends on how it was exported:

    @EXPORT = qw($FOO); # Usual form, can't be 
                        #   localized
    @EXPORT = qw(*FOO); # Can be localized



> 
> 
> 
> sub v0 {
> 
>     *v = \&v1;
> 
>     return 0;
> 
> }
> 
> 
> 
> *v = \&v0;
> 
> 
> 
> sub v1 {
> 
>     *v = \&v0;
> 
>     return 1;
> 
> }
> 
> 
> 
> 1;
> 
> ------------
> 
> 
> 
> I expected that using it like this:
> 
> 
> 
> ------------
> 
> use FlipFlop;
> 
> 
> 
> while (1) {
> 
>     print(v(), "\n");
> 
>     sleep 1;
> 
> }
> 
> ------------
> 
> 
> 
> would result in printing a sequence of alternating 0s and 1s, based on
> 
> knowing the 'aliasing' statements in perlmod but nothing about the
> 
> actual implementation (and without thinking very much about it). This
> 
> is, of course, wrong because the assignment done as part of the import
> 
> happens once and future changes of the supposedly 'imported' symbol
> 
> won't propagate to modules which supposedly 'imported' it.
> ...
> 

So, evidently, Perl has to be able localize the
glob... to do the glob twiddle on the fly. But
I'm at a loss about the internals.


-- 
Charles DeRykus


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

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


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