[32457] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3724 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jun 23 16:09:19 2012

Date: Sat, 23 Jun 2012 13: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           Sat, 23 Jun 2012     Volume: 11 Number: 3724

Today's topics:
        "use" inside of a subroutine <jwcarlton@gmail.com>
    Re: "use" inside of a subroutine <rweikusat@mssgmbh.com>
    Re: "use" inside of a subroutine <ben@morrow.me.uk>
    Re: "use" inside of a subroutine <jurgenex@hotmail.com>
    Re: "use" inside of a subroutine <xhoster@gmail.com>
    Re: "use" inside of a subroutine <jwcarlton@gmail.com>
    Re: "use" inside of a subroutine <ben@morrow.me.uk>
    Re: Error Handling in Net::SSH::Perl <ben@morrow.me.uk>
    Re: File::glob pattern matching <m@rtij.nl.invlalid>
    Re: File::glob pattern matching <ben@morrow.me.uk>
    Re: File::glob pattern matching <m@rtij.nl.invlalid>
    Re: File::glob pattern matching <marc.girod@gmail.com>
    Re: Question about a variable in list-context <willem@toad.stack.nl>
    Re: Question about a variable in list-context <markus.hutmacher@web.de>
    Re: Question about a variable in list-context <jurgenex@hotmail.com>
    Re: question concerning pipes and large strings <marc.girod@gmail.com>
    Re: Regex losing <br> (different from the earlier topic <jwcarlton@gmail.com>
    Re: Regex losing <br> (different from the earlier topic <ben@morrow.me.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 22 Jun 2012 14:24:15 -0700 (PDT)
From: Jason C <jwcarlton@gmail.com>
Subject: "use" inside of a subroutine
Message-Id: <0044cb6b-8f26-4094-95a5-b8a15721197a@googlegroups.com>

This is both a "can I" and "should I" question. Meaning, does it work, and if so, is there a reason to not do it?

I have a variables.lib that I use to hold most of my recurring variables and functions, then "require 'variables.lib' in the Perl scripts.

What I'm curious about is, can I "use" a module in a subroutine in the variables.lib, or does it have to be "use"d outside of the sub or in the main script?

Example:

# variables.lib
sub copyFile {
  use File::Copy;

  # Use: copyFile('file.txt', 'newfile.txt');
  copy("$basepath/$_[0]", "$basepath/$_[1]")
    or die "Copy failed: $!";
}

1;

That example is just typed up for this post, so please forgive any typos or logic errors. And if this is acceptable, then I would be doing it with several modules, not just File::Copy, this is isn't a module-specific question.

If "use"ing a module like this inside of a sub is acceptable, then am I correct that it wouldn't be loaded until (or unless) necessary? Or does Perl read ahead and load everything at once, regardless of whether the function or module is actually used?


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

Date: Fri, 22 Jun 2012 22:35:01 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: "use" inside of a subroutine
Message-Id: <87fw9n81yy.fsf@sapphire.mobileactivedefense.com>

Jason C <jwcarlton@gmail.com> writes:

[...]

> # variables.lib
> sub copyFile {
>   use File::Copy;
>
>   # Use: copyFile('file.txt', 'newfile.txt');
>   copy("$basepath/$_[0]", "$basepath/$_[1]")
>     or die "Copy failed: $!";
> }

[...]

> If "use"ing a module like this inside of a sub is acceptable, then
> am I correct that it wouldn't be loaded until (or unless) necessary?
> Or does Perl read ahead and load everything at once, regardless of
> whether the function or module is actually used?

Have you considered to 'ask' the documentation first?

	use Module LIST

	Imports some semantics into the current package from the named
	module, generally by aliasing certain subroutine or variable
	names into your package. It is exactly equivalent to

		BEGIN { require Module; Module->import( LIST ); }

	except that Module must be a bareword.

The 'BEGIN' implies that this code will run during the compilation
phase as soon as it has been parsed completely.

Apart from that, I would recommend against 'use of use in
subroutines': This doesn't really do anything other than an ordinary
'use Something' (AFAIK) but 'hides' external depedencies in a place
people usually wouldn't expect them.



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

Date: Fri, 22 Jun 2012 22:54:19 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: "use" inside of a subroutine
Message-Id: <b56fb9-fif2.ln1@anubis.morrow.me.uk>


Quoth Jason C <jwcarlton@gmail.com>:
> This is both a "can I" and "should I" question. Meaning, does it work,
> and if so, is there a reason to not do it?
> 
> I have a variables.lib that I use to hold most of my recurring variables
> and functions, then "require 'variables.lib' in the Perl scripts.

It's usual to give Perl libraries (to be loaded with require) a '.pl'
extension. It's also better nowadays to write a proper '.pm' module
instead.

I am curious as to what a 'recurring variable' is.

> What I'm curious about is, can I "use" a module in a subroutine in the
> variables.lib, or does it have to be "use"d outside of the sub or in the
> main script?
> 
> Example:
> 
> # variables.lib
> sub copyFile {
>   use File::Copy;
> 
>   # Use: copyFile('file.txt', 'newfile.txt');
>   copy("$basepath/$_[0]", "$basepath/$_[1]")
>     or die "Copy failed: $!";
> }

This will work, but you should not do it (at least, not with an ordinary
module like File::Copy). The reason for that is that 'use' happens at
compile time, so the module will be loaded when the file is required,
not when the sub is called. Strictly it does no harm to put the 'use'
inside a sub, but it does no good either, and it may encourage people
reading your code to fall into the same mistake as you just have.

The exception to this is modules like 'strict' and 'warnings' which have
a lexically-scoped compile-time effect. It is sensible to 'use warnings'
or 'no warnings' inside a sub, since 'warnings' has to be invoked at
compile time to have any effect, and it only affects the code in the
block it was used in. Generally speaking the only modules which work
like this are the lower-case modules supplied with the perl
distribution.

> If "use"ing a module like this inside of a sub is acceptable, then am I
> correct that it wouldn't be loaded until (or unless) necessary?

This is *not* correct. The module will always be loaded, at the point
where the file is required.

> Or does Perl read ahead and load everything at once, regardless of
> whether the function or module is actually used?

Not exactly. Perl does exactly what you ask it to do: if you ask it
(with 'use') to load a module at compile time, it does that. If you ask
it (with 'require') to load a module at runtime, it does that; but you
have to realise that 'require' will not export functions. If you want to
load a module on demand, you need to do it like this:

    sub copyFile {
        require File::Copy;

        File::Copy::copy(...) or die ...;
    }

Both the 'File::Copy::' prefix and the brackets around the function's
argument list are important.

Ben



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

Date: Fri, 22 Jun 2012 16:34:05 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: "use" inside of a subroutine
Message-Id: <t20au7dplut9ajacef24hnvv0lj75dj8gd@4ax.com>

Jason C <jwcarlton@gmail.com> wrote:
>What I'm curious about is, can I "use" a module in a subroutine in the variables.lib, or does it have to be "use"d outside of the sub or in the main script?

You _can_ use a "use" inside of a sub, but the effect is the same(*) as
if you would put it outside of the sub, because "use" is evaluated at
compile time.
Further details see "perldoc -f use"

jue

*: except for increased obscurity


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

Date: Fri, 22 Jun 2012 17:07:04 -0700
From: Xho Jingleheimerschmidt <xhoster@gmail.com>
Subject: Re: "use" inside of a subroutine
Message-Id: <4fe5086a$0$24441$ed362ca5@nr5-q3a.newsreader.com>

On 06/22/2012 02:24 PM, Jason C wrote:
> This is both a "can I" and "should I" question. Meaning, does it work, and if so, is there a reason to not do it?
>
> I have a variables.lib that I use to hold most of my recurring variables and functions, then "require 'variables.lib' in the Perl scripts.

A recurring variable?  Is that like a constant?  Any why not name your 
thingie variables.pl or variables.pm, so people would recognize it?


> What I'm curious about is, can I "use" a module in a subroutine in the variables.lib, or does it have to be "use"d outside of the sub or in the main script?
>
> Example:
>
> # variables.lib
> sub copyFile {
>    use File::Copy;
>
>    # Use: copyFile('file.txt', 'newfile.txt');
>    copy("$basepath/$_[0]", "$basepath/$_[1]")
>      or die "Copy failed: $!";
> }
>
> 1;
>
> That example is just typed up for this post, so please forgive any typos or logic errors. And if this is acceptable, then I would be doing it with several modules, not just File::Copy, this is isn't a module-specific question.

I do that often with peculiar modules that probably only serve a purpose 
in the context of one function.  That way if I copy the function to 
somewhere else, the "use" of the module comes with it.

But I wouldn't consider File::Copy to be peculiar, and so would just 
throw it up near the top of the module.

>
> If "use"ing a module like this inside of a sub is acceptable, then am I correct that it wouldn't be loaded until (or unless) necessary? Or does Perl read ahead and load everything at once, regardless of whether the function or module is actually used?
>

No, it is loaded at compile time.

Xho


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

Date: Fri, 22 Jun 2012 20:35:35 -0700 (PDT)
From: Jason C <jwcarlton@gmail.com>
Subject: Re: "use" inside of a subroutine
Message-Id: <931d197e-79f7-4d4b-b9db-f6d6975fb46c@googlegroups.com>

On Friday, June 22, 2012 5:54:19 PM UTC-4, Ben Morrow wrote:
> It's usual to give Perl libraries (to be loaded with require) a '.pl'
> extension. It's also better nowadays to write a proper '.pm' module
> instead.

Hmph, I didn't know that. These scripts actually date back to about 10 year=
s ago, so I just coded the "libraries" as .lib. It's too much to worry abou=
t changing right now, until a Perl update forces my hand :-)


> I am curious as to what a 'recurring variable' is.

Sorry, but since everyone that replied asked this, then obviously I should =
have been more clear!

I meant, variables that are used in all of my scripts. Example:

$home =3D "http://www.example.com";
$imagepath =3D $home . "/images";

$basepath =3D "/home/example";
$wwwpath =3D $basepath . "/www";

$mailprog =3D "/usr/sbin/sendmail";

and so on.


> Not exactly. Perl does exactly what you ask it to do: if you ask it
> (with 'use') to load a module at compile time, it does that. If you ask
> it (with 'require') to load a module at runtime, it does that; but you
> have to realise that 'require' will not export functions. If you want to
> load a module on demand, you need to do it like this:
>=20
>     sub copyFile {
>         require File::Copy;
>=20
>         File::Copy::copy(...) or die ...;
>     }
>=20
> Both the 'File::Copy::' prefix and the brackets around the function's
> argument list are important.

Just to make sure that I understand, are you saying that I could use requir=
e inside of the function instead of use, and the module would NOT be loaded=
 until necessary? Thereby making the scripts that do not use that particula=
r function marginally faster than if I had loaded all modules outside of th=
e function?

If so, is there a difference in the sample you posted, and using import? Ex=
ample:

sub copyFile {
  require File::Copy;
  File::Copy->import();

  copy(...) or die ...;
}

Oh, and thanks to all of you that have replied. I'm specifically replying t=
o Ben because of the require-import question that was segued in his post, b=
ut I do appreciate all of the advice.


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

Date: Sat, 23 Jun 2012 15:58:36 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: "use" inside of a subroutine
Message-Id: <s52hb9-ghm2.ln1@anubis.morrow.me.uk>


Quoth Jason C <jwcarlton@gmail.com>:
> On Friday, June 22, 2012 5:54:19 PM UTC-4, Ben Morrow wrote:
> > It's usual to give Perl libraries (to be loaded with require) a '.pl'
> > extension. It's also better nowadays to write a proper '.pm' module
> > instead.
> 
> Hmph, I didn't know that. These scripts actually date back to about 10
> years ago, so I just coded the "libraries" as .lib. It's too much to
> worry about changing right now, until a Perl update forces my hand :-)
> 
> > I am curious as to what a 'recurring variable' is.
> 
> Sorry, but since everyone that replied asked this, then obviously I
> should have been more clear!
> 
> I meant, variables that are used in all of my scripts. Example:
> 
> $home = "http://www.example.com";
> $imagepath = $home . "/images";
> 
> $basepath = "/home/example";
> $wwwpath = $basepath . "/www";
> 
> $mailprog = "/usr/sbin/sendmail";

You seem not to be using 'strict'. You should fix that as soon as you
can.

> > Not exactly. Perl does exactly what you ask it to do: if you ask it
> > (with 'use') to load a module at compile time, it does that. If you ask
> > it (with 'require') to load a module at runtime, it does that; but you
> > have to realise that 'require' will not export functions. If you want to
> > load a module on demand, you need to do it like this:
> > 
> >     sub copyFile {
> >         require File::Copy;
> > 
> >         File::Copy::copy(...) or die ...;
> >     }
> > 
> > Both the 'File::Copy::' prefix and the brackets around the function's
> > argument list are important.
> 
> Just to make sure that I understand, are you saying that I could use
> require inside of the function instead of use, and the module would NOT
> be loaded until necessary?

Yes.

> Thereby making the scripts that do not use
> that particular function marginally faster than if I had loaded all
> modules outside of the function?

Well, perhaps. If you're worried about load times you would be better
off switching from CGI (if that's what you're using) to FastCGI or
something else which uses a persistent Perl interpreter.

> If so, is there a difference in the sample you posted, and using import?
> Example:
> 
> sub copyFile {
>   require File::Copy;
>   File::Copy->import();
> 
>   copy(...) or die ...;
> }

That will work, but the brackets are mandatory. If you leave them off,
perl will not recognise 'copy' as a function, because it was not
imported at compile time.

Ben



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

Date: Fri, 22 Jun 2012 22:32:06 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Error Handling in Net::SSH::Perl
Message-Id: <mr4fb9-bcf2.ln1@anubis.morrow.me.uk>


Quoth dagomakoa <dago_makoa@hotmail.fr>:
> my $ssh;
> eval {
>    $ssh = Net::SSH::Perl->connect( ... );
> };
> if ($@) {
>   warn "Connect failed: $@\n";
> }
> 
> It does not work with me, should we remove the option -w on the shebang
> or remove use warnings / use strict?

No, that will certainly not help.

What error messages do you get, or what is not working?

Ben



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

Date: Fri, 22 Jun 2012 22:51:47 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: File::glob pattern matching
Message-Id: <3g2fb9-r16.ln1@news.rtij.nl>

On Fri, 22 Jun 2012 20:54:54 +0100, Rainer Weikusat wrote:

> bjlockie <bjlockie@lockie.ca> writes:
>> I tried [[:digit:]] which is posix but doesn't work.
>> I know ? matches any single character but I only want to match exactly
>> 2 digits.
> 
> [0-9][0-9]
> 
> should do the trick (if you're happy with standard 'western/ arabic'
> numbers).

or [0-9]{2}, or [[:digit:]]{2}.

M4




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

Date: Fri, 22 Jun 2012 22:33:01 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: File::glob pattern matching
Message-Id: <dt4fb9-bcf2.ln1@anubis.morrow.me.uk>


Quoth Martijn Lievaart <m@rtij.nl.invlalid>:
> On Fri, 22 Jun 2012 20:54:54 +0100, Rainer Weikusat wrote:
> 
> > bjlockie <bjlockie@lockie.ca> writes:
> >> I tried [[:digit:]] which is posix but doesn't work.
> >> I know ? matches any single character but I only want to match exactly
> >> 2 digits.
> > 
> > [0-9][0-9]
> > 
> > should do the trick (if you're happy with standard 'western/ arabic'
> > numbers).
> 
> or [0-9]{2}, or [[:digit:]]{2}.

The OP is using globs, not regexes.

Ben



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

Date: Sat, 23 Jun 2012 17:16:30 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: File::glob pattern matching
Message-Id: <e73hb9-reg.ln1@news.rtij.nl>

On Fri, 22 Jun 2012 22:33:01 +0100, Ben Morrow wrote:

> Quoth Martijn Lievaart <m@rtij.nl.invlalid>:
>> On Fri, 22 Jun 2012 20:54:54 +0100, Rainer Weikusat wrote:
>> 
>> > bjlockie <bjlockie@lockie.ca> writes:
>> >> I tried [[:digit:]] which is posix but doesn't work.
>> >> I know ? matches any single character but I only want to match
>> >> exactly 2 digits.
>> > 
>> > [0-9][0-9]
>> > 
>> > should do the trick (if you're happy with standard 'western/ arabic'
>> > numbers).
>> 
>> or [0-9]{2}, or [[:digit:]]{2}.
> 
> The OP is using globs, not regexes.

Oopsie.

M4


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

Date: Sat, 23 Jun 2012 11:37:56 -0700 (PDT)
From: Marc Girod <marc.girod@gmail.com>
Subject: Re: File::glob pattern matching
Message-Id: <16a7253f-eeb1-4bd1-9801-3b4e465b8174@v9g2000vbc.googlegroups.com>

On Jun 22, 10:33=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:

> > or [0-9]{2}, or [[:digit:]]{2}.
>
> The OP is using globs, not regexes.

But {2} is explicitly supported in File::Glob, and also works in
CORE::glob:

tmp> touch aa12bb
tmp> perl -le '@f=3Dglob(q(aa[0-9]{2}bb));print for @f'
aa12bb

Now, it is correct that [[:digit::]] doesn't work (and should?)

tmp> perl -M'File::Glob qw(:glob)' -le \
 '@f=3Dglob(q(aa[[:digit:]]{2}bb));print for @f'

Marc


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

Date: Fri, 22 Jun 2012 20:22:23 +0000 (UTC)
From: Willem <willem@toad.stack.nl>
Subject: Re: Question about a variable in list-context
Message-Id: <slrnju9kvv.18kt.willem@toad.stack.nl>

Markus Hutmacher wrote:
) Hello,
)
) in a recent thread in this group I found the following line of code:
)
) ($id) = $input =~ /^([^\t]+)\t/;
)
) I understand that ($id) means that $id is used in list-context which 
) means that the part of $input which matches [^\t]+ is assigned to $id. 
) I understand as well that the same line of code without the list-context 
) will assign a 0 or 1 to $id depending on "matches" or "matches not".
) But I don't understand to which part of the code the list-context refers.
)
) In other words: what is the list in the expression 
) $input =~ /^([^\t]+)\t/ 

The list-context is applied to the =~ operator, so the list is the return
value of the =~ operator.  This is then assigned to the list ($id), which
means the first value goes into $id and the rest is ignored.

Look at:

  @id = $input =~ /^(.)(.)(.)/;

And try to imagine what @id contains afterwards.


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: 22 Jun 2012 20:47:09 GMT
From: Markus Hutmacher <markus.hutmacher@web.de>
Subject: Re: Question about a variable in list-context
Message-Id: <a4k3udFp7lU2@mid.individual.net>

Am Fri, 22 Jun 2012 20:22:23 +0000 schrieb Willem:
> The list-context is applied to the =~ operator, so the list is the
> return value of the =~ operator.  This is then assigned to the list
> ($id), which means the first value goes into $id and the rest is
> ignored.
> 
> Look at:
> 
>   @id = $input =~ /^(.)(.)(.)/;
> 
> And try to imagine what @id contains afterwards.
> 
> 
> SaSW, Willem

Well, thanks for the explanation,

I did not see that but now I've understood. 
In your example @id would contain the variables $1, $2 and $3 which are 
assigned to the content of the brackets (.) and therefore ($id) would 
contain $1 in the above case.

-- 

Markus


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

Date: Fri, 22 Jun 2012 16:37:03 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Question about a variable in list-context
Message-Id: <o90au793mgtv7i5c1u85ar9ac1ash2si4t@4ax.com>

Markus Hutmacher <markus.hutmacher@web.de> wrote:
>In other words: what is the list in the expression 
>$input =~ /^([^\t]+)\t/ 

It is the return value of (when used in list context)
	/^([^\t]+)\t/ 

The $input =~ is irrelevant here, because it simply binds the RE to
$input instead of to the default $_.

jue


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

Date: Sat, 23 Jun 2012 03:44:34 -0700 (PDT)
From: Marc Girod <marc.girod@gmail.com>
Subject: Re: question concerning pipes and large strings
Message-Id: <ddc3b9e6-3441-4371-a1b9-e07060e5fe7f@e20g2000vbm.googlegroups.com>

I am enjoying your conversation.
Just a side comment not taking parts.

On Jun 22, 8:52=A0pm, Rainer Weikusat <rweiku...@mssgmbh.com> wrote:

> The state of certain sciences such as 'physics' or 'astronomy' has
> been 'good enough' for any even remotely practical purpose for
> something like a century or so.

It had been good enough, in retrospectively radically contradictory
ways, for several thousands of years before...
In fact, it is structurally good enough, isn't it?
The problems are always marginal, and ignored for practical purposes.

Marc


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

Date: Fri, 22 Jun 2012 21:00:57 -0700 (PDT)
From: Jason C <jwcarlton@gmail.com>
Subject: Re: Regex losing <br> (different from the earlier topic about losing $1)
Message-Id: <b773605a-c005-4ec6-b6aa-d8d82878f2db@googlegroups.com>

On Friday, June 22, 2012 5:07:40 AM UTC-4, Ben Morrow wrote:
> Unless $original is supposed to be a regex, you want \Q\E around it.

I originally did this in the function:

$original =3D quotemeta($original);
$text =3D~ ...;

Is there a difference between quotemeta() and \Q\E?


> You don't really need the final capture, you can just use lookahead.
> Similarly you don't need to capture more than one \s just to put it back
> again:
>=20
>     s/(\s|
> ) \Q$original\E (?=3D \s|
> )/$1$converted/ix;
>=20
> Turning the initial capture into lookbehind is harder, since Perl
> doesn't support variable-length lookbehind and the two branches of the
> alternation are different lengths. However, if you have at least 5.10
> (which you do, I hope), you can use \K like this:
>=20
>     s/ (?:\s|
> ) \K \Q$original\E (?=3D\s|
> ) /$converted/ix;

I'm afraid that you went just a little over my head on that one. What does =
the \K do? And what does (?=3D\s|<br>) do differently from (?:\s|<br>)? Or =
are they the same?

This is slightly different, but how do I include "or at the beginning of th=
e string" in that regex?

I don't think that this would work, would it?

((?:^|\s|<br>)*)

For this purpose, I'm specifically converting a string of "www.example.com"=
 to "http://www.example.com". A string like

$text =3D "Go to www.example.com";

matches, but

$text =3D "www.example.com<br><br>click here";

doesn't.

Further, in this case I don't want it to match when the www is between othe=
r characters (so that it doesn't change "http://www" to "http://http://www"=
), so I think I'll have to use a totally different regex without the traili=
ng. But I still need to figure out how to make it match if it follows a \s,=
 <br>, or is at the beginning of the string.


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

Date: Sat, 23 Jun 2012 15:44:30 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Regex losing <br> (different from the earlier topic about losing $1)
Message-Id: <eb1hb9-ghm2.ln1@anubis.morrow.me.uk>


Quoth Jason C <jwcarlton@gmail.com>:
> On Friday, June 22, 2012 5:07:40 AM UTC-4, Ben Morrow wrote:
> > Unless $original is supposed to be a regex, you want \Q\E around it.
> 
> I originally did this in the function:
> 
> $original = quotemeta($original);
> $text =~ ...;
> 
> Is there a difference between quotemeta() and \Q\E?

No.

> > You don't really need the final capture, you can just use lookahead.
> > Similarly you don't need to capture more than one \s just to put it back
> > again:
> > 
> >     s/(\s|
> > ) \Q$original\E (?= \s|
> > )/$1$converted/ix;
> > 
> > Turning the initial capture into lookbehind is harder, since Perl
> > doesn't support variable-length lookbehind and the two branches of the
> > alternation are different lengths. However, if you have at least 5.10
> > (which you do, I hope), you can use \K like this:
> > 
> >     s/ (?:\s|
> > ) \K \Q$original\E (?=\s|
> > ) /$converted/ix;
> 
> I'm afraid that you went just a little over my head on that one. What
> does the \K do? And what does (?=\s|<br>) do differently from
> (?:\s|<br>)? Or are they the same?

\K says 'pretend we started here'. If you say

    (my $x = "aaabbb") =~ s/a+ \K b+/XXX/x;

then the result is "aaaXXX", because even though the 'a's were matched,
the \K prevented them from being replaced.

(?=) is called 'positive lookahead': it checks that the pattern inside
it matches at this point, but then *doesn't* include the matched section
in the results. If you use it at the end of the pattern it comes out the
same as \K, except the other way around.

> This is slightly different, but how do I include "or at the beginning of
> the string" in that regex?
> 
> I don't think that this would work, would it?
> 
> ((?:^|\s|<br>)*)

No, because the whole group is optional (qualified with *). I think you
want

    (^|\s|<br>)

because you do want to insist on one of those being present.

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


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