[31525] in Perl-Users-Digest
Perl-Users Digest, Issue: 2784 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jan 23 00:09:22 2010
Date: Fri, 22 Jan 2010 21:09:06 -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 Fri, 22 Jan 2010 Volume: 11 Number: 2784
Today's topics:
Re: help with perl search/replace regex <tadmc@seesig.invalid>
Re: How to dissect a Regexp object? <ben@morrow.me.uk>
Re: How to dissect a Regexp object? <nospam-abuse@ilyaz.org>
Re: How to dissect a Regexp object? <ben@morrow.me.uk>
Re: How to dissect a Regexp object? <derykus@gmail.com>
Re: need some help excluding with file::find::rule <tadmc@seesig.invalid>
Re: need some help excluding with file::find::rule <globalsec@hotmail.com>
Re: need some help excluding with file::find::rule <tadmc@seesig.invalid>
Re: User-defined substitution <ben@morrow.me.uk>
Re: writing to terminal even with STDOUT and STDERR red <paduille.4061.mumia.w+nospam@earthlink.net>
Re: writing to terminal even with STDOUT and STDERR red <nospam-abuse@ilyaz.org>
Re: writing to terminal even with STDOUT and STDERR red <derykus@gmail.com>
Re: writing to terminal even with STDOUT and STDERR red <ben@morrow.me.uk>
Re: writing to terminal even with STDOUT and STDERR red <derykus@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 22 Jan 2010 16:29:00 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: help with perl search/replace regex
Message-Id: <slrnhlk9ev.qbg.tadmc@tadbox.sbcglobal.net>
solaristar <globalsec@hotmail.com> wrote:
> On Jan 22, 10:26Â am, solaristar <global...@hotmail.com> wrote:
>> I thought $ matched to the end of the line?
>
> figured it out
> thanks to
>
> http://www.comp.leeds.ac.uk/Perl/matching.html
The very first place to look for Perl help are the docs that
come with perl.
You needed to know about a regex metacharacter, and the "Metacharacters"
section in
perldoc perlre
says:
$ Match the end of the line (or before newline at the end)
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
------------------------------
Date: Fri, 22 Jan 2010 23:29:25 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: How to dissect a Regexp object?
Message-Id: <lvop27-r2f2.ln1@osiris.mauzo.dyndns.org>
Quoth kj <no.email@please.post>:
>
> Is there a way to tell if a given Regexp object, generated at
> runtime, includes at least one pair of capture parentheses?
>
> More generally, is there any documentation for the Regexp class?
> (I'm referring to the class alluded to by the output of, e.g., ref
> qr//). Running perldoc Regexp fails ("no docs found"), and perldoc
> perlre does not say much at all about this class as such.
The Regexp 'class' doesn't have any methods[1], and isn't really useable
as a class at all. In Perl 5.12 it will be mostly replaced by a new
REGEXP svtype, which was what it should have been from the beginning.
(qr// will still return a ref blessed into Regexp, for compatibility.)
Ben
[1] Well, it has a completely empty DESTROY method, because not having
one caused a problem somewhere.
------------------------------
Date: Sat, 23 Jan 2010 00:24:29 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: How to dissect a Regexp object?
Message-Id: <slrnhlkgdt.3vj.nospam-abuse@powdermilk.math.berkeley.edu>
On 2010-01-22, Ben Morrow <ben@morrow.me.uk> wrote:
> The Regexp 'class' doesn't have any methods[1], and isn't really useable
> as a class at all. In Perl 5.12 it will be mostly replaced by a new
> REGEXP svtype, which was what it should have been from the beginning.
> (qr// will still return a ref blessed into Regexp, for compatibility.)
Is there any way to find that an object is of a REGEXP svtype (without
using overload::StrVal)? Without this, serialization is not possible;
witness failure of FreezeThaw...
Or, at least, get hints that it "might be REGEXP" (with no false
negatives), so that a call to overload::StrVal() is needed...
Yours,
Ilya
------------------------------
Date: Sat, 23 Jan 2010 00:53:13 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: How to dissect a Regexp object?
Message-Id: <pstp27-drf2.ln1@osiris.mauzo.dyndns.org>
Quoth Ilya Zakharevich <nospam-abuse@ilyaz.org>:
> On 2010-01-22, Ben Morrow <ben@morrow.me.uk> wrote:
> > The Regexp 'class' doesn't have any methods[1], and isn't really useable
> > as a class at all. In Perl 5.12 it will be mostly replaced by a new
> > REGEXP svtype, which was what it should have been from the beginning.
> > (qr// will still return a ref blessed into Regexp, for compatibility.)
>
> Is there any way to find that an object is of a REGEXP svtype (without
> using overload::StrVal)? Without this, serialization is not possible;
> witness failure of FreezeThaw...
>
> Or, at least, get hints that it "might be REGEXP" (with no false
> negatives), so that a call to overload::StrVal() is needed...
As of 5.10, re::is_regexp will reliably identify a compiled regex,
regardless of reblessing or overloading. Under 5.10 it looks for a ref
to a scalar with 'r' magic (but correctly ignores the weird 'r' magic
used by /(??{})/), under 5.12 it will look for a REGEXP or a ref to a
REGEXP.
I don't think re is dual-life, so this isn't portable to pre-5.10,
though presumably it could be emulated with B if necessary.
Ben
------------------------------
Date: Fri, 22 Jan 2010 20:48:18 -0800 (PST)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: How to dissect a Regexp object?
Message-Id: <36630f29-2202-430b-a6d7-d9fc530f5412@m16g2000yqc.googlegroups.com>
On Jan 22, 9:54=A0am, kj <no.em...@please.post> wrote:
> Is there a way to tell if a given Regexp object, generated at
> runtime, includes at least one pair of capture parentheses?
> ...
If you have a recently current Perl version, you might be able
to leverage re::regexp_pattern in list context to check paren's.
On a Win32 5.10.1 strawberry distro for instance:
c:\strawberry\perl\bin\perl.exe -le "
use re 'regexp_pattern';
$r =3D qr/ab(\d+)/;
($pat) =3D regexp_pattern($r);
print $pat"
ab(\d+)
So you could parse $pat for capturing paren's. You'd need to
exclude certain assertions such as (? ... ) but that's left
as an exercise for the reader :)
--
Charles DeRykus
------------------------------
Date: Fri, 22 Jan 2010 16:46:58 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: need some help excluding with file::find::rule
Message-Id: <slrnhlkagk.qbg.tadmc@tadbox.sbcglobal.net>
solaristar <globalsec@hotmail.com> wrote:
> John,
John who?
Please learn the proper way of composing a followup:
http://web.presby.edu/~nnqadmin/nnq/nquote.html
Have you seen the Posting Guidelines that are posted here frequently?
> thanks for the reply, ok so i read your notes
What notes?
(we wouldn't have to ask if you had quoted the part of John's post
that you are referring to...)
> I must still be doing something wrong
You are doing many things wrong.
> $|=1;
Why do you think that you need to enable autoflush?
> my @data_dir =
> qw { /data/backups }; # list here the data dir if you want to
> loop on it.
You should either
1) ensure all lines are shorter than your newsreader's line length
or
2) figure out how to disable your newsreader's word wrapping
so that the code you post is code that can be run without syntax errors.
> my $server = '.[cmu]w';
That matches only 3 things:
.cw
.mw
.uw
I thought you wanted to match any string that *ended* with those:
my $server = '*.[cmu]w';
What you have matches only if the string is *equal* to one of those.
> my @files = glob "/data*/backups/$server/daily.0/$server";
What is the point of this operation?
You never use the @files variable again...
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
------------------------------
Date: Fri, 22 Jan 2010 17:49:55 -0800 (PST)
From: solaristar <globalsec@hotmail.com>
Subject: Re: need some help excluding with file::find::rule
Message-Id: <a0ff7588-f345-4f5d-aa0e-63b6c8280374@l11g2000yqb.googlegroups.com>
On Jan 22, 2:46=A0pm, Tad McClellan <ta...@seesig.invalid> wrote:
> solaristar <global...@hotmail.com> wrote:
> > John,
>
> John who?
>
> Please learn the proper way of composing a followup:
>
> =A0 =A0http://web.presby.edu/~nnqadmin/nnq/nquote.html
>
> Have you seen the Posting Guidelines that are posted here frequently?
Ok pardon me for not being an avid "Usenet" subscriber, lesson learned
>
> > thanks for the reply, ok so i read your notes
>
> What notes?
>
> (we wouldn't have to ask if you had quoted the part of John's post
> that you are referring to...)
>
Understood.
> > I must still be doing something wrong
>
> You are doing many things wrong.
>
> > $|=3D1;
>
> Why do you think that you need to enable autoflush?
>
Trying to debug the program
> > my @data_dir =3D
> > =A0 qw { /data/backups }; =A0 =A0# list here the data dir if you want t=
o
> > loop on it.
>
> You should either
>
> =A0 =A01) ensure all lines are shorter than your newsreader's line length
> or
> =A0 =A02) figure out how to disable your newsreader's word wrapping
>
> so that the code you post is code that can be run without syntax errors.
>
No clue how to do that, I'll look into it. I'm simply using firefox
> > my $server =3D '.[cmu]w';
>
> That matches only 3 things:
> =A0 =A0 .cw
> =A0 =A0 .mw
> =A0 =A0 .uw
>
> I thought you wanted to match any string that *ended* with those:
>
> =A0 =A0 my $server =3D '*.[cmu]w';
>
> What you have matches only if the string is *equal* to one of those.
>
My intent is to return any "filename" that contains .[umc]w. within
the name; for example /data/backups/db01.foobar.mw.blahbalh.com or /
data3/backups/db02.foobar2.uw.blahblah.com, but NOT
db01.blahbalah.ml.blahblah.com
> > my @files =3D glob "/data*/backups/$server/daily.0/$server";
>
> What is the point of this operation?
>
> You never use the @files variable again...
>
that was for later in the script, i just posted a snippet
> --
> Tad McClellan
> email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
------------------------------
Date: Fri, 22 Jan 2010 22:59:32 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: need some help excluding with file::find::rule
Message-Id: <slrnhll0b6.qqq.tadmc@tadbox.sbcglobal.net>
solaristar <globalsec@hotmail.com> wrote:
> On Jan 22, 2:46Â pm, Tad McClellan <ta...@seesig.invalid> wrote:
>> > Â qw { /data/backups }; Â Â # list here the data dir if you want to
>> > loop on it.
>>
>> You should either
>>
>> Â Â 1) ensure all lines are shorter than your newsreader's line length
>> or
>> Â Â 2) figure out how to disable your newsreader's word wrapping
>>
>> so that the code you post is code that can be run without syntax errors.
>>
>
> No clue how to do that,
Open an editor on your program file.
Find lines that are longer than about 72 characters.
Make them shorter.
qw { /data/backups }; Â Â # list here the data dir
# if you want to loop on it.
> that was for later in the script, i just posted a snippet
Cut out parts of the snippet that are irrelevant, else you'll get
comments that are also irrelevant.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
------------------------------
Date: Fri, 22 Jan 2010 23:17:46 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: User-defined substitution
Message-Id: <q9op27-r2f2.ln1@osiris.mauzo.dyndns.org>
Quoth "Peter J. Holzer" <hjp-usenet2@hjp.at>:
> I want to provide non-trusted users with a way to make substitutions
> similar to s///. To my surprise this isn't an FAQ (although there is a
> similar problem - "How can I expand variables in text strings?"), but
> maybe I shouldn't be surprised because this is the first time I needed
> to do that myself ;-).
>
> So here is what I came up with - please try to shoot it down:
>
>
> sub replace {
> my ($string, $pattern, $replacement) = @_;
>
> if (my @m = $string =~ m/$pattern/p) {
> for my $i (1 .. @m) {
> $replacement =~ s/\$$i(?=\D|)/$m[$i-1]/e;
> }
> return ${^PREMATCH} . $replacement . ${^POSTMATCH};
I don't think this behaviour is documented. That is, AIUI
${^{PRE,POST}MATCH} should be considered undefined (in the C sense, not
the Perl sense) after a match which didn't specify /p.
> } else {
> return $string;
> }
> }
>
> All three strings are potentially untrusted ($string comes from a
> database query, $pattern and $replacement are supplied by the user.
>
> The function should do the same as
> $string =~ s/$pattern/$replacement/;
> except that $<number> references to capture buffers are resolved, too.
>
> I think this is safe:
>
> * The match itself should be safe unless "use re 'eval'" is active
> (it isn't).
> * The s///e only evaluates to $m[$i-1], it doesn't evaluate the
> content, so it cannot be used to inject code.
s///e is always safe. It's only when you start adding more /es that it
becomes unsafe.
> There is one catch, though: If $pattern doesn't contain any capturing
> parentheses, @m is set to (1) on a successful match, which isn't
> distinguishable from a pattern which captures one string "1". I guess I
> could try to analyze $pattern or just document that at least one set of
> parentheses must be used.
$#+ gives the number of sets of capturing parens in the last successful
match.
Ben
------------------------------
Date: Fri, 22 Jan 2010 16:20:35 -0600
From: "Mumia W." <paduille.4061.mumia.w+nospam@earthlink.net>
Subject: Re: writing to terminal even with STDOUT and STDERR redirected
Message-Id: <q-GdncKA2_aOu8fWnZ2dnUVZ_qCdnZ2d@earthlink.com>
On 01/22/2010 11:31 AM, Dilbert wrote:
> I have a first perl-program ("first.pl") that calls a second perl-
> program ("second.pl") and captures STDOUT and STDERR.
> Is there a way inside "second.pl" to magically open a filehandle to
> the terminal, even though STDOUT and STDERR are both redirected ?
> I'm working under Ubuntu Linux.
>
> first.pl
> ======
> use strict;
> use warnings;
> system 'perl second.pl >file1.txt 2>file2.txt';
>
> second.pl
> ======
> use strict;
> use warnings;
> print STDOUT "This is STDOUT\n";
> print STDERR "This is STDERR\n";
> open my $fh, '>', ??magic-terminal?? or die "Error: Can't open magic
> terminal because $!";
> print {$fh} "This is magically going to terminal\n";
> close $fh;
The solution is probably platform-specific. Since you're on Linux,
you can use the "tty" command:
my $tty = `tty`;
chomp $tty;
print STDOUT "This is STDOUT.\n";
print STDERR "This is STDERR.\n";
open my $fh, '>', $tty or die ("open failure: $!");
print $fh "Magic terminal says 'Hello.'\n";
close $fh;
------------------------------
Date: Sat, 23 Jan 2010 00:27:53 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: writing to terminal even with STDOUT and STDERR redirected
Message-Id: <slrnhlkgk9.3vj.nospam-abuse@powdermilk.math.berkeley.edu>
On 2010-01-22, Dilbert <dilbert1999@gmail.com> wrote:
> I have a first perl-program ("first.pl") that calls a second perl-
> program ("second.pl") and captures STDOUT and STDERR.
> Is there a way inside "second.pl" to magically open a filehandle to
> the terminal, even though STDOUT and STDERR are both redirected ?
perldoc Term::ReadLine;
> I'm working under Ubuntu Linux.
Does not matter...
Hope this helps,
Ilya
------------------------------
Date: Fri, 22 Jan 2010 16:46:42 -0800 (PST)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: writing to terminal even with STDOUT and STDERR redirected
Message-Id: <13207cf9-da3e-426b-afd0-8fe99d0edc6b@k35g2000yqb.googlegroups.com>
On Jan 22, 9:31=A0am, Dilbert <dilbert1...@gmail.com> wrote:
> I have a first perl-program ("first.pl") that calls a second perl-
> program ("second.pl") and captures STDOUT and STDERR.
> Is there a way inside "second.pl" to magically open a filehandle to
> the terminal, even though STDOUT and STDERR are both redirected ?
> I'm working under Ubuntu Linux.
>
> first.pl
> =3D=3D=3D=3D=3D=3D
> use strict;
> use warnings;
> system 'perl second.pl >file1.txt 2>file2.txt';
>
> second.pl
> =3D=3D=3D=3D=3D=3D
> use strict;
> use warnings;
> print STDOUT "This is STDOUT\n";
> print STDERR "This is STDERR\n";
> open my $fh, '>', ??magic-terminal?? or die "Error: Can't open magic
> terminal because $!";
> print {$fh} "This is magically going to terminal\n";
> close $fh;
A convoluted but more Unix-y portable option
is to dup STDOUT and undo the close-on-exec
flag before the system call and then pass
the dup'ed fd in:
first.pl
=3D=3D=3D=3D=3D=3D=3D=3D
use strict;
use warnings;
use Fcntl qw/F_SETFD/;
open( SAVEOUT, ">&STDOUT" ) or die $!;
fcntl( SAVEOUT, F_SETFD, 0 )
or die "Can't clear close-on-exec flag on SAVEOUT",": $!";
system "perl second.pl " . fileno(SAVEOUT) . " "
. fileno(SAVEOUT) . " >&2"
. " >file1.txt 2>file2.txt";
second.txt
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
use strict;
use warnings;
my $savefd =3D shift; # get STDOUT dup fd
print STDOUT "This is STDOUT\n";
print STDERR "This is STDERR\n";
open(SAVEOUT, "+<&=3D$savefd") or die "can't re-open SAVEOUT: $!";
print SAVEOUT "This is magically going to terminal\n";
--
Charles DeRykus
------------------------------
Date: Sat, 23 Jan 2010 01:13:15 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: writing to terminal even with STDOUT and STDERR redirected
Message-Id: <b2vp27-c6g2.ln1@osiris.mauzo.dyndns.org>
Quoth "C.DeRykus" <derykus@gmail.com>:
> On Jan 22, 9:31 am, Dilbert <dilbert1...@gmail.com> wrote:
> > I have a first perl-program ("first.pl") that calls a second perl-
> > program ("second.pl") and captures STDOUT and STDERR.
> > Is there a way inside "second.pl" to magically open a filehandle to
> > the terminal, even though STDOUT and STDERR are both redirected ?
> > I'm working under Ubuntu Linux.
> >
> > first.pl
> > ======
> > use strict;
> > use warnings;
> > system 'perl second.pl >file1.txt 2>file2.txt';
> >
> > second.pl
> > ======
> > use strict;
> > use warnings;
> > print STDOUT "This is STDOUT\n";
> > print STDERR "This is STDERR\n";
> > open my $fh, '>', ??magic-terminal?? or die "Error: Can't open magic
> > terminal because $!";
> > print {$fh} "This is magically going to terminal\n";
> > close $fh;
>
>
> A convoluted but more Unix-y portable option
Since /dev/tty is one of only four files mentioned by name in POSIX,
it's probably safe to rely on it being there :).
Ben
------------------------------
Date: Fri, 22 Jan 2010 19:17:21 -0800 (PST)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: writing to terminal even with STDOUT and STDERR redirected
Message-Id: <2bcafd0f-c14f-4db4-8101-f6fe3deaed2a@q4g2000yqm.googlegroups.com>
On Jan 22, 5:13=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth "C.DeRykus" <dery...@gmail.com>:
>
>
> ...
>
> Since /dev/tty is one of only four files mentioned by name in POSIX,
> it's probably safe to rely on it being there :).
>
<off-topic>
Suprisingly, I didn't find it when I was logged into FreeBSD.
which led me down the path of convolution. Of course, it's
a free FreeBSD shell account...maybe that's significant.
</off-topic>
--
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 2784
***************************************