[30422] in Perl-Users-Digest
Perl-Users Digest, Issue: 1665 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jun 21 14:09:47 2008
Date: Sat, 21 Jun 2008 11: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 Sat, 21 Jun 2008 Volume: 11 Number: 1665
Today's topics:
code review request <stefan.petrea@gmail.com>
Re: FAQ 8.2 How come exec() doesn't return? <rvtol+news@isolution.nl>
Re: Filtering a string <bill@ts1000.us>
Re: Filtering a string <ben@morrow.me.uk>
Re: Learning Perl <allergic-to-spam@no-spam-allowed.org>
Re: Learning Perl <allergic-to-spam@no-spam-allowed.org>
Re: Learning Perl <gc[remove]e@gmail.com>
new CPAN modules on Sat Jun 21 2008 (Randal Schwartz)
Re: Print Spanish characters in Perl? <benkasminbullock@gmail.com>
Strange behaviour with unlink <bill@ts1000.us>
Re: Strange behaviour with unlink <sisyphus359@gmail.com>
Re: Strange behaviour with unlink <ben@morrow.me.uk>
Re: Strange behaviour with unlink <bill@ts1000.us>
Re: Substituting in a group <baxter.brad@gmail.com>
Re: Substituting in a group <m@rtij.nl.invlalid>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 21 Jun 2008 11:04:21 -0700 (PDT)
From: SPX2 <stefan.petrea@gmail.com>
Subject: code review request
Message-Id: <72c9c2b0-4e50-4005-b03a-43453d0fbce5@d77g2000hsb.googlegroups.com>
Hi,
I've written a search toolbar tha has the features described here
http://perlhobby.googlecode.com/svn/trunk/firefox_toolbar_version_0.5/README.TXT
It also uses a server-side script to pull out data from a database
and
send them back to the extension for it to display them.
Unfortunately I have some small problems because with this server-side
script
http://perlhobby.googlecode.com/svn/trunk/firefox_toolbar_version_0.5/get_data_example.pl
all things work well
but when I use another server-side script,this one
http://perlhobby.googlecode.com/svn/trunk/firefox_toolbar_version_0.5/get_data.pl
it stops working.
The code from the firefox toolbar is based on the code here
http://www.borngeek.com/firefox/toolbar-tutorial/
I mean I've started reading from there and I've went on from there
and
developed
my own extension.
The actual extension and the code for it can be found here
http://perlhobby.googlecode.com/svn/trunk/firefox_toolbar_version_0.5/tuttoolbar_unziped/
Any suggestions regarding either server-side code or extension code
are
very much appreciated :)
Thank you
------------------------------
Date: Sat, 21 Jun 2008 13:49:04 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: FAQ 8.2 How come exec() doesn't return?
Message-Id: <g3j0vr.vc.1@news.isolution.nl>
Bill H wrote:
> The only solution I can think of (and it probably is the best)
> is to make the maintenance program a cron job since it has no
> interaction with the user but I think it would be a cleaner
> solution to only have the maintenance program executed when
> something needs to be maintained.
Touch some 0-byte flag file if something is changed, and test in the
cronjob if the access data/time of that flag file was changed since the
previous run.
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
Date: Fri, 20 Jun 2008 18:24:38 -0700 (PDT)
From: Bill H <bill@ts1000.us>
Subject: Re: Filtering a string
Message-Id: <5ea9b9d8-8c1b-443e-9015-2460d1efd2cd@l64g2000hse.googlegroups.com>
On Jun 20, 1:48=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth Bill H <b...@ts1000.us>:
>
> > Can someone point me to some docs on how I would do this without
> > iterating over the whole string (pattern matching?):
>
> > $original =3D "a malformed%string/containi\"ng characters I don'~t
> > want! ...";
>
> > $filter =3D "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";
>
> > $new =3D &fix($original);
>
> > $new would now equal:
>
> > amalformedstringcontainingcharactersidontwant
>
> $orginal =3D~ tr/a-zA-Z0-9_-//cd;
>
> See tr/// under 'Regexp Quote-Like Operators' in perlop.
>
> Note that '-' must come first or last, as otherwise it will be
> interpreted as part of an X-Y range.
>
> Ben
>
> --
> For far more marvellous is the truth than any artists of the past imagine=
d!
> Why do the poets of the present not speak of it? What men are poets who c=
an
> speak of Jupiter if he were like a man, but if he is an immense spinning
> sphere of methane and ammonia must be silent? [Feynmann] =A0 =A0 b...@mor=
row.me.uk
Ben this one seems to work nice for me, only issue is I have to lc()
the results (not really a problem). Is there a way to have that done
in this ? Also thanks for pointing out the part in perlop that deals
with it.
Bill H
------------------------------
Date: Sat, 21 Jun 2008 03:51:04 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Filtering a string
Message-Id: <ot6ui5-v821.ln1@osiris.mauzo.dyndns.org>
Quoth Bill H <bill@ts1000.us>:
> On Jun 20, 1:48 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> > Quoth Bill H <b...@ts1000.us>:
> >
> > > Can someone point me to some docs on how I would do this without
> > > iterating over the whole string (pattern matching?):
> >
> > > $original = "a malformed%string/containi\"ng characters I don'~t
> > > want! ...";
> >
> > > $filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";
> >
> > > $new = &fix($original);
> >
> > > $new would now equal:
> >
> > > amalformedstringcontainingcharactersidontwant
> >
> > $orginal =~ tr/a-zA-Z0-9_-//cd;
> >
> > See tr/// under 'Regexp Quote-Like Operators' in perlop.
> >
> > Note that '-' must come first or last, as otherwise it will be
> > interpreted as part of an X-Y range.
>
> Ben this one seems to work nice for me, only issue is I have to lc()
> the results (not really a problem). Is there a way to have that done
> in this ?
Well, tr/-_a-zA-Z0-9\000-\177/-_a-za-z0-9/d will work, but personally
I'd probably just use lc, and probably beforehand, to avoid two
alphabetic ranges. Something like
(my $new = lc $original) =~ tr/a-z0-9_-//cd;
seems clearer than relying on the details of tr///, which is an operator
a lot of Perl programmers don't know very well.
Ben
--
Outside of a dog, a book is a man's best friend.
Inside of a dog, it's too dark to read.
ben@morrow.me.uk Groucho Marx
------------------------------
Date: Sat, 21 Jun 2008 10:52:55 +0200 (CEST)
From: Jim Cochrane <allergic-to-spam@no-spam-allowed.org>
Subject: Re: Learning Perl
Message-Id: <slrng5pga2.fgq.allergic-to-spam@no-spam-allowed.org>
On 2008-06-20, Ted Zlatanov <tzz@lifelogs.com> wrote:
> On Fri, 20 Jun 2008 15:09:59 GMT Uri Guttman <uri@stemsystems.com> wrote:
>
> UG> i will be amazed and dazzled if he comes up with a use that all of p5p
> UG> and thousands of experienced perl hackers never thought of
> UG> before. considering that defined and undef are scalar concepts and
> UG> always have been, any use for them on aggregated is beyond our mortal
> UG> comprehension.
>
> This makes some sense to me from a Lisp perspective, where nil is an
> empty list and a null value at the same time. Because Perl lists and
> hashes are so different from Lisp structures, though, it just doesn't
> translate well. In Lisp it can lead to some wonderfully concise code:
>
> (cond
> ((null v) ...) ; it's empty
> ((stringp v) ...) ; it's a string
> ((listp v) ...) ; it's a list
> ((t) ...)) ; anything else
>
> Implementing the same in Perl 5 is not as nice.
>
> Ted
Do you mean that since the empty set is a member of all sets, nil (the
empty set) is an empty list, an empty string, ...?
Makes sense.
I suppose Lisp is more mathematically consistent than Perl.
--
------------------------------
Date: Sat, 21 Jun 2008 10:58:27 +0200 (CEST)
From: Jim Cochrane <allergic-to-spam@no-spam-allowed.org>
Subject: Re: Learning Perl
Message-Id: <slrng5pgkc.fgq.allergic-to-spam@no-spam-allowed.org>
On 2008-06-20, Gordon Corbin Etly <g> wrote:
> Uri Guttman wrote:
>>>>>>> "JC" == Jim Cochrane <allergic-to-spam@no-spam-allowed.org>
>>>>>>> writes:
>> > On 2008-06-19, Jim Cochrane <allergic-to-spam@no-spam-allowed.org>
>> > wrote:
>> > > ...
>> > > >
>> > > > I wasn't really disputing the documentation, though; I just feel
>> > > > it can be useful to test weather an array or hash has been given
>> > > > a value since it's inception, or if it has been run through the
>> > > > undef() function. I can thing of a few scenarios where it maybe
>> > > > be useful to able to check for such a state separately from
>> > > > being just empty, much like one might test for undef vs. 0 or
>> > > > "".
>
>> > > What do you mean by "given a value"? Do you mean "has memory for
>> > > the array [or hash] been allocated?"?
>> > >
>> > > If so, when you that knowledge be useful in a program?
>
>> > Ugh - correction:
>
>> > If so, when do you think that knowledge would be useful in a
>> > program?
>
>> i will be amazed and dazzled if he comes up with a use that all of p5p
>> and thousands of experienced perl hackers never thought of
>> before.
>
> In all honest I cannot think of a real word example, and I don't believe
> I claimed one before. I just thought there could be situations (like
> some complex class) where it needs to be known if any array or hash has
> been initialized, but after taking some time to really think about it,
> it indeed seems like a useless function, as all you really need to know
> is if it contains anything.
Yes, I think whether or not memory for the array (or hash) has been
allocated is just an implementation/optimization detail - not of
relevance to the program one is writing (unless, perhaps, one is writing
a perl compiler in perl [not sure if that is a good idea]). Since the
programmer does not have to explicilty allocate memory, he doesn't
really need to think about it.
[If one wants to worry about memory allocation, one can suffer with a
lower-level language, like C or C++ :-).]
--
------------------------------
Date: Sat, 21 Jun 2008 09:34:19 -0700
From: "Gordon Corbin Etly" <gc[remove]e@gmail.com>
Subject: Re: Learning Perl
Message-Id: <6c4osdF3f8m89U1@mid.individual.net>
Jim Cochrane wrote:
> On 2008-06-20, Gordon Corbin Etly <g> wrote:
> > Uri Guttman wrote:
> > > "Jim Cochrane wrote:
> > > > On 2008-06-19, Jim Cochrane wrote:
> > > > > Gordon Corbin Etly wrote:
> > > > > ...
> > > > > > I wasn't really disputing the documentation, though; I just
> > > > > > feel it can be useful to test weather an array or hash has
> > > > > > been given a value since it's inception, or if it has been
> > > > > > run through the undef() function. I can thing of a few
> > > > > > scenarios where it maybe be useful to able to check for
> > > > > > such a state separately from being just empty, much like
> > > > > > one might test for undef vs. 0 or "".
> > > > > What do you mean by "given a value"? Do you mean "has memory
> > > > > for the array [or hash] been allocated?"?
> > > > >
> > > > > If so, when you that knowledge be useful in a program?
> > > > Ugh - correction:
> > > >
> > > > If so, when do you think that knowledge would be useful in a
> > > > program?
> > > i will be amazed and dazzled if he comes up with a use that all
> > > of p5p and thousands of experienced perl hackers never thought
> > > of before.
> > In all honesty I cannot think of a real word example, and I
> > don't believe I claimed one before. I just thought there could
> > be situations (like some complex class) where it needs to be known
> > if any array or hash has been initialized, but after taking some
> > time to really think about it, it indeed seems like a useless
> > function, as all you really need to know is if it contains anything.
> Yes, I think whether or not memory for the array (or hash) has been
> allocated is just an implementation/optimization detail - not of
> relevance to the program one is writing (unless, perhaps, one is
> writing a perl compiler in perl [not sure if that is a good idea]).
> Since the programmer does not have to explicilty allocate memory,
> he doesn't really need to think about it.
True, I agree. Though if experience has taught me anything, there may
always been unforeseen uses, although it seems rather unlikely in this
case.
> [If one wants to worry about memory allocation, one can suffer with a
> lower-level language, like C or C++ :-).]
Been there, done that :)
--
G. C. Etly
------------------------------
Date: Sat, 21 Jun 2008 04:42:20 GMT
From: merlyn@stonehenge.com (Randal Schwartz)
Subject: new CPAN modules on Sat Jun 21 2008
Message-Id: <K2sqEK.21G3@zorch.sf-bay.org>
The following modules have recently been added to or updated in the
Comprehensive Perl Archive Network (CPAN). You can install them using the
instructions in the 'perlmodinstall' page included with your Perl
distribution.
Apache-Session-1.86_01
http://search.cpan.org/~chorny/Apache-Session-1.86_01/
A persistence framework for session data
----
Bio-GenBankParser-0.04
http://search.cpan.org/~kclark/Bio-GenBankParser-0.04/
Parse::RecDescent parser for a GenBank record
----
CPAN-Reporter-1.15_56
http://search.cpan.org/~dagolden/CPAN-Reporter-1.15_56/
Adds CPAN Testers reporting to CPAN.pm
----
Catalyst-Authentication-Credential-OpenID-0.03
http://search.cpan.org/~ashley/Catalyst-Authentication-Credential-OpenID-0.03/
OpenID credential for Catalyst::Plugin::Authentication framework.
----
Catalyst-Authentication-Credential-OpenID-0.04
http://search.cpan.org/~ashley/Catalyst-Authentication-Credential-OpenID-0.04/
OpenID credential for Catalyst::Plugin::Authentication framework.
----
Config-BuildHelper-0.02
http://search.cpan.org/~triddle/Config-BuildHelper-0.02/
A tool to help build config files
----
DBD-Ingres-0.5206
http://search.cpan.org/~sreagle/DBD-Ingres-0.5206/
DBI driver for Ingres database systems
----
Devel-Hints-Lexical-0.06
http://search.cpan.org/~chocolate/Devel-Hints-Lexical-0.06/
make %^H lexically-scoped
----
Devel-Hints-Lexical-0.07
http://search.cpan.org/~chocolate/Devel-Hints-Lexical-0.07/
make %^H lexically-scoped
----
Devel-Hints-Lexical-0.08
http://search.cpan.org/~chocolate/Devel-Hints-Lexical-0.08/
make %^H lexically-scoped
----
Enbugger-2.004
http://search.cpan.org/~jjore/Enbugger-2.004/
Enables the debugger at runtime.
----
Enbugger-Restarts-0.01_02
http://search.cpan.org/~whitepage/Enbugger-Restarts-0.01_02/
Reach around in your stack and restart execution from arbitrary places
----
Foorum-0.1.8
http://search.cpan.org/~fayland/Foorum-0.1.8/
Foorum is a forum script built in Catalyst.
----
Geo-Hashing-0.01
http://search.cpan.org/~zigdon/Geo-Hashing-0.01/
Perl library to calculate Geohashing points
----
Geo-Hashing-0.02
http://search.cpan.org/~zigdon/Geo-Hashing-0.02/
Perl library to calculate Geohashing points
----
HTML-FormFu-0.03001
http://search.cpan.org/~cfranks/HTML-FormFu-0.03001/
HTML Form Creation, Rendering and Validation Framework
----
HTML-TurboForm-0.16
http://search.cpan.org/~camelcase/HTML-TurboForm-0.16/
----
JavaScript-Lite-0.01
http://search.cpan.org/~crakrjack/JavaScript-Lite-0.01/
Bare-bones interface to SpiderMonkey ECMAscript API
----
Makefile-GraphViz-0.18
http://search.cpan.org/~agent/Makefile-GraphViz-0.18/
Draw building flowcharts from Makefiles using GraphViz
----
MooseX-Signature-0.00_01
http://search.cpan.org/~berle/MooseX-Signature-0.00_01/
A framework for signatures in Moose
----
Music-Audioscrobbler-Submit-0.05
http://search.cpan.org/~ealleniii/Music-Audioscrobbler-Submit-0.05/
Module providing routines to submit songs to last.fm using 1.2 protocol.
----
MySQL-Insert-0.01
http://search.cpan.org/~despair/MySQL-Insert-0.01/
extended inserts for MySQL via DBI
----
Net-WhitePages-1.03
http://search.cpan.org/~darren/Net-WhitePages-1.03/
A Perl interface to the WhitePages.com API
----
Net-WhitePages-1.04
http://search.cpan.org/~darren/Net-WhitePages-1.04/
A Perl interface to the WhitePages.com API
----
Object-eBay-0.2.0
http://search.cpan.org/~mndrix/Object-eBay-0.2.0/
Object-oriented interface to the eBay API
----
Perl6-Junction-1.40000
http://search.cpan.org/~cfranks/Perl6-Junction-1.40000/
Perl6 style Junction operators in Perl5.
----
PerlIO-Util-0.49_02
http://search.cpan.org/~gfuji/PerlIO-Util-0.49_02/
A selection of general PerlIO utilities
----
Ruby-0.02
http://search.cpan.org/~gfuji/Ruby-0.02/
Perl interface to Ruby interpreter
----
Sub-ArgShortcut-1.02
http://search.cpan.org/~aristotle/Sub-ArgShortcut-1.02/
simplify writing functions that use default arguments
----
Template-Plugin-TruncateByWord-0.1
http://search.cpan.org/~kawabata/Template-Plugin-TruncateByWord-0.1/
A Template Toolkit filter to truncate not the number of bytes but characters
----
Test-MonitorSites-0.15
http://search.cpan.org/~hesco/Test-MonitorSites-0.15/
Monitor availability and function of a list of websites
----
UML-Class-Simple-0.10
http://search.cpan.org/~agent/UML-Class-Simple-0.10/
Render simple UML class diagrams, by loading the code
----
WordPress-API-1.07
http://search.cpan.org/~leocharre/WordPress-API-1.07/
----
WordPress-XMLRPC-1.12
http://search.cpan.org/~leocharre/WordPress-XMLRPC-1.12/
api to wordpress rpc
----
XLSperl-0.7
http://search.cpan.org/~jonallen/XLSperl-0.7/
use Perl "one-liners" with Microsoft Excel files
----
stockmonkey-2.7
http://search.cpan.org/~jettero/stockmonkey-2.7/
If you're an author of one of these modules, please submit a detailed
announcement to comp.lang.perl.announce, and we'll pass it along.
This message was generated by a Perl program described in my Linux
Magazine column, which can be found on-line (along with more than
200 other freely available past column articles) at
http://www.stonehenge.com/merlyn/LinuxMag/col82.html
print "Just another Perl hacker," # the original
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
------------------------------
Date: Sat, 21 Jun 2008 00:12:53 +0000 (UTC)
From: Ben Bullock <benkasminbullock@gmail.com>
Subject: Re: Print Spanish characters in Perl?
Message-Id: <g3hh25$e9e$1@ml.accsnet.ne.jp>
On Fri, 20 Jun 2008 04:37:34 +0000, Jürgen Exner wrote:
> DanB <dbxxxxxxx@yahoo.com> wrote:
>>I am trying to build a set of Spanish flash cards using TK, and I
>>need to be able to display the accented characters. I know that I
>>need to specify them in some unicode besides utf-8,
> Actually, you don't. Just put them into your code in your favourite
> editor and treat them like any ASCII character.
I suggest *not* doing this, but
use utf8;
and ensure that your file is saved in the UTF-8 format.
> A problems arise only if your editor saves the file in a different
> encoding then your display device expects.
You are just playing Russian Roulette with the encodings. Playing
Russian Roulette might be a safe hobby. After all there was no bullet
there last time you pulled the trigger, so this time you'll be safe
too. In the same way, you can cross your fingers and hope the
encodings match.
> Typical examples are e.g.
> saving as UTF-8, then including the text in an HTML page but
> forgetting to specify UTF-8 as charset.
To avoid this kind of problem, make sure that all the characters are
encoded into Perl's internal encoding with
use utf8;
and always specify the output encoding you want:
binmode STDOUT,":encoding(utf8)";
binmode STDOUT,":encoding(cp850)";
or
open my $file, ">:encoding(iso-latin-1)", "filename";
If you need to pass a string to some kind of module which doesn't
understand UTF-8 (there are lots of these), then you can decode it into
whatever the module wants with
use Encode 'encode';
encode ("cp850", $string);
Similarly, there is
decode ("cp850", $string);
to go the other way.
I recommend you to keep everything in the Perl code which is under your
control as UTF-8, and don't use anything else. Always
use utf8;
at the top of the script. If your editor accidentally saves the file
in a non-UTF-8 format, then when you try to compile your Perl script
you'll get lots of messages like
Malformed UTF-8 character (unexpected non-continuation byte 0xd1,
immediately after start byte 0xf1) at ./encodings.pl line 8.
Malformed UTF-8 character (unexpected non-continuation byte 0xdc,
immediately after start byte 0xd1) at ./encodings.pl line 8.
Malformed UTF-8 character (unexpected non-continuation byte 0xfc,
immediately after start byte 0xdc) at ./encodings.pl line 8.
Malformed UTF-8 character (5 bytes, need 6, after start byte 0xfc) at ./
encodings.pl line 8.
So
use utf8;
means that Perl can protect you against accidents with text editors.
Here is a small sample script to test it out with:
#!/usr/local/bin/perl
use utf8;
binmode STDOUT,":encoding(cp850)";
my $spanish = "ñÑÜü¿¡«»\n";
print $spanish;
You don't even need to "use warnings;" to get the error messages.
> In this case the browser
> defaults to ISO-Latin-1 and the non-ASCII characters will be messed
> up, of course. Or saving the file as Windows-1252 (or ISO-Latin-1)
> and then viewing the output in a DOS Window which for western
> languages uses OEM CP 850.
The attitude of a lot of people towards encodings seems to be just ignore
the problem and hope it will go away. That's OK as long as your luck
holds. If you are lucky, the encoding you used for the script may happen
to be the same one as the web browser or whatever expects. If you are
unlucky then they won't be the same. Then you'll get strange bugs, and
you won't know why.
So I suggest that unless you only use ASCII, you should get encodings
under close control. Specify the encoding of your Perl script with
use utf8;
and then specify exactly how you want to encode input and output at each
point. Then there won't be so many unexpected things waiting for you next
time something goes wrong with your code.
------------------------------
Date: Fri, 20 Jun 2008 18:44:23 -0700 (PDT)
From: Bill H <bill@ts1000.us>
Subject: Strange behaviour with unlink
Message-Id: <7a4e3b7c-dfc6-4b0c-bf92-3b1250608aad@26g2000hsk.googlegroups.com>
I dont have the original code with me (its on a pc I have shut down
for the night) but a few days ago I came upon a strange behaviour with
unlink.
The routine basically did the following (I will post real code
tomorrow):
$somevar = &getsomevalue();
sub getsomevalue
{
my $results = "";
my @scratch = ();
open(FILE,"somefile");
@scratch = <FILE>;
close(FILE);
$results = $scratch[0];
# unlink("somefile");
return($results);
}
The above works fine as expected and gets me the value I want in
$somevar. But if I uncomment the unlink, $somevar is always blank! And
yes, the file does exist before I open it (real code checks that).
Can the the value being returned somehow still be linked to that file?
I thought maybe cause I was in the subroutine and put the unlink after
the "$somevar = &getsomevalue()" outside of the subroutine and it
still made $somevar blank???
Bill H
------------------------------
Date: Fri, 20 Jun 2008 20:01:15 -0700 (PDT)
From: sisyphus <sisyphus359@gmail.com>
Subject: Re: Strange behaviour with unlink
Message-Id: <5bc90170-f925-40a0-9352-a0d77b00037d@i36g2000prf.googlegroups.com>
On Jun 21, 11:44=A0am, Bill H <b...@ts1000.us> wrote:
=2E
=2E
> Can the the value being returned somehow still be linked to that file?
No ... at least not with the code you posted.
Wrt the code you supplied, if the file is either empty or non-
existent, $somevar would be blank (and undef).
Otherwise $somevar would have to contain something.
Cheers,
Rob
------------------------------
Date: Sat, 21 Jun 2008 04:00:37 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Strange behaviour with unlink
Message-Id: <lf7ui5-v821.ln1@osiris.mauzo.dyndns.org>
Quoth Bill H <bill@ts1000.us>:
> I dont have the original code with me (its on a pc I have shut down
> for the night) but a few days ago I came upon a strange behaviour with
> unlink.
>
> The routine basically did the following (I will post real code
> tomorrow):
>
> $somevar = &getsomevalue();
Don't call subs with & unless you know what it does.
> sub getsomevalue
> {
> my $results = "";
> my @scratch = ();
You don't need to initialize variables like this. Perl isn't C:
variables get perfectly well-defined[0] initial values on their own.
[0] 'defined' in the C sense, not the Perl sense, of course... :)
> open(FILE,"somefile");
The usual... (you may know all this already, but it's good to get into
the habit. Even in example code.)
Use lexical filehandles.
Check the return value of open.
Use three-arg open: I know it can't make a difference when the
filename is a string literal like this, but in a minute you'll
change it to a variable, and then you'll end up with that variable
containing '|rm -rf /'... :)
> @scratch = <FILE>;
Don't declare variables before you need to. In this case, you can
declare @scratch right here:
my @scratch = <$FILE>;
> close(FILE);
> $results = $scratch[0];
Err... you're reading the whole file, just to trow most of it away and
only keep the first line? What's wrong with
my $results = <$FILE>;
?
> # unlink("somefile");
> return($results);
> }
>
> The above works fine as expected and gets me the value I want in
> $somevar. But if I uncomment the unlink, $somevar is always blank! And
> yes, the file does exist before I open it (real code checks that).
I don't believe you :). Post some real code that exhibits this
behaviour, and include details about your OS, the file you were
reading, &c.
> Can the the value being returned somehow still be linked to that file?
Nope. (Well, not unless you'd used Tie::File, or something similar...)
> I thought maybe cause I was in the subroutine and put the unlink after
> the "$somevar = &getsomevalue()" outside of the subroutine and it
> still made $somevar blank???
I suspect your code didn't say what you thought it did, and you weren't
returning from the sub where you thought. Check it again, and post some
real code if you still can't figure it out.
Ben
--
For far more marvellous is the truth than any artists of the past imagined!
Why do the poets of the present not speak of it? What men are poets who can
speak of Jupiter if he were like a man, but if he is an immense spinning
sphere of methane and ammonia must be silent? [Feynmann] ben@morrow.me.uk
------------------------------
Date: Sat, 21 Jun 2008 04:08:58 -0700 (PDT)
From: Bill H <bill@ts1000.us>
Subject: Re: Strange behaviour with unlink
Message-Id: <c1626101-85aa-4ffc-a201-9ecaf0071736@z72g2000hsb.googlegroups.com>
On Jun 20, 11:00=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth Bill H <b...@ts1000.us>:
>
> > I dont have the original code with me (its on a pc I have shut down
> > for the night) but a few days ago I came upon a strange behaviour with
> > unlink.
>
> > The routine basically did the following (I will post real code
> > tomorrow):
>
> > $somevar =3D &getsomevalue();
>
> Don't call subs with & unless you know what it does.
>
> > sub getsomevalue
> > {
> > my $results =3D "";
> > my @scratch =3D ();
>
> You don't need to initialize variables like this. Perl isn't C:
> variables get perfectly well-defined[0] initial values on their own.
>
> [0] 'defined' in the C sense, not the Perl sense, of course... :)
>
> > open(FILE,"somefile");
>
> The usual... (you may know all this already, but it's good to get into
> the habit. Even in example code.)
>
> =A0 =A0 Use lexical filehandles.
>
> =A0 =A0 Check the return value of open.
>
> =A0 =A0 Use three-arg open: I know it can't make a difference when the
> =A0 =A0 filename is a string literal like this, but in a minute you'll
> =A0 =A0 change it to a variable, and then you'll end up with that variabl=
e
> =A0 =A0 containing '|rm -rf /'... :)
>
> > @scratch =3D <FILE>;
>
> Don't declare variables before you need to. In this case, you can
> declare @scratch right here:
>
> =A0 =A0 my @scratch =3D <$FILE>;
>
> > close(FILE);
> > $results =3D $scratch[0];
>
> Err... you're reading the whole file, just to trow most of it away and
> only keep the first line? What's wrong with
>
> =A0 =A0 my $results =3D <$FILE>;
>
> ?
>
> > # unlink("somefile");
> > return($results);
> > }
>
> > The above works fine as expected and gets me the value I want in
> > $somevar. But if I uncomment the unlink, $somevar is always blank! And
> > yes, the file does exist before I open it (real code checks that).
>
> I don't believe you :). Post some real code that exhibits this
> behaviour, and include details about your OS, the file you were
> reading, &c.
>
> > Can the the value being returned somehow still be linked to that file?
>
> Nope. (Well, not unless you'd used Tie::File, or something similar...)
>
> > I thought maybe cause I was in the subroutine and put the unlink after
> > the "$somevar =3D &getsomevalue()" outside of the subroutine and it
> > still made $somevar blank???
>
> I suspect your code didn't say what you thought it did, and you weren't
> returning from the sub where you thought. Check it again, and post some
> real code if you still can't figure it out.
>
> Ben
>
> --
> For far more marvellous is the truth than any artists of the past imagine=
d!
> Why do the poets of the present not speak of it? What men are poets who c=
an
> speak of Jupiter if he were like a man, but if he is an immense spinning
> sphere of methane and ammonia must be silent? [Feynmann] =A0 =A0 b...@mor=
row.me.uk
Ben
I have to assume you are right. I just took the code, cut out the
relevant parts and made a small program to duplicate it and it works
as expected, unlink doesn't change the value. I'm not sure why it was
doing it before, but I was able to make it do it on command back then
(a few days ago) by just removing the # in front of the unlink (and
unfortunately I deleted that line and said screw it, I'll just remove
these old maintenance files in a cron job).
Bill H
------------------------------
Date: Sat, 21 Jun 2008 08:32:55 -0700 (PDT)
From: Brad Baxter <baxter.brad@gmail.com>
Subject: Re: Substituting in a group
Message-Id: <b3708645-a24e-456b-81b2-bab1c86c541f@k13g2000hse.googlegroups.com>
On Jun 20, 11:51 am, aquadoll <sam...@gmail.com> wrote:
> Also, is there a not-so-cryptic one-liner for this one?
I have to wonder if you're saying that you're not familiar
with tools for parsing CSV-like data.
perl -MText::ParseWords=parse_line -ne'@a=parse_line(",",0,$_);
$a[2]=~tr/,/#/;print join",",@a' infile
--
Brad
------------------------------
Date: Sat, 21 Jun 2008 19:27:45 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: Substituting in a group
Message-Id: <pan.2008.06.21.17.27.44@rtij.nl.invlalid>
On Fri, 20 Jun 2008 19:20:15 +0200, Dave B wrote:
> aquadoll wrote:
>
>> ABC XXX,2231,"Math, Physics",0.45,2
>> PQR ERR,2217,"Physics, Chemistry, Math",0.21,5 ABC
>> PQR,1213,Physics,0.5,1
>>
>> I want to detect when there are groups of subjects in the 3rd column,
>> remove the quotes in those cases and replace the comma by # inside the
>> groups. So, the above lines would be transformed to:
>>
>> ABC XXX,2231,Math# Physics,0.45,2
>> PQR ERR,2217,Physics# Chemistry# Math,0.21,5 ABC PQR,1213,Physics,0.5,1
>>[snip]
>> Also, is there a not-so-cryptic one-liner for this one?
>
> I'm a beginner in perl, so please forgive any naivety. This oneliner
> seems to work:
>
> $ perl -pe 'if (s/"([^"]*)"/$1/) {$m=$n=$1; $n=~s/,/#/g; s/$m/$n/;}'
> file XXX,2231,Math# Physics,0.45,2
> PQR ERR,2217,Physics# Chemistry# Math,0.21,5 ABC PQR,1213,Physics,0.5,1
>
> This assumes that the text between double quotes (the part that is
> matched in the first place) does not appear elsewhere before the double
> quotes, and assumes that it's the only text in double quotes in the
> line.
Very good! Now change it slightly (untested):
perl -pe 'if (/"([^"]*)"/) {$n=$1; s/"([^"]*)"/$n/ if ($n=~s/,/#/g); }'
The first substitution is guaranteed to match the same string as the
match, so the assumption is not relevant anymore.
M4
------------------------------
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:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#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 1665
***************************************