Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > moving a match into a subroutine

Reply
Thread Tools

moving a match into a subroutine

 
 
Stefan Ram
Guest
Posts: n/a
 
      07-16-2009
I want to have the match »something =~ /(.)/g« to be kept in a
separate subroutine, because then, I can keep this at the
start of my script. (I like to collect those parts of my
script that I change often there.)

However, as one can see from the script below, my first attempt
(exhibit 0) does not work, it only prints empty lines.

When I do not use a subroutine (exhibit 1), the behavior is as
wanted: it prints lines with »a«, »b«, and »c«.

Can I put the match »something =~ /(.)/g« in a separate
source code entity that can be moved to the top of my script
(like a subroutine) and still get the behavior of exhibit 1?

#!/usr/bin/perl
#perl 5.8.3

use strict;
use warnings;

my $text = "abc";

# exhibit 0
sub match($){ $_[0] =~ /(.)/g }
while( match( $text ))
{ print $1, "\n"; }

# exhibit 1
while( $text =~ /(.)/g )
{ print $1, "\n"; }

 
Reply With Quote
 
 
 
 
C.DeRykus
Guest
Posts: n/a
 
      07-16-2009
On Jul 16, 4:15*am, (E-Mail Removed)-berlin.de (Stefan Ram) wrote:
> * I want to have the match »something =~ /(.)/g« to be kept in a
> * separate subroutine, because then, I can keep this at the
> * start of my script. (I like to collect those parts of my
> * script that I change often there.)
>
> * However, as one can see from the script below, my first attempt
> * (exhibit 0) does not work, it only prints empty lines.



>
> * When I do not use a subroutine (exhibit 1), the behavior is as
> * wanted: it prints lines with »a«, »b«, and »c«.
>
> * Can I put the match »something =~ /(.)/g« in a separate
> * source code entity that can be moved to the top of my script
> * (like a subroutine) and still get the behavior of exhibit 1?
>
> #!/usr/bin/perl
> #perl 5.8.3
>
> use strict;
> use warnings;
>
> my $text = "abc";
>
> # exhibit 0
> sub match($){ $_[0] =~ /(.)/g }
> while( match( $text ))
> { print $1, "\n"; }



Backreferences are dynamically scoped within the local block
so, in this case, $1 is undefined outside your subroutine
block. You should also see the helpful warning:

Use of uninitialized value $1 in concatenation (.) ...

One way to correct the problem:

sub match { return $_[0] =~ /(.)/g ? $1 : undef; }
while( my $matched = match( $text )){ print "$matched\n"; }

> ...


--
Charles DeRykus
 
Reply With Quote
 
 
 
 
Eric Pozharski
Guest
Posts: n/a
 
      07-17-2009
On 2009-07-16, Stefan Ram <(E-Mail Removed)-berlin.de> wrote:
*SKIP*
> However, as one can see from the script below, my first attempt
> (exhibit 0) does not work, it only prints empty lines.


Did you run it? Besides you didn't copy-paste.

*SKIP*

> #!/usr/bin/perl
> #perl 5.8.3
>
> use strict;
> use warnings;
>
> my $text = "abc";
>
> # exhibit 0
> sub match($){ $_[0] =~ /(.)/g }
> while( match( $text ))
> { print $1, "\n"; }


print "$_\n" for match $text;

That's your fish.

> # exhibit 1
> while( $text =~ /(.)/g )
> { print $1, "\n"; }
>



--
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      07-24-2009
On 16 Jul 2009 11:15:56 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Stefan Ram) wrote:

> I want to have the match »something =~ /(.)/g« to be kept in a
> separate subroutine, because then, I can keep this at the
> start of my script. (I like to collect those parts of my
> script that I change often there.)
>
> However, as one can see from the script below, my first attempt
> (exhibit 0) does not work, it only prints empty lines.
>
> When I do not use a subroutine (exhibit 1), the behavior is as
> wanted: it prints lines with »a«, »b«, and »c«.
>
> Can I put the match »something =~ /(.)/g« in a separate
> source code entity that can be moved to the top of my script
> (like a subroutine) and still get the behavior of exhibit 1?
>
>#!/usr/bin/perl
>#perl 5.8.3
>
>use strict;
>use warnings;
>
>my $text = "abc";
>
># exhibit 0
>sub match($){ $_[0] =~ /(.)/g }
>while( match( $text ))
>{ print $1, "\n"; }
>
># exhibit 1
>while( $text =~ /(.)/g )
>{ print $1, "\n"; }


The other posters pointed out your problem.
Probably thier two solutions can be combined into 1.

------------------------------
use strict;
use warnings;

my $text = "abc";
my $val;

sub match($) {
return $_[0] =~ /./g if wantarray;
$_[0] =~ /(.)/g;
$1; # <-- goes out of scope here, just return its value
}

# Scalar context, multiple match() calls
print "$val\n" while ( $val = match( $text ));

print '-' x 20, "\n"; # pos($text) is reset after failed match (unless \G)

# List context, single match() call
print "$_\n" for ( match( $text ));

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

-sln
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Free Moving Estimate, Local Movers, Long Distance Moving, PackingSupplies, Storage Rental, Home Moving, Apartment Moving, Office Moving,Commercial Moving linkswanted ASP .Net 0 01-06-2008 04:45 AM
pat-match.lisp or extend-match.lisp in Python? ekzept Python 0 08-10-2007 06:08 PM
use one subroutine's variable value in another subroutine inside a module. king Perl Misc 5 04-29-2007 06:39 AM
$match = true() for empty $match?? Victor XML 2 05-17-2004 10:43 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments