develooper Front page | perl.beginners | Postings from March 2002

Re: Using strict and getting return values

Thread Previous
Jonathan E. Paton
March 4, 2002 09:58
Re: Using strict and getting return values
Message ID:
 --- Dermot Paikkos <> wrote:

> Hi Gurus,
> I am trying to get tidy with my scripts and want to
> use Strict

Use lowercase for strict!  Anything else will cause havoc.

> but am having difficulty with return values from
> subroutines.  I have the following snippet:
>   while (defined(my $i =<$fh>)) {

Don't bother with $i... lot neater if you use the
implict $_ - hence your code:

>     chomp($i);
>     my @a = split(/|/,$i);
>     my $last = $a[1];
>     my $first = $a[0];
>     if ( $name =~ /$last/i ) {
>           return($first,$last);
>     }


    while (<$fh>) {
         my ($first, $last) = split /|/;
         return ($first, $last)
             if $name eq lc $last;

with the following benefits:

1. Shorter/cleaner.
2. Doesn't bother with an needless array
3. lc() and eq is better than using =~ and /i since
   $last might contain a dodgy regex that hacks your
4. It looks more Perlish :)

anyway, I haven't tested it so complain if something
doesn't work - it'll be some minor syntax to fix.
You can probably shorten chomp($_) - but strange things
can happen that I'd rather test first.

>  and get the error:
> Global symbol "$last" requires explicit package name at 
> /var/www/perl/ line 16.
> When I later:  print "Hello $first $last\n";

my ($first, $last) = function();

Take care,

Jonathan Paton

Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About