help to understand a broken script

Discussion in 'Perl Misc' started by Chad, Oct 23, 2003.

  1. Chad

    Chad Guest

    I am still trying to learn perl. I know there are basic rename scripts
    out there, but I am trying to make one as a learning tool.

    The script should take a directory from the prompt and make a renamed
    copy in the same directory replacing spaces with underscores. It seems
    to do everything except the actual file copy.

    It's not that I need this program. Instead I would be gratefull for
    any insight and undestanding as to why it does not work.

    Thank you
    Chad
    ---

    #!/usr/bin/perl
    use File::Copy;

    $output = `cmd 2>&1`; #trying to see errors


    $olddirname = $ARGV[0];
    $newdirname = "$olddirname.new";

    opendir(DirNameold,"$olddirname") or die "could not open
    directory:$olddirname";

    while (defined($file = readdir DirNameold )) {

    $newfilename = $file;
    $newfilename =~ tr/ /_/;
    copy("$file","$newfilename") unless $file eq $newfilename;

    };

    ---
     
    Chad, Oct 23, 2003
    #1
    1. Advertising

  2. Chad wrote:
    > I am still trying to learn perl. I know there are basic rename scripts
    > out there, but I am trying to make one as a learning tool.
    >
    > The script should take a directory from the prompt and make a renamed
    > copy in the same directory replacing spaces with underscores. It seems
    > to do everything except the actual file copy.
    >
    > It's not that I need this program. Instead I would be gratefull for
    > any insight and undestanding as to why it does not work.
    >
    > Thank you
    > Chad
    > ---
    >
    > #!/usr/bin/perl


    You should always ask for as much help from perl as possible.
    Right at the beginning you are missing a

    use strict;
    use warnings;

    > use File::Copy;


    Good!

    [...]
    > copy("$file","$newfilename") unless $file eq $newfilename;


    And here you are missing the fail condition.
    Besides, that is a useless use of quotes.

    {copy($file, $newfilename) or die "Can't copy $file to $newfilename
    because $!"}
    unless $file eq $newfilename;

    I didn't analyse you program any further because it is humiliating to ask a
    human to do work that a machine can do better and faster.
    The actual error message should give you a hint about what is wrong.

    jue
     
    Jürgen Exner, Oct 23, 2003
    #2
    1. Advertising

  3. Chad

    JSV Guest

    "Chad" <> wrote in message
    news:...
    > I am still trying to learn perl. I know there are basic rename scripts
    > out there, but I am trying to make one as a learning tool.
    >

    <snip>

    > It's not that I need this program. Instead I would be gratefull for
    > any insight and undestanding as to why it does not work.
    >
    >

    <snip>
    > copy("$file","$newfilename") unless $file eq $newfilename;


    I guess, you are executing this copy command, when not present inside the
    directory passed in $ARGV[0]. Also trap error by using die "$!" after the
    copy.
     
    JSV, Oct 23, 2003
    #3
  4. Also sprach Jürgen Exner:

    > Chad wrote:


    >> copy("$file","$newfilename") unless $file eq $newfilename;

    >
    > And here you are missing the fail condition.
    > Besides, that is a useless use of quotes.
    >
    > {copy($file, $newfilename) or die "Can't copy $file to $newfilename
    > because $!"}
    > unless $file eq $newfilename;


    That's a syntax error. You can't (yet) use statement modifiers on bare
    blocks. You don't even need a block. Simply

    copy($file, $newfilename)
    or die "Can't copy $file to $newfilename because $!"
    unless $file eq $newfilename;

    will do.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Oct 23, 2003
    #4
  5. Chad <> wrote:

    > I would be gratefull for
    > any insight and undestanding as to why it does not work.



    Reading the documentation for the functions that you use often
    provides insights. :)


    > opendir(DirNameold,"$olddirname") or die "could not open

    ^ ^
    ^ ^
    > directory:$olddirname";



    What's wrong with always quoting "$vars"?

    If it fails, wouldn't you want to know _why_ it failed?

    If so, then include $! in your diagnostic message.


    > while (defined($file = readdir DirNameold )) {



    perldoc -f readdir

    ... you'd better prepend the directory in question


    > copy("$file","$newfilename") unless $file eq $newfilename;



    copy("$olddirname/$file", "$newdirname/$newfilename") ...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 23, 2003
    #5
  6. Chad

    Chad Guest

    Thanks to everyone who contributed. it works now.
    all the advice made the difference. The code is still ugly, but it
    works and I can work on more elegant code as I progress.

    Once again
    Thank you

    Chad
     
    Chad, Oct 25, 2003
    #6
    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. Leo Smith
    Replies:
    5
    Views:
    616
    Oliver Wong
    May 26, 2006
  2. Steven D'Aprano

    Why are "broken iterators" broken?

    Steven D'Aprano, Sep 21, 2008, in forum: Python
    Replies:
    8
    Views:
    662
  3. Cameron Simpson

    Re: Why are "broken iterators" broken?

    Cameron Simpson, Sep 22, 2008, in forum: Python
    Replies:
    0
    Views:
    592
    Cameron Simpson
    Sep 22, 2008
  4. Fredrik Lundh

    Re: Why are "broken iterators" broken?

    Fredrik Lundh, Sep 22, 2008, in forum: Python
    Replies:
    0
    Views:
    608
    Fredrik Lundh
    Sep 22, 2008
  5. pdc
    Replies:
    1
    Views:
    88
Loading...

Share This Page