Script dying at opendir()

Discussion in 'Perl Misc' started by geek, Apr 27, 2005.

  1. geek

    geek Guest

    Hi all,

    My script is dying when I try to open the current dir( at the line
    opendir(DIR, "$dir").
    This is the code.
    ==================================
    my $dir = `pwd`;
    my ($filename,$mydate);
    print "HELLO";
    opendir(DIR,"$dir") or die ("$0:can't opendir public_html: $!");
    print "HELLO";
    while (defined($filename = readdir(DIR))) {
    print "Hello";
    if ($filename =~ /\.date$/){
    ======================================
    Any help will be great.

    Thanks,
    MJ
     
    geek, Apr 27, 2005
    #1
    1. Advertising

  2. geek

    Guest

    geek wrote:
    >
    > My script is dying when I try to open the current dir
    > ( at the line opendir(DIR, "$dir").
    > This is the code.
    > ==================================
    > my $dir = `pwd`;
    > my ($filename,$mydate);
    > print "HELLO";
    > opendir(DIR,"$dir") or die ("$0:can't opendir public_html: $!");
    > print "HELLO";
    > while (defined($filename = readdir(DIR))) {
    > print "Hello";
    > if ($filename =~ /\.date$/){
    > ======================================
    > Any help will be great.



    Try inserting the line:

    chomp($dir);

    right after you assign $dir the return value of `pwd`.

    -- Jean-Luc
     
    , Apr 27, 2005
    #2
    1. Advertising

  3. geek

    geek Guest

    Thank you so much !! It works.
     
    geek, Apr 27, 2005
    #3
  4. "geek" <> wrote in news:1114616899.049589.37860
    @l41g2000cwc.googlegroups.com:

    > My script is dying when I try to open the current dir( at the line
    > opendir(DIR, "$dir").
    > This is the code.
    > ==================================
    > my $dir = `pwd`;


    You don't really need to spawn an external process to get the current
    directory, you know.

    > my ($filename,$mydate);
    > print "HELLO";
    > opendir(DIR,"$dir") or die ("$0:can't opendir public_html: $!");


    The die message is very misleading. You would have found out your error
    if instead you had used:

    opendir my $dir_h, $dir
    or die "Can't opendir $dir: $!";

    (Note that I removed the useless quotation marks around $dir in the
    opendir call -- see perldoc -q always).

    On the other hand, if all you want is to read the entries in the current
    directory, you could use:

    opendir my $dir_h, '.'
    or die "Can't open current directory: $!";

    Sinan.

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 27, 2005
    #4
  5. geek wrote:

    > Hi all,
    >
    > My script is dying when I try to open the current dir( at the line
    > opendir(DIR, "$dir").
    > This is the code.
    > ==================================
    > my $dir = `pwd`;


    pwd returns an newline at the end...

    > my ($filename,$mydate);
    > print "HELLO";
    > opendir(DIR,"$dir") or die ("$0:can't opendir public_html: $!");


    ....which you never chomped. The opendir fails and the die kills the
    script, because the directory you tried to open doesn't exist. The die
    should have told you this.

    > print "HELLO";
    > while (defined($filename = readdir(DIR))) {
    > print "Hello";
    > if ($filename =~ /\.date$/){
    > ======================================
    > Any help will be great.
    >
    > Thanks,
    > MJ


    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
     
    Chris Mattern, Apr 27, 2005
    #5
  6. geek

    Tintin Guest

    "geek" <> wrote in message
    news:...
    > Hi all,
    >
    > My script is dying when I try to open the current dir( at the line
    > opendir(DIR, "$dir").
    > This is the code.
    > ==================================
    > my $dir = `pwd`;
    > my ($filename,$mydate);
    > print "HELLO";
    > opendir(DIR,"$dir") or die ("$0:can't opendir public_html: $!");
    > print "HELLO";
    > while (defined($filename = readdir(DIR))) {
    > print "Hello";
    > if ($filename =~ /\.date$/){
    > ======================================
    > Any help will be great.


    You already have answers as to why it doesn't work. I would write it as:

    foreach my $file (<*.date>) {
    ..
    }
     
    Tintin, Apr 27, 2005
    #6
  7. Tintin <> wrote:

    > You already have answers as to why it doesn't work. I would write it as:
    >
    > foreach my $file (<*.date>) {



    I would write it as:

    foreach my $file (glob '*.date') {


    Because I don't want to have to pause everytime I see <> in my code
    in order to figure out if it is readline() or glob().

    <> is always input in my programs, so there's no need to slow
    down when reading it.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 27, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew

    opendir()

    Andrew, Sep 3, 2003, in forum: Perl
    Replies:
    2
    Views:
    9,479
    John Bokma
    Sep 3, 2003
  2. Bob Gervais

    opendir and if -d $files problem

    Bob Gervais, Nov 7, 2003, in forum: Perl
    Replies:
    1
    Views:
    1,651
    Steve Grazzini
    Nov 12, 2003
  3. Alex
    Replies:
    2
    Views:
    700
  4. Ramon
    Replies:
    5
    Views:
    10,843
    John C. Bollinger
    Jan 3, 2005
  5. Markus Pitha

    opendir doesn't work..

    Markus Pitha, Sep 12, 2004, in forum: C Programming
    Replies:
    5
    Views:
    473
    Markus Pitha
    Sep 12, 2004
Loading...

Share This Page