[31515] in Perl-Users-Digest
Perl-Users Digest, Issue: 2774 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jan 18 14:09:47 2010
Date: Mon, 18 Jan 2010 11:09:12 -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, 18 Jan 2010 Volume: 11 Number: 2774
Today's topics:
embedded Perl process in Apache using mod_perl <cartercc@gmail.com>
Re: FAQ 7.28 How do I clear a package? <kst-u@mib.org>
Re: FAQ 9.16 How do I decode a CGI form? <hhr-m@web.de>
new dbi connection for each request under mod_perl <michaelgang@gmail.com>
Re: new dbi connection for each request under mod_perl <paduille.4061.mumia.w+nospam@earthlink.net>
Re: new dbi connection for each request under mod_perl <michaelgang@gmail.com>
Re: new dbi connection for each request under mod_perl <OJZGSRPBZVCX@spammotel.com>
perl runtime model <pengyu.ut@gmail.com>
Re: perl runtime model <smallpond@juno.com>
Re: perl runtime model <OJZGSRPBZVCX@spammotel.com>
Prada shoes (paypal payment)(http://www.honest-trade06. <ccharley69@yahoo.com>
search and replace in Perl <dominicphilsby@googlemail.com>
Re: search and replace in Perl <OJZGSRPBZVCX@spammotel.com>
Re: search and replace in Perl <glennj@ncf.ca>
Re: search and replace in Perl <john@castleamber.com>
Re: Subroutines and $_[0] <willem@stack.nl>
Re: Subroutines and $_[0] <uri@StemSystems.com>
Re: Subroutines and $_[0] <me@me.com>
Re: Subroutines and $_[0] <OJZGSRPBZVCX@spammotel.com>
Re: Subroutines and $_[0] <john@castleamber.com>
Re: Subroutines and $_[0] <john@castleamber.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 18 Jan 2010 08:03:22 -0800 (PST)
From: ccc31807 <cartercc@gmail.com>
Subject: embedded Perl process in Apache using mod_perl
Message-Id: <caa7bf31-c91a-40a0-a020-5f2ff2cf442e@34g2000yqp.googlegroups.com>
Could someone give a simple and short explanation of what it means to
say that mod_perl embeds a Perl process in Apache?
I understand that classical CGI passes requests for a script to Perl,
which fires up a Perl process, runs the script, returns a value, and
shuts down. I also understand that FastCGI does the same thing except
that the Perl process doesn't start and stop with each request but
remains running in the background.
However, I don't understand t mechanism of an embedded Perl process in
mod_perl, and the question seems so elementary that the documentation
doesn't address it. Or maybe I just haven't read the right
documentation.
Thanks, CC.
------------------------------
Date: Mon, 18 Jan 2010 09:49:03 -0800
From: Keith Thompson <kst-u@mib.org>
Subject: Re: FAQ 7.28 How do I clear a package?
Message-Id: <lnzl4b9ucw.fsf@nuthaus.mib.org>
PerlFAQ Server <brian@theperlreview.com> writes:
> 7.28: How do I clear a package?
>
> Use this code, provided by Mark-Jason Dominus:
>
> sub scrub_package {
> no strict 'refs';
> my $pack = shift;
> die "Shouldn't delete main package"
> if $pack eq "" || $pack eq "main";
> my $stash = *{$pack . '::'}{HASH};
> my $name;
> foreach $name (keys %$stash) {
> my $fullname = $pack . '::' . $name;
> # Get rid of everything with that name.
> undef $$fullname;
> undef @$fullname;
> undef %$fullname;
> undef &$fullname;
> undef *$fullname;
> }
> }
>
> Or, if you're using a recent release of Perl, you can just use the
> Symbol::delete_package() function instead.
I think this answer would be improved by explaining what it means to
"clear" a package, and why one would want to do so (I can't think of a
good reason off the top of my head).
Oh, and the indentation of the second-to-last "}" is off.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
------------------------------
Date: Mon, 18 Jan 2010 16:55:47 +0100
From: Helmut Richter <hhr-m@web.de>
Subject: Re: FAQ 9.16 How do I decode a CGI form?
Message-Id: <Pine.LNX.4.64.1001181618010.4463@lxhri01.lrz.lrz-muenchen.de>
On Sat, 16 Jan 2010, Mumia W. wrote:
> I upgraded to CGI.pm 3.48; I had no idea that I wasn't using the correct
> version; CGI.pm 3.29 (Debian Lenny) doesn't complain if you give it an
> unused/invalid option:
>
> use CGI qw/-utf44/; # There is no utf44; there is no complaint either.
I have even 3.15 on the computer where the script runs (SuSE 10). There is
no mention of "utf8" in the source, and probably not in 3.29 either.
I will install the new module in another place and then test again.
> Strangely, after I upgraded to 3.48, your program worked perfectly. All I need
> do is type in "München" for the location, and the file is accepted and is not
> corrupted. From my point of view, CGI.pm 3.48 does option "e" described
> above--possibly with some magic to exclude binary files from the utf8
> conversion.
If so, the documentation could do with a little update. I'll make a
suggstion as soon as I have tested.
> The changes I made to your program were modest (no real changes). I placed a
> copy here:
> http://home.earthlink.net/~mumia.w.18.spam/docs/try-binary1.txt
Could you please leave it there for a while? Or should I get my own copy?
Thank you.
--
Helmut Richter
------------------------------
Date: Mon, 18 Jan 2010 05:39:03 -0800 (PST)
From: david <michaelgang@gmail.com>
Subject: new dbi connection for each request under mod_perl
Message-Id: <dd2efddf-30c2-4eb1-bb10-d7389c335ada@m3g2000yqf.googlegroups.com>
Hi all,
I have an application that i want to move to mod_perl.
My problem is that under the application i have a class variable that
makes caching of the database handler of dbi
This is how the class looks like
package Connection;
use DBI;
my $dbh;
sub instance {
if (!$dbh) {
$dbh = DBI->new;
}
return $dbh;
}
Before it was nice, because if in the same request i wanted to get
more than one connection it returned me the same.
But here in mod_perl i get for different requests the same dbh and the
application relies that it won't be the connection. For example, i
create temporary tables that are deleted automatically after each
request, because the connection expires, but now not.
What is the solution for this problem ?
Thanks in advance,
David
------------------------------
Date: Mon, 18 Jan 2010 09:53:38 -0600
From: "Mumia W." <paduille.4061.mumia.w+nospam@earthlink.net>
Subject: Re: new dbi connection for each request under mod_perl
Message-Id: <NKGdnX6K3d-GG8nWnZ2dnUVZ_r6dnZ2d@earthlink.com>
On 01/18/2010 07:39 AM, david wrote:
> Hi all,
>
> I have an application that i want to move to mod_perl.
> My problem is that under the application i have a class variable that
> makes caching of the database handler of dbi
This sentence is unfinished.
> This is how the class looks like
> package Connection;
> use DBI;
>
> my $dbh;
>
> sub instance {
> if (!$dbh) {
> $dbh = DBI->new;
> }
> return $dbh;
> }
>
> Before it was nice, because if in the same request i wanted to get
> more than one connection it returned me the same.
> But here in mod_perl i get for different requests the same dbh and the
> application relies that it won't be the connection.
That sentence is difficult to parse, but I think you're saying that you
get the same $dbh for different requests, and you don't want that.
> For example, i
> create temporary tables that are deleted automatically after each
> request, because the connection expires, but now not.
> What is the solution for this problem ?
>
> Thanks in advance,
> David
You say you're moving the application to mod_perl, but you didn't say
what you're moving it from--normal CGI? Are you using Registry scripts
or Perlrun?
Anyway, do this search:
http://search.aol.com/aol/search?s_it=topsearchbox.search&q=mod+perl+END
The second result shows how to use register_cleanup().
------------------------------
Date: Mon, 18 Jan 2010 09:04:50 -0800 (PST)
From: david <michaelgang@gmail.com>
Subject: Re: new dbi connection for each request under mod_perl
Message-Id: <4af1a325-8bf7-4b47-a82f-2447f5e6cbc4@j19g2000yqk.googlegroups.com>
On Jan 18, 5:53=A0pm, "Mumia W." <paduille.4061.mumia.w
+nos...@earthlink.net> wrote:
> On 01/18/2010 07:39 AM, david wrote:
>
> > Hi all,
>
> > I have an application that i want to move to mod_perl.
> > My problem is that under the application i have a class variable that
> > makes caching of the database handler of dbi
>
> This sentence is unfinished.
>
>
>
> > This is how the class looks like
> > package Connection;
> > use DBI;
>
> > my $dbh;
>
> > sub instance {
> > if (!$dbh) {
> > $dbh =3D DBI->new;
> > }
> > return $dbh;
> > }
>
> > Before it was nice, because if in the same request i wanted to get
> > more than one connection it returned me the same.
> > But here in mod_perl i get for different requests the same dbh and the
> > application relies that it won't be the connection.
>
> That sentence is difficult to parse, but I think you're saying that you
> get the same $dbh for different requests, and you don't want that.
>
> > For example, i
> > create temporary tables that are deleted automatically after each
> > request, because the connection expires, but now not.
> > What is the solution for this problem ?
>
> > Thanks in advance,
> > David
>
> You say you're moving the application to mod_perl, but you didn't say
> what you're moving it from--normal CGI? Are you using Registry scripts
> or Perlrun?
>
> Anyway, do this search:http://search.aol.com/aol/search?s_it=3Dtopsearchb=
ox.search&q=3Dmod+perl+END
>
> The second result shows how to use register_cleanup().
I wanted to move a normal cgi to mod_perl.
Thanks a lot for the link
------------------------------
Date: Mon, 18 Jan 2010 19:46:33 +0100
From: "Jochen Lehmeier" <OJZGSRPBZVCX@spammotel.com>
Subject: Re: new dbi connection for each request under mod_perl
Message-Id: <op.u6qrrvl2mk9oye@frodo>
On Mon, 18 Jan 2010 14:39:03 +0100, david <michaelgang@gmail.com> wrote:
> What is the solution for this problem ?
Google "mod_perl cleanup" or check out the mod_perl documentation, should
be pretty clear.
------------------------------
Date: Mon, 18 Jan 2010 07:01:03 -0800 (PST)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: perl runtime model
Message-Id: <0660610a-c0d7-4d97-bf2e-37785d624c8c@a15g2000yqm.googlegroups.com>
Could somebody let me know a reference on the runtime of a perl
script? I.e how a perl script run? What parts make a perl script be
slower than an equivalent, say, C programe?
------------------------------
Date: Mon, 18 Jan 2010 07:55:08 -0800 (PST)
From: smallpond <smallpond@juno.com>
Subject: Re: perl runtime model
Message-Id: <ade04b08-9e57-4336-bbf0-18de50fbc35b@u41g2000yqe.googlegroups.com>
On Jan 18, 10:01=A0am, Peng Yu <pengyu...@gmail.com> wrote:
> Could somebody let me know a reference on the runtime of a perl
> script? I.e how a perl script run? What parts make a perl script be
> slower than an equivalent, say, C programe?
What do you mean by equivalent?
Perl scalar variables have runtime type checking and conversion. A
"string" in C is just a pointer to memory with no checking, so of
course the code runs much faster.
If you wrote a C library that supported Perl scalar data types it's
not clear that C would be much faster. And C doesn't have hash
or array data types at all.
------------------------------
Date: Mon, 18 Jan 2010 20:03:02 +0100
From: "Jochen Lehmeier" <OJZGSRPBZVCX@spammotel.com>
Subject: Re: perl runtime model
Message-Id: <op.u6qsjcswmk9oye@frodo>
On Mon, 18 Jan 2010 16:01:03 +0100, Peng Yu <pengyu.ut@gmail.com> wrote:
> Could somebody let me know a reference on the runtime of a perl
> script? I.e how a perl script run?
If you are asking for the internal execution details, then you have to
read the source code (of perl, not your script). It's fun!
Everything else is explained to total exhaustion in "man perl" (also fun
to read through).
> What parts make a perl script be slower than an equivalent, say, C
> programe?
Google "interpreted vs compiled".
Plus, just for your information, your question is pure flame bait - don't
be irritated if you get heated answers.
------------------------------
Date: Mon, 18 Jan 2010 04:37:21 -0800 (PST)
From: charly <ccharley69@yahoo.com>
Subject: Prada shoes (paypal payment)(http://www.honest-trade06.com)
Message-Id: <dce35956-93ce-40eb-8ba0-c19a47536ccc@c34g2000yqn.googlegroups.com>
Footwear (paypal payment) (http://www.honest-trade06.com )
nike,adidas shoes
Paul Smith shoes
Jordan shoes
Bape shoes
Chanel shoes (paypal payment)(http://www.honest-trade06.com)
D&G shoes
Dior shoes
ED hardy shoes
Evisu shoes
Fendi shoes (paypal payment)(http://www.honest-trade06.com)
Gucci shoes
Hogan shoes (paypal payment)(http://www.honest-trade06.com)
Lv shoes
Prada shoes (paypal payment)(http://www.honest-trade06.com)
Timberland shoes
Tous shoes
Ugg shoes (paypal payment)(http://www.honest-trade06.com)
Ice cream shoes
Sebago shoes (paypal payment)( http:/www.honest-trade06.com)
Lacoste shoes
Air force one shoes (paypal payment)(http://www.honest-trade06.com)
TODS shoes
AF shoes
More detail land, address:http://www.honest-trade06.com
------------------------------
Date: Mon, 18 Jan 2010 09:40:44 -0800 (PST)
From: Dominic Philsby <dominicphilsby@googlemail.com>
Subject: search and replace in Perl
Message-Id: <4b3e9a06-4fdd-41a9-ae81-baed97a71e5c@f12g2000yqn.googlegroups.com>
Hi, I'm using Perl to do simple text search & replace within a text
file. The Perl version, sample file, and commandline syntax I am using
is shown below.
C:\test>
C:\test>
C:\test>type file.txt
the quick brown cow jumps over the lazy horse
C:\test>
C:\test>
C:\test>perl -p -e "s/cow/fox/g;s/horse/dog/g" file.txt
the quick brown fox jumps over the lazy dog
C:\test>
C:\test>
C:\test>perl -v
This is perl, v5.6.1 built for MSWin32-x86
Copyright 1987-2001, Larry Wall
Perl may be copied only under the terms of either the Artistic License
or the
GNU General Public License, which may be found in the Perl 5 source
kit.
Complete documentation for Perl, including FAQ lists, should be found
on
this system using `man perl' or `perldoc perl'. If you have access to
the
Internet, point your browser at http://www.perl.com/, the Perl Home
Page.
C:\test>
C:\test>
C:\test>
My file.txt document contains only one line but the real files are
several hundred thousand lines.
The words I am changing are not just "cow" and "horse" but hundreds of
words.
I am using Windows.
In my commandline program, my question is rather than specifying "s/
cow/fox/g;s/horse/dog/g" on the commandline, I want to reference a
file containing this. In otherwords, I want my commandline program to
reference a text file, lets call it regexReplace.txt, containing the
following
s/cow/fox/g;
s/horse/dog/g;
Can someone help me out with the syntax or how to do this?
Thank you
Dominic
------------------------------
Date: Mon, 18 Jan 2010 19:55:19 +0100
From: "Jochen Lehmeier" <OJZGSRPBZVCX@spammotel.com>
Subject: Re: search and replace in Perl
Message-Id: <op.u6qr6h1amk9oye@frodo>
On Mon, 18 Jan 2010 18:40:44 +0100, Dominic Philsby
<dominicphilsby@googlemail.com> wrote:
> Can someone help me out with the syntax or how to do this?
Pseudo code, untested and possibly incomplete, just to get you started:
open(REGS,"<regs.txt") or die "regs.txt: $!";
my @regs=<REGS>;
open(LINES,"<lines.txt") or die "lines.txt: $!";
while (<LINES>)
{
my $line=$_;
foreach my $reg (@regs)
{
eval "\$line =~ $reg"; # HUGE SECURITY RISK IF YOU ARE NOT
ABSOLUTE SURE ABOUT THE CONTENTS OF regs.txt
}
print $line;
}
You can implement a better (more elegant, more efficient) solution if you
don't store "s/.../.../g" in your file and use those strings directly, but
parse it and pre-calculate regular expression objects outside of the loop
(hint: perldoc perlop, look for "qr").
Have fun!
------------------------------
Date: 18 Jan 2010 19:03:42 GMT
From: Glenn Jackman <glennj@ncf.ca>
Subject: Re: search and replace in Perl
Message-Id: <slrnhl9c4f.7ak.glennj@smeagol.ncf.ca>
At 2010-01-18 12:40PM, "Dominic Philsby" wrote:
[...]
> My file.txt document contains only one line but the real files are
> several hundred thousand lines.
> The words I am changing are not just "cow" and "horse" but hundreds of
> words.
>
> In my commandline program, my question is rather than specifying "s/
> cow/fox/g;s/horse/dog/g" on the commandline, I want to reference a
> file containing this. In otherwords, I want my commandline program to
> reference a text file, lets call it regexReplace.txt, containing the
> following
>
> s/cow/fox/g;
> s/horse/dog/g;
>
Just replace the "-e script" with the name of the perl script
perl -p regexReplace.txt file.txt
It would be better to name it "regexReplace.pl" to indicate the nature
of the file.
If you're just replacing words (i.e. not really using regular
expressions) this _might_ be faster:
my %replacements = (
cow => "fox",
horse => "dog",
);
while (<>) {
while (my ($old, $new) = each %replacements) {
my $idx = -1;
while (($idx = index($_, $old, $idx)) > -1) {
substr($_, $idx, length($old)) = $new;
}
}
print;
}
then
perl replacement.pl file.txt
--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
------------------------------
Date: Mon, 18 Jan 2010 13:06:11 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: search and replace in Perl
Message-Id: <87hbqj5j30.fsf@castleamber.com>
"Jochen Lehmeier" <OJZGSRPBZVCX@spammotel.com> writes:
> open(REGS,"<regs.txt") or die "regs.txt: $!";
You might want to write:
my $filename = 'regs.txt';
open my $regs, '<', $filename
or die "Can't open '$filename' for reading: $!";
> while (<LINES>)
> {
> my $line=$_;
Why not:
while ( my $line = <$fh> ) {
(assuming you did open my $fh ... )
--
John Bokma j3b
Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
------------------------------
Date: Mon, 18 Jan 2010 17:18:23 +0000 (UTC)
From: Willem <willem@stack.nl>
Subject: Re: Subroutines and $_[0]
Message-Id: <slrnhl95uv.1tbr.willem@turtle.stack.nl>
Uri Guttman wrote:
) or even this so you can declare $title and make sure it is set to
) something useful
)
) my $title = $_[0] =~ m{...}) ? $1 : '' ;
)
) but i still can't see why he had to save the value in a lexical to make
) it work. i think there is unpasted code that affects things.
Because otherwise $_[0] is an alias for $1, and the next regular
expression will change the value of $1, and therefore the value of $_[0] ?
This is a pretty basic perl gotcha, you know.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
------------------------------
Date: Mon, 18 Jan 2010 12:40:42 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Subroutines and $_[0]
Message-Id: <878wbvmhut.fsf@quad.sysarch.com>
>>>>> "W" == Willem <willem@stack.nl> writes:
W> Uri Guttman wrote:
W> ) or even this so you can declare $title and make sure it is set to
W> ) something useful
W> )
W> ) my $title = $_[0] =~ m{...}) ? $1 : '' ;
W> )
W> ) but i still can't see why he had to save the value in a lexical to make
W> ) it work. i think there is unpasted code that affects things.
W> Because otherwise $_[0] is an alias for $1, and the next regular
W> expression will change the value of $1, and therefore the value of $_[0] ?
W> This is a pretty basic perl gotcha, you know.
he is doing m// ops which don't modify their arg. maybe he was doing
s/// ops but he didn't show any that i saw.
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: Mon, 18 Jan 2010 18:06:03 +0000
From: George <me@me.com>
Subject: Re: Subroutines and $_[0]
Message-Id: <hj27ub$jih$1@canard.ulcc.ac.uk>
Willem wrote:
> Uri Guttman wrote:
> ) or even this so you can declare $title and make sure it is set to
> ) something useful
> )
> ) my $title = $_[0] =~ m{...}) ? $1 : '' ;
> )
> ) but i still can't see why he had to save the value in a lexical to make
> ) it work. i think there is unpasted code that affects things.
>
> Because otherwise $_[0] is an alias for $1, and the next regular
> expression will change the value of $1, and therefore the value of $_[0] ?
>
> This is a pretty basic perl gotcha, you know.
>
>
> SaSW, Willem
I am actually not doing any substitutions, simply checking whether there
is a pattern match or not. I cut down the subroutine to:
sub check_url {
print "Orginal 1: ",$_[0],"\n";
my $test = "foobar12";
print "Test before: ",$test,"\n";
$test =~ m/([a-z]{1,})[0-9]{1,}/;
print "Test 2: ",$test,"\n";
print "Original argument: ",$_[0], "\n";
}
Now, if I run it the original argument (first print statement) prints as
<!--
-->
<div class="listing_results_logo"><a
href="http://www.test.com/hayles-accountants.html"><img border="0"
src="http://www.test.com/template/default-siva/images/noimage.gif"
alt="Hayles Accountants" /></a></div>
<div class="listing_results_listing">
<div class="listing_results_rating"></div>
<div class="listing_results_title"><a
href="http://www.test.com/hayles-accountants.html"><span
class="listing_default">Hayles Accountants</span></a> </div>
<div class="listing_results_address">
<!-- new code added by PMD GFX -->
Boston Rd,<br /> Hanwell,<br />
Middlesex W7 3TT <!-- end -->
<br /><br />
</div>
<div class="listing_results_description">
</div>
</div>
which is correct as well as the value of $test which is foobar12. My
understanding is that the next line will only check if the test variable
matches the pattern and will not make any changes to it, so the next
print statement is correct as well (am I right here in thinking that it
will just check if there is a match and not change the original variable)?
But, the print statement on $_[0] prints foobar - could you please
explain why this is the case?
Regards,
George
------------------------------
Date: Mon, 18 Jan 2010 19:43:31 +0100
From: "Jochen Lehmeier" <OJZGSRPBZVCX@spammotel.com>
Subject: Re: Subroutines and $_[0]
Message-Id: <op.u6qrmtg1mk9oye@frodo>
On Mon, 18 Jan 2010 18:40:42 +0100, Uri Guttman <uri@stemsystems.com>
wrote:
> [...]
Look what happened when I played around with the OP's case yesterday. I
thought I'd ignore what happened then, but I find it interesting, actually.
# perl -v
This is perl, v5.8.8 built for i486-linux-gnu-thread-multi
# perl -e '
"a" =~ m/(.*)/;
print "before: $1\n";
fn($1);
print "after: $1\n";
sub fn
{
print "inside 1: @_\n";
"b" =~ m//; # !!!!!
print "inside 2: @_\n"
}'
before: a
inside 1: a
inside 2: b
after: b # !!!!!
# perl -e '
"a" =~ m/(.*)/;
print "before: $1\n";
fn($1);
print "after: $1\n";
sub fn
{
print "inside 1: @_\n";
"b" =~ m/(.*)/; # !!!!!
print "inside 2: @_\n"
}'
before: a
inside 1: a
inside 2: b
after: a # !!!!!
First, some pertinent lines from the documentation:
perlvar on $1...: "These variables are all read-only and dynamically
scoped to the current BLOCK."
perlop: m// uses "the last successfully matched regular expression".
perlsub: "The array @_ is a local array, but its elements are aliases for
the actual scalar parameters."
So, in the first test, m// matches and uses the old regexp m/(.*)/. It
sets $1 from the point of the view of the sub *and* of the caller. ***This
contradicts the scoping to the current BLOCK*** if I'm not mistaken.
In the second test, m/(.*)/ matches. It sets $1 from the point of view of
the sub (it's not in the code but we can assume that ;-) ) ****and also
$_[0]**** which is aliased to $1 (in the caller). ***But it does not set
$1*** from the point of view of the caller. ***How does it know that $_[0]
is $1?***
There is something very funny going on, which I would definitely not have
expected.
I think one could explain the first effect (that m// overwrites the
original $1) by assuming that the $1 is actually linked to the regexp, and
by re-using the old regexp, is overwritten. Though this is not documented.
The second escapes me.
------------------------------
Date: Mon, 18 Jan 2010 12:51:22 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: Subroutines and $_[0]
Message-Id: <87pr575jrp.fsf@castleamber.com>
George <me@me.com> writes:
> sub check_url {
>
> print "Orginal 1: ",$_[0],"\n";
> my $test = "foobar12";
> print "Test before: ",$test,"\n";
> $test =~ m/([a-z]{1,})[0-9]{1,}/;
> print "Test 2: ",$test,"\n";
> print "Original argument: ",$_[0], "\n";
>
> }
perl -e '
use strict;
use warnings;
sub check_url {
print "Orginal 1: ",$_[0],"\n";
my $test = "foobar12";
print "Test before: ",$test,"\n";
$test =~ m/([a-z]{1,})[0-9]{1,}/;
print "Test 2: ",$test,"\n";
print "Original argument: ",$_[0], "\n";
}
my $url = "http://example.com/";
check_url( $url );
print "\n\nAnd here it goes wrong...\n";
$url =~ /(.*)/;
check_url( $1 );
'
Orginal 1: http://example.com/
Test before: foobar12
Test 2: foobar12
Original argument: http://example.com/
And here it goes wrong...
Orginal 1: http://example.com/
Test before: foobar12
Test 2: foobar12
Original argument: foobar
Since $_[ 0 ] is an alias for $1, you modify $_[ 0 ] if you modify $1 in
your regexp.
Solution: use:
my $url = shift;
at the start of your sub, and replace $_[0] with $url in the rest of
your code.
Another tip:
print "Original 1: ", $_[0], "\n";
Can be written as:
print "Original 1: $_[0]\n";
etc.
--
John Bokma j3b
Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
------------------------------
Date: Mon, 18 Jan 2010 13:00:17 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: Subroutines and $_[0]
Message-Id: <87ljfv5jcu.fsf@castleamber.com>
"Uri Guttman" <uri@StemSystems.com> writes:
>>>>>> "W" == Willem <willem@stack.nl> writes:
>
> W> Uri Guttman wrote:
> W> ) or even this so you can declare $title and make sure it is set to
> W> ) something useful
> W> )
> W> ) my $title = $_[0] =~ m{...}) ? $1 : '' ;
> W> )
> W> ) but i still can't see why he had to save the value in a lexical to make
> W> ) it work. i think there is unpasted code that affects things.
>
> W> Because otherwise $_[0] is an alias for $1, and the next regular
> W> expression will change the value of $1, and therefore the value of $_[0] ?
>
> W> This is a pretty basic perl gotcha, you know.
>
> he is doing m// ops which don't modify their arg. maybe he was doing
> s/// ops but he didn't show any that i saw.
He passes $1 as an argument ;-)
--
John Bokma j3b
Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
------------------------------
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 2774
***************************************