Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > RE: help in whi...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 10744 of 11948
Post > Topic >>

RE: help in while loop

by t.baetzler@[EMAIL PROTECTED] (T Baetzler) Mar 17, 2008 at 01:49 PM

Irfan.Sayed@[EMAIL PROTECTED]
 asked

> Can somebody please let me know the meaning of this line.
>=20
> =20
>=20
> while (<$in>)
>=20
> {
> if(/,/) {print "before match: $`\t and after match:=20
> $'\n\n";}; $x=3D$'; $y=3D$`; &mysubroutine($x,$y);
> }

The loop iterates over a filehandle, setting $_ to each
line in turn. If that line contains a comma, then the=20
line is split at the comma and mysubroutine() is being
called with the parts before and after the comma as
arguments.

My first try at tidying this up would be:

while( my $line =3D <$in> )
{
  if( my( $pre, $post ) =3D ( $line =3D~ m/^(.*?),(.*)/ ) ){
    print "before match: $pre\t and after match: $post\n\n";
    mysubroutine($pre,$post)
  }
}

I'm assuming that mysubroutine is only being called if there
actually was a match. Your code above would erroneously call
it for each line.

Using the pre/postmatch captures $' and $` is expensive if
you don't need it all the time (cf. perldoc perlre):

 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.

Using &mysubroutine with arguments would only make sense if
mysubroutine was defined with prototypes and you were trying
to disable that. I'm assuming that in this case it's rather
cargo cult programming.

HTH,
Thomas
 




 1 Posts in Topic:
RE: help in while loop
t.baetzler@[EMAIL PROTECT  2008-03-17 13:49:57 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 7 16:11:11 CDT 2008.