in reply to Turning foreach into map?

Once you decide to use map, you probably don't need the subroutine anymore: you're not hiding that much code. Previously, you had a subroutine call like this:

my @munged = links( $url, @links );

To replace links() with an inline map() is only a few characters longer, and it tells the programmer exactly what is happening.

my @munged = map { "$url/$_" } @links;

However, if link() does a lot of other stuff you aren't showing us, then you may still want to use it to hide things.

As a bonus, the URI module can turn relative URLs into absolute ones for you. :)

--
brian d foy <brian@stonehenge.com>

Replies are listed 'Best First'.
Re^2: Turning foreach into map?
by ghenry (Vicar) on Apr 04, 2005 at 22:21 UTC

    As usual, I have learned a lot from your replies. Thanks.

    dragonchild, I never got a chance to test it, only post this question, as I had my son on my lap ;-) He's now in bed, so I can reply.

    The reason I posted this, is because I am going back to my old programs after learning more, and trying to make them clean and have less code.

    The program in question was written for my wife, as she always asks me how to resize images and upload them to ebay. I wrote this, called ebaypics, which is then saved in ~/.gnome2/nautilus-scripts she then right clicks in the folder she has the photos in, and that's it.

    #!/usr/bin/perl use strict; use warnings; use Net::FTP; use Mail::Sendmail; use Image::Magick; use Term::ProgressBar; ################################################# # program: ebaypics # # license: GPL # # author: Gavin Henry # # # # version: v0.1 # # # # first draft : 22-09-04 # # last update : 27-09-04 # ################################################# my @images = <*.jpg>; my $resize = '640x480'; my ($image,$process); my $ftpsite = 'ftp.somewhere.net'; my $website = 'http://www.somewhere.com/ebay'; my $remotedir = '/htdocs/ebay'; my $username = 'user'; my $password = 'pass'; my $to = '"My name" <my.name@me.co.uk>'; my $from = '"ebaypics" <ebaypics@me.com>'; my $sep = '-' x 76 . "\n"; my $time = localtime; my $progress = Term::ProgressBar->new({name => 'Scaling images', count => scalar @images, ETA => 'linear'}); # Messages print "\n", $sep, "Starting....\n", $sep; &resize; my @thumbs = <thumbnail-*.jpg>; # Get pictures and upload my $ftp = Net::FTP->new("$ftpsite", Timeout => 30) or die "Sorry I can't connect: $@\n"; $ftp->login($username, $password) or die "Wrong password. Please call Gavin.\n"; $ftp->cwd("$remotedir"); foreach (@thumbs) { $ftp->put($_) or die "Can't find the resized pictures: $!\n"; } $ftp->quit() or warn "Couldn't quit. Damn.\n"; my @email_links = links($website, @thumbs); my %mails = ( To => "$to", From => "$from", Subject => "Here are the links to your pictures", Message => "Click on any of the links below to check the pictures" + . " are correct, then copy and paste them into your" . " ebay listing.\n\n" . " Weblinks:\n" . " @email_links\n\n" ); sendmail(%mails); print "\n", $sep, "Resizing of images complete at $time. E-mail sent w +ith website links.\n", $sep; # Subroutines sub resize { print "\n", $sep, "Resizing images.....this may take some time.... +.\n", $sep; foreach (@images) { $image = Image::Magick->new(); $process = $image->Read("$_"); $process = $image->Resize(geometry => "$resize"); warn "$process" if "$process"; $process = $image->Write("thumbnail-$_"); warn "$process" if "$process"; $progress->update(); } print "\n", $sep, "Resizing complete, starting upload to webspace. +....\n", $sep; } # Generate weblinks sub links { my ($url, @list) = @_; my @return; foreach (@list) { push @return, "$url/$_\n"; } return @return; }

    Needs a bit of work ;-)

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!