Questions on porting Perl from Windows to Unix
Questions on porting Perl from Windows to Unix
am 24.01.2008 20:17:11 von Gary Yang
--===============1901961711==
Content-Type: multipart/alternative; boundary="0-1955723654-1201202231=:19473"
Content-Transfer-Encoding: 7bit
--0-1955723654-1201202231=:19473
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
Hi All,
=20
I need to port my Perl script from Unix to Windows. Can someone tell me h=
ow to figure out which platform I am on? i.e. I need to know which Perl c=
ommand can help me determin the platform. Once I know the platform, I'll =
code my script as the example bellow. But, how to figure out I am on Wind=
ows or Unix?
=20
if ($usingUNIX)
{
$directory =3D `ls`; #UNIX version.
$copyCommand =3D `cp`; #UNIX version.
}
else
{
$directory =3D `dir`; #Win32 version.
$copyCommand =3D `COPY`; #Win32 version.
}=20
=20
=20
Second question:
The UNIX #!/usr/bin/perl notation does not work with Perl scripts on Wind=
ows. How should I code if it is Unix I place "#!/usr/bin/perl" at the ver=
y first line of the script? But, I do not place it at the first line of c=
ode if it is not Unix? How should I do it?
=20
Your answers are greatly appreciated.
=20
=20
Thanks,
=20
=20
Gary
=20
=20
=20
---------------------------------
Never miss a thing. Make Yahoo your homepage.
--0-1955723654-1201202231=:19473
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
Hi All,
I need to port my Perl script from Unix to Wind=
ows. Can someone tell me how to figure out which platform I am on? i.e. I=
need to know which Perl command can help me determin the platform. Once =
I know the platform, I'll code my script as the example bellow. But, how =
to figure out I am on Windows or Unix?
if ($usingUNIX)
&n=
bsp; {
$directory =3D `ls`; =
#UNIX version.
$copyCommand =3D `cp`; =
#UNIX version.
}
else
{
> $directory =3D `dir`; #Win32 version.
=
$copyCommand =3D `COPY`; #Win32 version.
&nbs=
p; }
Second question:
The UNIX #!/usr/bi=
n/perl notation does not work with Perl scripts on Windows. How should I =
code if it is Unix I place "#!/usr/bin/perl" at the very first line of th=
e script? But, I do not place it at the first
line of code if it is not Unix? How should I do it?
Your an=
swers are greatly appreciated.
Thanks,
R>
Gary
Never miss a thing.
/evt=3D51438/*http://www.yahoo.com/r/hs"> Make Yahoo your homepage.
--0-1955723654-1201202231=:19473--
--===============1901961711==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1901961711==--
RE: Questions on porting Perl from Windows to Unix
am 24.01.2008 20:21:17 von Scott_Campbell
This is a multi-part message in MIME format.
--===============0865457515==
Content-class: urn:content-classes:message
Content-Type: multipart/alternative;
boundary="----_=_NextPart_001_01C85EBE.4AFDCFBE"
This is a multi-part message in MIME format.
------_=_NextPart_001_01C85EBE.4AFDCFBE
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
$^O
=20
=20
=20
if($^O!~m/Win32/){
#This is NOT Windows
}
else{
#This is Windows
}
=20
________________________________
From: activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of Gary
Yang
Sent: Thursday, January 24, 2008 2:17 PM
To: activeperl@listserv.ActiveState.com
Subject: Questions on porting Perl from Windows to Unix
=20
Hi All,
=20
I need to port my Perl script from Unix to Windows. Can someone tell me
how to figure out which platform I am on? i.e. I need to know which Perl
command can help me determin the platform. Once I know the platform,
I'll code my script as the example bellow. But, how to figure out I am
on Windows or Unix?
=20
if ($usingUNIX)
{
$directory =3D `ls`; #UNIX version.
$copyCommand =3D `cp`; #UNIX version.
}
else
{
$directory =3D `dir`; #Win32 version.
$copyCommand =3D `COPY`; #Win32 version.
}=20
=20
=20
Second question:
The UNIX #!/usr/bin/perl notation does not work with Perl scripts on
Windows. How should I code if it is Unix I place "#!/usr/bin/perl" at
the very first line of the script? But, I do not place it at the first
line of code if it is not Unix? How should I do it?
=20
Your answers are greatly appreciated.
=20
=20
Thanks,
=20
=20
Gary
=20
=20
=20
________________________________
Never miss a thing. Make Yahoo your homepage.
=20
------_=_NextPart_001_01C85EBE.4AFDCFBE
Content-Type: text/html;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
xmlns:o=3D"urn:schemas-microsoft-com:office:office" =
xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:st1=3D"urn:schemas-microsoft-com:office:smarttags" =
xmlns=3D"http://www.w3.org/TR/REC-html40">
charset=3Dus-ascii">
namespaceuri=3D"urn:schemas-microsoft-com:office:smarttags" =
name=3D"City"/>
namespaceuri=3D"urn:schemas-microsoft-com:office:smarttags"
name=3D"place"/>
style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>$^O
style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>
style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>
style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>
style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>if($^O!~m/Win32/){=
style=3D'font-size:
10.0pt;font-family:Arial;color:navy'> =
#This is NOT =
Windows
style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>}
style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>else{
style=3D'font-size:
10.0pt;font-family:Arial;color:navy'> =
#This is =
Windows
style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>}
style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>
size=3D3
face=3D"Times New Roman">
style=3D'font-size:10.0pt;
font-family:Tahoma;font-weight:bold'>From:
size=3D2
face=3DTahoma> =
activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com]
style=3D'font-weight:
bold'>On Behalf Of Gary Yang
Sent: Thursday, January =
24, 2008
2:17 PM
To:
activeperl@listserv.ActiveState.com
Subject: Questions on =
porting Perl
from Windows to Unix
style=3D'font-size:
12.0pt'>
style=3D'font-size:
12.0pt'>Hi All,
I need to port my Perl script from Unix to Windows. Can someone tell me =
how to
figure out which platform I am on? i.e. I need to know which Perl =
command can
help me determin the platform. Once I know the platform, I'll code my =
script as
the example bellow. But, how to figure out I am on Windows or Unix?
if ($usingUNIX)
{
$directory =3D `ls`; #UNIX version.
$copyCommand =3D `cp`; #UNIX version.
}
else
{
$directory =3D `dir`; #Win32 version.
$copyCommand =3D `COPY`; #Win32 version.
}
Second question:
The UNIX #!/usr/bin/perl notation does not work with Perl scripts on =
Windows.
How should I code if it is Unix I place "#!/usr/bin/perl" at =
the very
first line of the script? But, I do not place it at the first line of =
code if
it is not Unix? How should I do it?
Your answers are greatly appreciated.
Thanks,
w:st=3D"on">Gary
style=3D'font-size:
12.0pt'>
style=3D'font-size:12.0pt'>
size=3D3
face=3D"Times New Roman">
style=3D'font-size:
12.0pt'>Never miss a thing.
href=3D"http://us.rd.yahoo.com/evt=3D51438/*http:/www.yahoo. com/r/hs">Mak=
e Yahoo
your homepage.
------_=_NextPart_001_01C85EBE.4AFDCFBE--
--===============0865457515==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0865457515==--
RE: Questions on porting Perl from Windows to Unix
am 24.01.2008 20:59:16 von Curtis Leach
Also look into using File::Spec module for platform independent file
manipulation. Much easier to use than doing it yourself in your code.
Or using the glob() method instead of spawning calls to dir or ls.
There are usually modules you can download to avoid having to test $^O
settings and spawn platform dependant tasks to hide the mess.
As to windows, you have to associate the ".pl" extension with the
"perl.exe" executable so it will automatically run. It doesn't care
about the 1st line of the file like Unix does, windows is file extension
based. So by default I always put in #!/usr/bin/perl at the start of
every perl program, even on windows. Also if you put a perl program in
a scheduled task that requires command line arguments, you will need to
do "perl prog.pl -a -b -c", but if it doesn't take arguments, you can
just do "prog.pl" once the association is made.
Curtis
________________________________
From: activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of
Campbell, Scott
Sent: Thursday, January 24, 2008 1:21 PM
To: Gary Yang; activeperl@listserv.ActiveState.com
Subject: RE: Questions on porting Perl from Windows to Unix
$^O
if($^O!~m/Win32/){
#This is NOT Windows
}
else{
#This is Windows
}
________________________________
From: activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of Gary
Yang
Sent: Thursday, January 24, 2008 2:17 PM
To: activeperl@listserv.ActiveState.com
Subject: Questions on porting Perl from Windows to Unix
Hi All,
I need to port my Perl script from Unix to Windows. Can someone tell me
how to figure out which platform I am on? i.e. I need to know which Perl
command can help me determin the platform. Once I know the platform,
I'll code my script as the example bellow. But, how to figure out I am
on Windows or Unix?
if ($usingUNIX)
{
$directory = `ls`; #UNIX version.
$copyCommand = `cp`; #UNIX version.
}
else
{
$directory = `dir`; #Win32 version.
$copyCommand = `COPY`; #Win32 version.
}
Second question:
The UNIX #!/usr/bin/perl notation does not work with Perl scripts on
Windows. How should I code if it is Unix I place "#!/usr/bin/perl" at
the very first line of the script? But, I do not place it at the first
line of code if it is not Unix? How should I do it?
Your answers are greatly appreciated.
Thanks,
Gary
________________________________
Never miss a thing. Make Yahoo your homepage.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Questions on porting Perl from Windows to Unix
am 25.01.2008 13:15:53 von Brian Raven
From: activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of Gary
Yang
Sent: 24 January 2008 19:17
To: activeperl@listserv.ActiveState.com
Subject: Questions on porting Perl from Windows to Unix
> Hi All,
> =
> I need to port my Perl script from Unix to Windows. Can someone tell
me how to figure out which platform I am =
> on? i.e. I need to know which Perl command can help me determin the
platform. Once I know the platform, I'll =
> code my script as the example bellow. But, how to figure out I am on
Windows or Unix?
> =
> if ($usingUNIX)
> {
> $directory =3D `ls`; #UNIX version.
> $copyCommand =3D `cp`; #UNIX version.
> }
> else
> {
> $directory =3D `dir`; #Win32 version.
> $copyCommand =3D `COPY`; #Win32 version.
> } =
First, it is probably better if you write your scripts so that you don't
need to ask, i.e. aim for platform independence. Where this is not
possible there is the special variable $^O (See 'perldoc perlvar' for
details). If you need more detailed info about your platform you will
need the Config module (See 'perldoc Config'). Also see the Activestate
documentation for windows specific help.
> =
> =
> Second question:
> The UNIX #!/usr/bin/perl notation does not work with Perl scripts on
Windows. How should I code if it is Unix I > place "#!/usr/bin/perl" at
the very first line of the script? But, I do not place it at the first
line of code > if it is not Unix? How should I do it?
The #! notation us unix specific. Win32 achieves a similar effect by
associating file extensions with a specific executable, called file
association and managed with the assoc command, but I think that the
Activestate installation procedure sets up the necessary associations
for Perl, so you shouldn't need to. If you use the #! line for to
specify run time switches you can still do that as perl will parse that
as well (see 'perldoc perlrun').
HTH
-- =
Brian Raven =
==================== =====3D=
================
Atos Euronext Market Solutions Disclaimer
==================== =====3D=
================
The information contained in this e-mail is confidential and solely for the=
intended addressee(s). Unauthorised reproduction, disclosure, modification=
, and/or distribution of this email may be unlawful.
If you have received this email in error, please notify the sender immediat=
ely and delete it from your system. The views expressed in this message do =
not necessarily reflect those of Atos Euronext Market Solutions.
Atos Euronext Market Solutions Limited - Registered in England & Wales with=
registration no. 3962327. Registered office address at 25 Bank Street Lon=
don E14 5NQ United Kingdom. =
Atos Euronext Market Solutions SAS - Registered in France with registration=
no. 425 100 294. Registered office address at 6/8 Boulevard Haussmann 750=
09 Paris France.
L'information contenue dans cet e-mail est confidentielle et uniquement des=
tinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. Tou=
te copie, publication ou diffusion de cet email est interdite. Si cet e-mai=
l vous parvient par erreur, nous vous prions de bien vouloir prevenir l'exp=
editeur immediatement et d'effacer le e-mail et annexes jointes de votre sy=
steme. Le contenu de ce message electronique ne represente pas necessaireme=
nt la position ou le point de vue d'Atos Euronext Market Solutions.
Atos Euronext Market Solutions Limited Soci=E9t=E9 de droit anglais, enregi=
str=E9e au Royaume Uni sous le num=E9ro 3962327, dont le si=E8ge social se =
situe 25 Bank Street E14 5NQ Londres Royaume Uni.
Atos Euronext Market Solutions SAS, soci=E9t=E9 par actions simplifi=E9e, e=
nregistr=E9 au registre dui commerce et des soci=E9t=E9s sous le num=E9ro 4=
25 100 294 RCS Paris et dont le si=E8ge social se situe 6/8 Boulevard Hauss=
mann 75009 Paris France.
==================== =====3D=
================
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
$& and friends
am 29.01.2008 01:06:42 von Wayne Simmons
Ok I've read in the docs that $& and his mates $' and $` and some bad boys
that slow down all regular expressions in an entire program. So in an effort
to prevent having to use them I was going to use either:
@- and @+
Or
The index function
I was wondering if anyone knows whether the @- and @+ system variables were
bad like the saw ampersand and friends.
Also would they be better to use than searching the string again using the
index function?
-Wayne Simmons
--
Software Engineer
InterSystems USA, Inc.
303-858-1000
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: $& and friends
am 29.01.2008 01:31:50 von Bill Luebkert
Wayne Simmons wrote:
> Ok I've read in the docs that $& and his mates $' and $` and some bad boys
> that slow down all regular expressions in an entire program. So in an effort
> to prevent having to use them I was going to use either:
> @- and @+
> Or
> The index function
>
> I was wondering if anyone knows whether the @- and @+ system variables were
> bad like the saw ampersand and friends.
>
> Also would they be better to use than searching the string again using the
> index function?
It would help if you gave an example of what you're trying to accomplish.
I've used those vars maybe once or twice in testing, but never found a
need for them in any of my normal scripts (but then again, I bleieve number
vars ($1, $2, ...) have the same cost which I do use frequently).
It's a one-time penalty and I wouldn't really worry much about it. You could
time it and see what the penalty actually is and see if it's warranted.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: $& and friends
am 29.01.2008 20:05:52 von Wayne Simmons
>Bill Luebkert said:
>It would help if you gave an example of what you're trying to accomplish.
>I've used those vars maybe once or twice in testing, but never found a
>need for them in any of my normal scripts (but then again, I bleieve number
>vars ($1, $2, ...) have the same cost which I do use frequently).
>
>It's a one-time penalty and I wouldn't really worry much about it. You
>could time it and see what the penalty actually is and see if it's
>warranted.
Bill,
According to sawampersand.pm :
There's a global variable in the perl source, called PL_sawampersand.
It gets set to true in that moment in which the parser sees one of $`,
$', and $&. It never can be set to false again. Trying to set it to
false breaks the handling of the $`, $&, and $' completely.
If the global variable C is set to true, all
subsequent RE operations will be accompanied by massive in-memory
copying, because there is nobody in the perl source who could predict,
B the (necessary) copy for the ampersand family will be needed.
So B subsequent REs are considerable slower than necessary.
End quote
So the "bad variables" I'm concerned about are not the same as the $1, $2
variables, nor is it a "one time hit" as you indicate.
What I'm doing is detecting if a marker exists in a string then truncating
it at the marker point ie:
if ($buffer =~ /MARKER/)
{
$buffer = $&
#OR something like:
#$buffer = substr($buffer,0,$-[0]);
#OR even:
#$markerpos = index($buffer,MARKER);
#$buffer = substr($buffer,0,$markerpos);
}
It's just an efficiency question that I suppose is not critical in my
program but I always like to write efficient, maintainable code. Also I'm
still in the implementation phase, and can't really do timed tests until
later, so I was hoping a Guru (like yourself, or someone from activestate)
would have some personal knowledge on the matter.
-Wayne Simmons
--
Software Engineer
InterSystems USA, Inc.
303-858-1000
-----Original Message-----
From: Bill Luebkert [mailto:dbecoll@roadrunner.com]
Sent: Monday, January 28, 2008 5:32 PM
To: Wayne Simmons
Cc: activeperl@listserv.ActiveState.com
Subject: Re: $& and friends
Wayne Simmons wrote:
> Ok I've read in the docs that $& and his mates $' and $` and some bad boys
> that slow down all regular expressions in an entire program. So in an
effort
> to prevent having to use them I was going to use either:
> @- and @+
> Or
> The index function
>
> I was wondering if anyone knows whether the @- and @+ system variables
were
> bad like the saw ampersand and friends.
>
> Also would they be better to use than searching the string again using the
> index function?
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: $& and friends
am 29.01.2008 21:45:25 von ken1
> if ($buffer =~ /MARKER/)
> {
> $buffer = $&
> #OR something like:
> #$buffer = substr($buffer,0,$-[0]);
> #OR even:
> #$markerpos = index($buffer,MARKER);
> #$buffer = substr($buffer,0,$markerpos);
> }
>
Not that I'm an RE guru or anything but my code could look like
something like this:
# consider the marker to be '#'
$buffer = $1 if ($buffer =~ /(.*)#/);
Simple, sweet and to the point. I'm sure there are faster ways to do
this, but considering I run about a million iterations of this in 1.5 to
2 secs, it's fast enough. For me. In my small test. YMMV - you don't
mention the size of buffer, the size of marker, how often/time-critical
it can be. Also, nothing about the characteristics of the buffer, for
example, if the marker may occur several times - if so for example, you
might wish to make the * nongreedy:
# consider the marker to be '#'
$buffer = $1 if ($buffer =~ /(.*?)#/);
Then again, maybe the index/substr technique is better - it seems a lot
faster:
# consider the marker to be '#'
my $pos = index($buffer, '#');
$buffer = substr($buffer, 0, $pos) if $pos >= 0;
There are many ways to achieve your goal, but I guess only you can tell
as you know most about the characteristics of the data...FWIW, I
could'nt get $` to work. Sure, there are discussions about
maintainability that could be held, but there are as many opinions as
people so...
In general, I think Bill is right - don't worry too much about it. If
you need to optimize, do it later - if it's ever needed.
ken1
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: $& and friends
am 29.01.2008 22:02:16 von Andy_Bach
>> if ($buffer =~ /MARKER/)
> {
> $buffer = $&
> }
> Not that I'm an RE guru or anything but my code could look like
> something like this:
# consider the marker to be '#'
$buffer = $1 if ($buffer =~ /(.*)#/);
You might also look at using /g and, say, a while loop:
while ( $buffer =~ /(.+)MARKER/msg ) {
my $current_buf = $1;
# do what you like w/ the data
}
this'll step throught the $buffer one marker full at a time. That is data
like [1]
dlfkjaslf # lkasdfjdlks #
sdfslkjl
asldfkjd #
sfaslfdjdlk # asdfldsfas
Note, markers can be tricky, as is the ".+" ".*" etc. and if it's possibly
multi-line (handled above by the 's' switch) data, If it were a '#'
marker, I'd use a negative char class for the match:
while ( $buffer =~ /([^#]+)#/msg ) {
just to be safe.
a
[1]
m.pl:
#!/usr/bin/perl
use strict;
use warnings;
local $/ = undef;
my $buffer = <>;
print "Buf: $buffer|\n";
while ( $buffer =~ /(.+?)#/smg ) {
my $current_buf = $1;
print "CB: $current_buf|\n";
# do what you like w/ the data
}
m.dat:
dlfkjaslf # lkasdfjdlks #
sdfslkjl
asldfkjd #
sfaslfdjdlk # asdfldsfas
# perl /tmp/m.pl /tmp/m.dat
Buf: dlfkjaslf # lkasdfjdlks #
sdfslkjl
asldfkjd #
sfaslfdjdlk # asdfldsfas
|
CB: dlfkjaslf |
CB: lkasdfjdlks |
CB:
sdfslkjl
asldfkjd |
CB:
sfaslfdjdlk |
Andy Bach
Systems Mangler
Internet: andy_bach@wiwb.uscourts.gov
VOICE: (608) 261-5738 FAX 264-5932
"The Microsoft Exchange Information Store service depends on
the Microsoft Exchange Directory service which failed to
start because of the following error:
The operation completed successfully."
(actual MSE error msg)
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: $& and friends
am 30.01.2008 01:08:28 von Bill Luebkert
Wayne Simmons wrote:
>> Bill Luebkert said:
>> It would help if you gave an example of what you're trying to accomplish.
>> I've used those vars maybe once or twice in testing, but never found a
>> need for them in any of my normal scripts (but then again, I bleieve number
>> vars ($1, $2, ...) have the same cost which I do use frequently).
>>
>> It's a one-time penalty and I wouldn't really worry much about it. You
>> could time it and see what the penalty actually is and see if it's
>> warranted.
>
> Bill,
>
> According to sawampersand.pm :
>
> There's a global variable in the perl source, called PL_sawampersand.
> It gets set to true in that moment in which the parser sees one of $`,
> $', and $&. It never can be set to false again. Trying to set it to
> false breaks the handling of the $`, $&, and $' completely.
>
> If the global variable C is set to true, all
> subsequent RE operations will be accompanied by massive in-memory
> copying, because there is nobody in the perl source who could predict,
> B the (necessary) copy for the ampersand family will be needed.
> So B subsequent REs are considerable slower than necessary.
>
> End quote
>
> So the "bad variables" I'm concerned about are not the same as the $1, $2
> variables, nor is it a "one time hit" as you indicate.
I was using the man pages for my source - which looking at them a second time
are not all that explicit.
perlre man page:
WARNING: Once Perl sees that you need one of $&, $`, or $' anywhere in the
program, it has to provide them for every pattern match. This may
substantially slow your program. Perl uses the same mechanism to produce $1,
$2, etc, so you also pay a price for each pattern that contains capturing
parentheses. (To avoid this cost while retaining the grouping behaviour, use
the extended regular expression "(?: ... )" instead.) But if you never use
$&, $` or $', then patterns *without* capturing parentheses will not be
penalized. So avoid $&, $', and $` if you can, but if you can't (and some
algorithms really appreciate them), once you've used them once, use them at
will, because you've already paid the price. As of 5.005, $& is not so
costly as the other two.
The second last sentence kinda contradicts the first sentence, so without some
testing, I'm not sure what the full speed implications would be. As far as I'm
concerned, I will always be using ()s and number variables at will until I run
into a situation where it appears they are degrading my code excessively.
As far as your code below:
perlvar man page:
$` is the same as "substr($var, 0, $-[0])"
$& is the same as "substr($var, $-[0], $+[0] - $-[0])"
$' is the same as "substr($var, $+[0])"
$1 is the same as "substr($var, $-[1], $+[1] - $-[1])"
$2 is the same as "substr($var, $-[2], $+[2] - $-[2])"
$3 is the same as "substr($var, $-[3], $+[3] - $-[3])"
> What I'm doing is detecting if a marker exists in a string then truncating
> it at the marker point ie:
>
> if ($buffer =~ /MARKER/)
> {
> $buffer = $&
> #OR something like:
> #$buffer = substr($buffer,0,$-[0]);
> #OR even:
> #$markerpos = index($buffer,MARKER);
> #$buffer = substr($buffer,0,$markerpos);
> }
>
> It's just an efficiency question that I suppose is not critical in my
> program but I always like to write efficient, maintainable code. Also I'm
> still in the implementation phase, and can't really do timed tests until
> later, so I was hoping a Guru (like yourself, or someone from activestate)
> would have some personal knowledge on the matter.
I almost always tend to use:
$buffer =~ s/^(.*?)MARKER.*$/$1/;
or
$buffer =~ /^(.*?)MARKER/;
$buffer = $1;
I'm not sure without some bench test what sort of penalty this might entail
over other methods, but 99% of the time it's not relevant.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs