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

Hi you monks out there,

I wrote the next code:
COPY: $J="Just a stupid command"; use File::Copy; copy("$Line[0]","$Line[1]") or die "Copy failed: $!";

It works fine.
Now, one would say that the stupid command
is not necessary, the fact is that if I delete
the stupid command I get an error (copilation) message.

What say you???

Moked

Replies are listed 'Best First'.
Re: File::Copy a bug?
by japhy (Canon) on Aug 18, 2005 at 13:27 UTC
    You cannot put a label on a 'use' statement (or any BEGIN block, and likewise, any subroutine definition.)

    FIN


    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: File::Copy a bug?
by Roger (Parson) on Aug 18, 2005 at 13:32 UTC
    COPY: is a label. It puts a "goto" point before the next statement, so that when you do goto COPY your code will resume at $J=....

    If you get rid of the $J=... line, the next line, use File::Copy is actually not a run time statement you can place a label on. It is executed at compile time. Therefore you will get a compilation error. If you move the use File::Copy to the top of the Perl script, it will resolve the compilation error.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: File::Copy a bug?
by Corion (Patriarch) on Aug 18, 2005 at 13:23 UTC

    I say: Show us the program, and the error message. Perl tries to tell you something. Listen to it.

Re: File::Copy a bug?
by davidrw (Prior) on Aug 18, 2005 at 13:33 UTC
    From perldoc perlsyn it says that the LABEL goes with a block, and is just like creating a single-iteration loop. So if you think of the COPY: and 'stupid' line as $J="blah" for 1; you can see why COPY: immediately followed by the use statement wouldn't work. You can solve this by using curly brackets, but a better solution would be to pull the use File::Copy; statement towards the top of your file. It is run at compile-time anyways, so having it in the block (which may or may not be run) doesn't gain you anything.
Re: File::Copy a bug?
by NateTut (Deacon) on Aug 18, 2005 at 15:59 UTC
    Might I also suggest the use of
    use strict; use warnings;
    and maybe even

    use diagnostics;

    These will help you avoid many problems.
      <rant>
      I really hate the stupid suggestions of adding 'strict' and 'warnings' if they do not solve the problem presented.

      Problems should be presented with as minimal code as necessary to show the problem. The OP did that. Slapping the three lines you suggested on to it would just make the code larger, drawing away the attention from the problem. Don't scold at the OP for doing the right thing.

      And would have gained nothing - except to stop mindless responses like yours.

      Yeah, yeah, someone will say, but it's good advice anyway. Sure it is. And so are the other 255 points from Damians newest book. Should we post a list of 256 good advices on every problem posted here?
      </rant>

        I said it because he seemed like a newbie and I thought he could use the advice. Also he probably could have figured it out on his own if he had used strict et al.

        Also why rant anonomyously? Be proud of your rants, take credit for them.