Re: PHP to perl - recursion limitation?
You could/can always run php from the command-line (including panther.) php -f scriptname.php Output is obviously to stdout rather than through the Apache server, but it works (and it handy to initiate php scripts from crontabs that do useful work. Matthew http://www.redmac.ca - Getting Canadian's their Macintosh accessories http://www.justaddanoccasion.com - Great gift ideas, featuring smoked salmon On Feb 10, 2004, at 4:34 AM, kynan wrote: Hi all, I'm converting a PHP script to perl so I can run it on the command line in Panther. The following sub routine is a cut down example of what one I'm using. It gets called once, passing the name of a directory. It is meant to recursively call itself for each directory it finds, so it work it's way down the 'branches' of the 'tree'. If it finds a directory any called ekstro it does some stuff. In PHP it worked because it would be looping over one directory, find a directory and start looping over that. When it had gone through all the contents of that directory it would continue looping through the first one. This would just keep happening - directory within directory within directory etc. My problem is that the perl script seems to work in a different way. It looks through the contents of one directory and, when it finds a directory it begins looping through the contents of that one. But when it's finished with that one the it doesn't continue looping through the first one again - ie. it goes down only one branch of the 'tree'. Is this a limitation? Does perl work inherently differently to PHP in this respect? Do I have to forget about using perl? sub replaceEkstros{ my $startDir = shift; # make an object out of the directory opendir DIR,$startDir || die $!; # while there is something in the directory to read, read it. while (my $entry = readdir(DIR)) { if(-d $startDir/$entry $entry =~ /^ekstro$/){ #do some stuff }elsif(-d $startDir/$entry){ # call itself replaceEkstros($startDir/$entry); } } } + Kynan Hughes phone 9281 2088 fax 9211 4433 mobile 0411 231099 Additive design pty ltd Amitabha pty ltd http://www.additive.net.au Level 4, 104 Commonwealth St Surry Hills NSW 2010 Australia +
Re: PHP to perl - recursion limitation?
Kynan- You should consider using the perl module File::Find to solve this problem. See: http://search.cpan.org/~nwclark/perl-5.8.3/lib/File/Find.pm It's a built-in module, so there's nothing to install. Just a few lines of code and you're done. -Bill Hi all, I'm converting a PHP script to perl so I can run it on the command line in Panther. The following sub routine is a cut down example of what one I'm using. It gets called once, passing the name of a directory. It is meant to recursively call itself for each directory it finds, so it work it's way down the 'branches' of the 'tree'. If it finds a directory any called ekstro it does some stuff. In PHP it worked because it would be looping over one directory, find a directory and start looping over that. When it had gone through all the contents of that directory it would continue looping through the first one. This would just keep happening - directory within directory within directory etc. My problem is that the perl script seems to work in a different way. It looks through the contents of one directory and, when it finds a directory it begins looping through the contents of that one. But when it's finished with that one the it doesn't continue looping through the first one again - ie. it goes down only one branch of the 'tree'. Is this a limitation? Does perl work inherently differently to PHP in this respect? Do I have to forget about using perl? sub replaceEkstros{ my $startDir = shift; # make an object out of the directory opendir DIR,$startDir || die $!; # while there is something in the directory to read, read it. while (my $entry = readdir(DIR)) { if(-d $startDir/$entry $entry =~ /^ekstro$/){ #do some stuff }elsif(-d $startDir/$entry){ # call itself replaceEkstros($startDir/$entry); } } } + Kynan Hughes phone 9281 2088 fax 9211 4433 mobile 0411 231099 Additive design pty ltd Amitabha pty ltd http://www.additive.net.au Level 4, 104 Commonwealth St Surry Hills NSW 2010 Australia +
Re: PHP to perl - recursion limitation?
On Feb 10, 2004, at 7:34 AM, kynan wrote: Is this a limitation? No, it's a bug in your code. :-( As Bill mentioned, the File::Find module is made for recursing through directories. It still might be useful for you to know what's going wrong, though. sub replaceEkstros{ my $startDir = shift; # make an object out of the directory opendir DIR,$startDir || die $!; Filehandles and dirhandles are global, so each time this sub is called, DIR is closed and re-opened on a new directory. To avoid that, pass a scalar to opendir() instead of a literal name. When a scalar is passed as the first parameter to open() or opendir(), it is used as the name of the filehandle/dirhandle to open. Likewise with readdir(), , read() and other functions that take filehandle/dirhandle parameters. my $dh = $startDir; $dh =~ s/\W/_/g; # Transform all non-alphanumerics to _ opendir $dh, $startDir || die $!; while (my $entry = readdir($dh)) { ... and so on. sherm--
Re: PHP to perl - recursion limitation?
Thanks Sherm, Mike and Bill, Totally helpful and much appreciated. K On 11/02/2004, at 1:27 AM, Sherm Pendley wrote: On Feb 10, 2004, at 7:34 AM, kynan wrote: Is this a limitation? No, it's a bug in your code. :-( As Bill mentioned, the File::Find module is made for recursing through directories. It still might be useful for you to know what's going wrong, though. sub replaceEkstros{ my $startDir = shift; # make an object out of the directory opendir DIR,$startDir || die $!; Filehandles and dirhandles are global, so each time this sub is called, DIR is closed and re-opened on a new directory. To avoid that, pass a scalar to opendir() instead of a literal name. When a scalar is passed as the first parameter to open() or opendir(), it is used as the name of the filehandle/dirhandle to open. Likewise with readdir(), , read() and other functions that take filehandle/dirhandle parameters. my $dh = $startDir; $dh =~ s/\W/_/g; # Transform all non-alphanumerics to _ opendir $dh, $startDir || die $!; while (my $entry = readdir($dh)) { ... and so on. sherm-- + Kynan Hughes phone 9281 2088 fax 9211 4433 mobile 0411 231099 Additive design pty ltd Amitabha pty ltd http://www.additive.net.au Level 4, 104 Commonwealth St Surry Hills NSW 2010 Australia +
Re: PHP to perl - recursion limitation?
On Feb 10, 2004, at 5:45 PM, Peter N Lewis wrote: my $dh; opendir $dh, $startDir || die $!; while (my $entry = readdir($dh)) { Or consider using DirHandle: use DirHandle; my $dh = DirHandle-new($startDir) or die $!; while (defined(my $entry = $dh-read())) { ... Edward Moy [EMAIL PROTECTED] --- (This messages is from me as a reader of this list, and not a statement from Apple.)