tags:

views:

170

answers:

2

I'm working my way up towards creating a script that will create image galleries for me.

When I run what I have it tells me

No such file or directory at photographycreate line 16.
------------
(program exited with code: 2)

Here is the code that I've gotten so far.

#!/etc/perl -w

#CHANGE THIS
$filecategory = "cooking";

$filenumber = 0;
#$filename = "photography";
$imagedirectory = "\"/media/New Volume/Programming/kai product/media/photography/".$filecategory."/images/\"";
$galleryfile = "\"/media/New Volume/Programming/kai product/pages/".$filenumber."_".$filecategory."_gallery.html\"";

@imagelocation = <$imagedirectory/*>; #*/
$filecount = @imagelocation;

while($filenumber < 3) {     
    open GALLERY, "+>", $galleryfile or die $!;
    print GALLERY ($filecount."\n");
    print GALLERY ($imagedirectory."\n");
    print GALLERY ($galleryfile."\n");
    close GALLERY;

    ++$filenumber;
}

What I want it to do is create the file, open it, write stuff to it, and then close/save it. How can I, using what I have, do this?

Here is the fix:

#!/etc/perl -w
use Fcntl; #The Module
use strict;

#CHANGE THIS
my $filecategory = "cooking";

my $filenumber = 0;
my $imagedirectory = "\"/media/New Volume/Programming/kaiproduct/media/photography/".$filecategory."/images/\"";
my $galleryfile = "/media/New Volume/Programming/kaiproduct/pages/".$filenumber."_".$filecategory."_gallery.html";

my @imagelocation = <$imagedirectory/*>; #*/
my $filecount = @imagelocation;

while($filenumber < 3)
    {
        open GALLERY, "+>", $galleryfile or die $!;
        print (GALLERY $filecount."\n");
        print (GALLERY $imagedirectory."\n");
        print (GALLERY $galleryfile."\n");
        close GALLERY;

        ++$filenumber;
    }
+7  A: 

I think the problem is here:

$imagedirectory = "\"/media/New Volume/Programming/kai product/media/photography/".$filecategory."/images/\"";
$galleryfile    = "\"/media/New Volume/Programming/kai product/pages/".$filenumber."_".$filecategory."_gallery.html\"";

Specifically, each of these strings starts with "\" and ends with \"", which means your files and folders will be surrounded with double quotes. So Perl isn't trying to open /media/New Volume/etc..., but "/media/New Volume/etc...", which doesn't exist because there is no directory called ". You're over-quoting.

One thing you can (and should always) do to make your code better is to use strict;. I see you already have use warnings; at the top there, which is good, but using both strict and warnings will make your code a lot safer and nicer to look at.

Chris Lutz
Yep - you got it - nice one! ;)
ire_and_curses
Thank you for your help.
aggitan
Be sure to fix both variables, not just one. I think.
Chris Lutz
+2  A: 

IMNHO, the real answer is to use File::Spec and write things a bit clearer:

use File::Spec::Functions qw( catfile );

# ...

my $root = "/media/New Volume/Programming/kai product";

my $imagedirectory = catfile($root,
    'photography',
    $filecategory,
    'images',
);

my $galleryfile = catfile($root,
    'pages',
    "${filenumber}_${filecategory}_gallery.html",
);

In addition, it is a good idea to observe good habits, especially since you are just learning Perl:

Always put:

use strict;
use warnings;

as the first thing in your program.

Use lexical filehandles rather than bareword filehandles (which are package global):

open my $gallery, '+>', $galleryfile 
    or die "Cannot open '$galleryfile': $!;

and include the name of the file in the error message.

Finally, I like File::Slurp's append_file:

append_file $gallery_file, [ 
    map { "$_\n" } ( $filecount, $imagedirectory, $galleryfile )
];

Here is a revised version of your program:

#!/etc/perl

use strict;
use warnings;

use File::Spec::Functions qw( catfile );

my $root = "/media/New Volume/Programming/kai product";
my $filecategory = "cooking";

my $imagedirectory = catfile($root,
    'photography',
    $filecategory,
    'images',
);

my @imagelocation = read_dir $imagedirectory;

for my $filenumber ( 0 .. 2 ) { 

    my $galleryfile = catfile($root, 'pages',
        "${filenumber}_${filecategory}_gallery.html",
    );

    append_file $gallery_file, [ 
        map { "$_\n" } ( 
            scalar @imagelocation, $imagedirectory, $galleryfile,
        )];
}
Sinan Ünür