Re: block of code doesn't get executed

From: David K. Wall (dwall_at_fastmail.fm)
Date: 03/15/04


Date: Mon, 15 Mar 2004 19:53:06 -0000

stalwart <hammer@thatbox.net> wrote:

> I"m fairly new to perl,

Ditto what Brian McCauley said.

A few things I noticed:

> #!/usr/bin/perl
>
> use Net::FTP ;
> use Date::Parse ;
> $home = "/home/backups/recordings";
>
> # this should go out before the mp3 backup script runs and get a
> listing for it to use to compare file dates
> chdir $home;

Check to see if chdir() succeeded:

    chdir $home or die "Cannot chdir to $home: $!";

Consider what might happen if, for some reason, the chdir() did not
succeed. Might be bad, might not be a problem -- but it should be checked
and appropriate action taken.

[snip]
> print("Getting directory listing of mp3s.....\n");
> %filename = $ftp->ls
> or die "Unable to list the dir!:\n", $ftp->message;

The 'ls' method returns a list, which this code... puts into a hash?
Probably not what you want.

>
> print("Ok, now I need to do some date checking....\n");
>
> foreach($filename) {

$filename has not been used until now, so it's auto-vivified as undef.
Strictures would have caught this.

> s/^_RC//;
> s/_RC^/"\n"/;

A carat (^) in a regex means the beginning of a string. How can '_RC' be
*before* the beginning of the string?

> };
>
> while ($filename) {

$filename is undefined, so this loop never runs.

[snip rest of code]

I get the impression you're confused about Perl data types. %filename,
@filename, and $filename can exist in the same program, but they don't
necessarily have *anything* to do with each other. They are completely
different variables.

-- 
David Wall


Relevant Pages