Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

IO::Dir confusing behaviour

by DaWolf (Curate)
on Nov 21, 2001 at 18:43 UTC ( [id://126769]=perlquestion: print w/replies, xml ) Need Help??

DaWolf has asked for the wisdom of the Perl Monks concerning the following question:

Hey fellas.

I'm using IO::Dir in a script. That script should simply show the files in a defined dir or if there aren't any, show a message telling the user.

So, I did it this way:
$file_folder = "foo/foo2/"; tie %arquivos, IO::Dir, $file folder; if (scalar ( keys %arquivos) ne 0) { foreach (keys %arquivos) { if ($_ ne "." and $_ ne "..") { <a href=\"$file_folder$_\">$_</a>\n"; } } } else { print "There are no files\n"; }
Well, how can this same code works in one kind of situation and don't work on other kind?

When it works, it displays the "No files" message, but when it doesn't it just doesn't print anything.

I would specify the situations here, but it is the same script, with the same empty directory, so why this is happenning?

I found out that when it doesn't works is because of the . and the .. "entries" in NT, but then again it should never work, because NT always create this two "entries", right? And it's looking on the same dir anyways, so it just doesn't make any sense.

Please help me here, fellas, because I just can't see any logic reason for this behaviour.

Er Galvão Abbott
a.k.a. Lobo, DaWolf
Webdeveloper

Replies are listed 'Best First'.
Re: IO::Dir confusing behaviour
by MZSanford (Curate) on Nov 21, 2001 at 19:02 UTC
    hmmm, based on the HTML in the post, i am prompted to ask the following question :

    Is this executing in a web browser ?

       Yes : While this is just a snippet, make sure the header is getting printed. Without it, it is possible standard prints will show up, but not the links.

       No : your tie command is missing the _ in $file_folder. Assumably a copy/paste error, but maybe not ?

    Just a guess, but not sure, never really used IO::Dir
    i had a memory leak once, and it ruined my favorite shirt.
Re: IO::Dir confusing behaviour
by IOrdy (Friar) on Nov 21, 2001 at 19:07 UTC
    If you cut and paste this script in then I think you are missing an underscore in your tie statement. '$file folder;' should be $file_folder;

    If you had strict on I think it would have picked up the bare word.

    As always I could be wrong, I dont even know what tie does :)
      Thanks, but actually I've changed the scalar name. It's a habit that I've got to make my code clear enough for others to read.

      In the original code the scalar variable don't even have an underscore. : )

      Er Galvão Abbott
      a.k.a. Lobo, DaWolf
      Webdeveloper
(tye)Re: IO::Dir confusing behaviour
by tye (Sage) on Nov 21, 2001 at 22:48 UTC

    I would think that the "works" cases are actually when the tie fails. You should check for that case:

    if( ! tie %arquivos, IO::Dir, $folder ) { print "Can't read directory ($folder): $!\n"; } else { # ... }

            - tye (but my friends call me "Tye")
      Hey tye.

      A good guess, but if the tie fails then it shouldn't show me any files in the directory. If I select some files to upload, the upload is done and the script show them correctly.

      Er Galvão Abbott
      a.k.a. Lobo, DaWolf
      Webdeveloper
Re: IO::Dir confusing behaviour
by mce (Curate) on Nov 21, 2001 at 20:42 UTC
    Hi there,

    Just my 0.02$ worth, but I see you define your directory $file_folder using a relative path.

    So, when you launch this script in a directory where there is no foo/foo2 subdir, you'll get this error. And certainly when you run if from apache, you will look for the directory where you start your httpd daemons, which might be / or /usr/local/apache depending on your config.
    ---------------------------
    Dr. Mark Ceulemans
    Senior Consultant
    IT Masters, Belgium

Re: IO::Dir confusing behaviour
by Amoe (Friar) on Nov 21, 2001 at 21:29 UTC
    Um, are you actually printing the HTML you define in the loops? And it seems to be wrongly quoted...perhaps something like this:
    if (scalar ( keys %arquivos) ne 0) { foreach (keys %arquivos) { if ($_ ne "." && $_ ne "..") { print qq{<a href="$file_folder$_\">$_</a>\n}; } } } else { print "These aren't the files you're looking for. Move along. +\n"; }
    Otherwise, could you please explain the line I corrected?

    --
    my one true love
      Hmmm sorry 'bout that.

      That's the problem with changing the code to post a question.

      Actually I'm putting that html snip in a variable like:
      $html_code .= "<a href=\"$file_folder$_\">$_</a>";
      Er Galvão Abbott
      a.k.a. Lobo, DaWolf
      Webdeveloper
Re: IO::Dir confusing behaviour
by DaWolf (Curate) on Nov 21, 2001 at 22:24 UTC
    Well, poeple, nice tips from everyone, but I'm still stucked here.

    The worst thing is althrough I know that it IS the same code and the same dir, it looks like I'm making a very stupid mistake, wich I often do...

    Any thoughts - not about me being stupid but about the code itself :) - would be appreciated.

    Er Galvão Abbott
    a.k.a. Lobo, DaWolf
    Webdeveloper

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://126769]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-19 16:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found