[31754] in Perl-Users-Digest
Perl-Users Digest, Issue: 3017 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Jul 4 06:09:23 2010
Date: Sun, 4 Jul 2010 03:09:05 -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, 4 Jul 2010 Volume: 11 Number: 3017
Today's topics:
Re: FAQ 3.14 How can I write a GUI (X, Tk, Gtk, etc.) i <brian.d.foy@gmail.com>
Re: FAQ 5.29 How can I read in an entire file all at on <uri@StemSystems.com>
Re: How to up file with SFTP protocol using Windows Act <glex_no-spam@qwest-spam-no.invalid>
Re: How to up file with SFTP protocol using Windows Act <spamtrap@shermpendley.com>
Re: How to use $string=~s/(whatever)/${$i}/; with stric <ben@morrow.me.uk>
Re: How to use $string=~s/(whatever)/${$i}/; with stric <ben@morrow.me.uk>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 03 Jul 2010 15:05:02 -0500
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: FAQ 3.14 How can I write a GUI (X, Tk, Gtk, etc.) in Perl?
Message-Id: <030720101505028201%brian.d.foy@gmail.com>
In article <20100629123153.4192b891.groundXaero-gmail@no.poop.hehe>,
Xaero <groundXaero-gmail@no.poop.hehe> wrote:
> Could Prima be included too?
Can you send the patch the describes Prima?
------------------------------
Date: Sun, 04 Jul 2010 01:15:40 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: FAQ 5.29 How can I read in an entire file all at once?
Message-Id: <87mxu7j08z.fsf@quad.sysarch.com>
brian, here are some edits and comments for this faq:
>>>>> "PS" == PerlFAQ Server <brian@theperlreview.com> writes:
PS> 5.29: How can I read in an entire file all at once?
PS> Are you sure you want to read the entire file and store it in memory? If
PS> you mmap the file, you can virtually load the entire file into a string
PS> without actually storing it in memory:
Reading in an entire file at one time can be useful and more efficient
providing the file is small enough. With modern systems, even a 1MB file
can be considered small and almost all common text files and many others
are less than 1MB. Also some files need to be processed as whole
entities (e.g. image formats) and are best loaded into a scalar.
PS> use File::Map qw(map_file);
PS> map_file my $string, $filename;
PS> Once mapped, you can treat $string as you would any other
PS> string. Since you don't actually load the data, mmap-ing is
PS> very fast and does not increase your memory footprint.
i disagree with that last point. mmap always needs virtual ram allocated
for the entire file to be mapped. it only saves ram if you map part of
the file into a smaller virtual window. the win of mmap is that it won't
do the i/o until you touch a section. so if you want random access to
sections of a file, mmap is a big win. if you are going to just process
the whole file, there isn't any real win over File::Slurp
PS> If you really want to load the entire file, you can use the
PS> "File::Slurp" module to do it in one step.
If you decide to load the entire file, you can use the "File::Slurp"
module to do it in one simple and efficient step.
PS> use File::Slurp;
PS> my $all_of_it = read_file($filename); # entire file in scalar
PS> my @all_lines = read_file($filename); # one line per element
PS> The customary Perl approach for processing all the lines in a file is to
PS> do so one line at a time:
PS> open my $input, '<', $file or die "can't open $file: $!";
PS> while (<$input>) {
PS> chomp;
PS> # do something with $_
PS> }
PS> close $input or die "can't close $file: $!";
PS> This is tremendously more efficient than reading the entire file into
PS> memory as an array of lines and then processing it one element at a
PS> time, which is often--if not almost always--the wrong approach. Whenever
PS> you see someone do this:
again, i disagree. you can easily benchmark slurping an array of lines
and looping vs line by line reading. the win with slurping (with
File::Slurp) is bypassing perl's i/o layer. the looping overhead is the
same and the ram overhead isn't so much for most files as i have said
above. also some parsing or regex stuff is MUCH faster with whole files
in ram. a single s///g done over a whole file in a scalar is way faster
than doing it over each line in a loop. parsing and munging whole files
can be much easier too as you can do multiline matches and such.
here is a super fast way to read and parse a simple config file (key:
value lines):
use File::Slurp ;
my %config = read_file( $conf_file ) =~ /^(\w+):\s*(.+)$/mg ;
doing that line by line takes more code and is much slower as you need
to call the regex for each line.
PS> my @lines = <INPUT>;
PS> You can read the entire filehandle contents into a scalar.
PS> {
PS> local $/;
PS> open my $fh, '<', $file or die "can't open $file: $!";
PS> $var = <$fh>;
PS> }
PS> That temporarily undefs your record separator, and will automatically
PS> close the file at block exit. If the file is already open, just use
PS> this:
PS> $var = do { local $/; <$fh> };
you missed the coolest variant:
my $text = do { local( @ARGV, $/) = $file ; <> };
no open needed!
other than file::slurp not being in core (and it should be! :), there is
no reason to show the $/ = undef trick. it is always slower and more
obscure then calling read_file (which also does better error handling
and has more options).
PS> For ordinary files you can also use the read function.
PS> read( $fh, $var, -s $fh );
might as well use sysread as it is faster and has the same api. read is
almost never needed unless you are doing block reads on a file and
mixing in line reads (they share the perl stdio).
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Fri, 02 Jul 2010 10:46:26 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: How to up file with SFTP protocol using Windows Activestate perl?
Message-Id: <4c2e09d2$0$87074$815e3792@news.qwest.net>
info@potop.hr wrote:
> How to use SFTP protocol with Activestate perl?
>
> Please help me
You can help yourself and greatly cut down the time it takes
for you to possibly find a solution, by first trying to find
the information on your own.
Put "perl sftp" into your favorite search engine and you should find
many helpful pages.
Oh look.. the very first result from the search is for:
http://search.cpan.org/~dbrobins/Net-SFTP-0.10/lib/Net/SFTP.pm
which looks pretty useful.
------------------------------
Date: Fri, 02 Jul 2010 12:16:45 -0400
From: Sherm Pendley <spamtrap@shermpendley.com>
Subject: Re: How to up file with SFTP protocol using Windows Activestate perl?
Message-Id: <m2aaq9505u.fsf@shermpendley.com>
"J. Gleixner" <glex_no-spam@qwest-spam-no.invalid> writes:
> info@potop.hr wrote:
>> How to use SFTP protocol with Activestate perl?
>>
>> Please help me
>
> You can help yourself and greatly cut down the time it takes
> for you to possibly find a solution, by first trying to find
> the information on your own.
>
> Put "perl sftp" into your favorite search engine and you should find
> many helpful pages.
>
> Oh look.. the very first result from the search is for:
>
> http://search.cpan.org/~dbrobins/Net-SFTP-0.10/lib/Net/SFTP.pm
>
> which looks pretty useful.
On the other paw, if you're trying that module and having problems
with it, you should say so and describe the problems.
sherm--
--
Sherm Pendley <www.shermpendley.com>
<www.camelbones.org>
Cocoa Developer
------------------------------
Date: Fri, 2 Jul 2010 23:11:03 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: How to use $string=~s/(whatever)/${$i}/; with strict ref?
Message-Id: <no42g7-rfn1.ln1@osiris.mauzo.dyndns.org>
Quoth yjnnhauhht <yjnnhauhht@mailinator.com>:
> On Jul 2, 2:00 am, s...@netherlands.com wrote:
> > On Thu, 01 Jul 2010 16:47:51 -0700, s...@netherlands.com wrote:
> > >Or you can combine it all in a double eval regex as something like this:
> > >(expanded with print for detail)
> >
> > >$i = 2;
> > >$string =~ s/(test)(string)/print '${'.$i."}\n"; '${'.$i.'}'/ee;
> > ># or, shortened
> > ># $string =~ s/(test)(string)/'$'.$i/ee;
> >
> > Or, another popular notation:
> >
> > $string =~ s/(test)(string)/"\$$i"/ee;
>
> Thanks!
> This answer my needs.
BE CAREFUL. /ee does a string eval, so if (as I suspect) your $i is
actually coming from user input, you must validate it carefully.
Consider $i = '{system("rm -rf /")}', for instance.
Ben
------------------------------
Date: Fri, 2 Jul 2010 23:31:15 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: How to use $string=~s/(whatever)/${$i}/; with strict ref?
Message-Id: <ju52g7-b2o1.ln1@osiris.mauzo.dyndns.org>
Quoth yjnnhauhht <yjnnhauhht@mailinator.com>:
> On Jul 2, 3:39 pm, yjnnhauhht <yjnnhau...@mailinator.com> wrote:
> > On Jul 2, 12:23 am, Big and Blue <N...@dsl.pipex.com> wrote:
> > > On 07/01/10 17:29, Tad McClellan wrote:
> >
> > > > $string=~s/(test)/test/;
> >
> > > > I smell an XY-problem.
> >
> > > > What is it that you are actually trying to accomplish?
> >
> > > Is it, by chance:
> >
> > > my $i=1;
> > > my $string="teststring";
> > > $string=~s/(?<test>test)/$+{test}/;
> > > print "$string\n";
> >
> > This doesn't solve my issue, cause I need to do the thing in a for
> > loop where the $i is incremented.
> > Thanks for your time though.
>
> Well what I really want to do is some kind of "highlighting grep -E"
> which highlights the part of the match pattern that is inside ().
> For example if I do mygrep "...\w*(test)..." file, I get the lines
> matching the pattern and the word test is highlighted.
> To do this I have to add a highligth/nohighligth control string before
> and after each () group.
> I though I will be able to do this in one regex, but failed and only
> found a solution where I need to iterate on the () groups and do the
> s/.../$i/ I ask you about.
Well, now that you've *said* so...
my $str = "1a:23b:4c";
while ($str =~ /(\d+)(\w+)/g) {
my $added = 0;
for (1..$#+) {
substr $str, $-[$_] + $added, 0, "[";
$added++;
substr $str, $+[$_] + $added, 0, "]";
$added++;
}
pos($str) = $+[-1] + $added;
}
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 3017
***************************************