Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] trying to cp a file

Expand Messages
  • David Precious
    ... Then do so in Perl, in a robust and portable way: my @files_sorted = sort { -M $a -M $b } glob /sds/ibr/waibrs/submit/SPD/00* ; That will give you the
    Message 1 of 14 , Oct 31, 2011
    • 0 Attachment
      On Monday 31 October 2011 18:16:19 Regina Miller wrote:
      > From: Randal L. Schwartz [mailto:merlyn@...]
      > > Why are you using "ls" when Perl is perfectly good at globbing?
      > >
      > > my @names = glob "/sds/ibr/waibrs/submit/SPD/00*";
      > >
      > > And then figure out which name you want.

      > Because I want the latest one, so I need them in order, not by name, but
      > by datetime.

      Then do so in Perl, in a robust and portable way:

      my @files_sorted = sort { -M $a <=> -M $b }
      glob "/sds/ibr/waibrs/submit/SPD/00*";

      That will give you the matching files, sorted by modification time, oldest
      first.

      If you only want the first one, either then say $files_sorted[0] to get it, or
      capture only the first one with:

      my ($oldest_file) = sort { -M $a <=> -M $b }
      glob "/sds/ibr/waibrs/submit/SPD/00*";
    • David Precious
      ... Yup, the newline from the `ls -at1` output was the reason behind the (fairly clear) error message you received when using File::Copy. Whilst you now have a
      Message 2 of 14 , Oct 31, 2011
      • 0 Attachment
        On Monday 31 October 2011 18:23:03 rmillergraysharbor wrote:
        > That was it!
        > I didn't chomp!

        Yup, the newline from the `ls -at1` output was the reason behind the (fairly
        clear) error message you received when using File::Copy.

        Whilst you now have a script doing what you want, for the sake of your future
        code and those who come after you, I would urge you to consider executing
        things like 'cp', 'ls' etc a Bad Thing, often leading to non-portable code
        with potential security vulnerabilities - if you can avoid executing shell
        commands and just do it in Perl, it'll usually be more secure and more
        portable.

        Otherwise, you may as well be writing a shell script :)
      • rmillergraysharbor
        Thank you for the advice. And thanks to David Precious for the sample code that is more Perl-specific. I appreciate it. Regina
        Message 3 of 14 , Oct 31, 2011
        • 0 Attachment
          Thank you for the advice.
          And thanks to David Precious for the sample code that is more Perl-specific.

          I appreciate it.

          Regina
          --- In perl-beginner@yahoogroups.com, David Precious <davidp@...> wrote:
          >
          > On Monday 31 October 2011 18:23:03 rmillergraysharbor wrote:
          > > That was it!
          > > I didn't chomp!
          >
          > Yup, the newline from the `ls -at1` output was the reason behind the (fairly
          > clear) error message you received when using File::Copy.
          >
          > Whilst you now have a script doing what you want, for the sake of your future
          > code and those who come after you, I would urge you to consider executing
          > things like 'cp', 'ls' etc a Bad Thing, often leading to non-portable code
          > with potential security vulnerabilities - if you can avoid executing shell
          > commands and just do it in Perl, it'll usually be more secure and more
          > portable.
          >
          > Otherwise, you may as well be writing a shell script :)
          >
        Your message has been successfully submitted and would be delivered to recipients shortly.