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

question on glob behavior

Expand Messages
  • Schelske, Steve K.
    Active Perl/Win XP I ran into what I thought was strange behavior with a glob and was hoping for an explanation. This first glob does what I expect - it
    Message 1 of 4 , Jul 3, 2007
    • 0 Attachment
      Active Perl/Win XP

      I ran into what I thought was strange behavior with a glob and was
      hoping for an explanation.

      This first glob does what I expect - it populates the array with all of
      the files in the globtest directory that start with test.

      @files = glob('c:/temp/globtest/test*');
      print "@files\n";

      c:/temp/globtest/test .txt c:/temp/globtest/test 20070703082107.txt
      c:/temp/glob
      test/test 20070703082256.txt c:/temp/globtest/test20070703081959.txt
      c:/temp/glo
      btest/test20070703082010.txt

      When I put a space at the end of the string in this glob, @files gets
      populated with the one file in globtest that matches (but not the file
      file name, and not all of the files that match), but also with every
      file in the current directory.

      @files = glob('c:/temp/globtest/test *');
      print "@files\n";

      c:/temp/globtest/test acg_batch.pl acg_batch.pl.bak perl_env.pl
      systest.pl test.
      pl test.pl.bak test_ora.pl testarg.txt vpn.pl x.txt xrfwin.pl
      xrfwin.pl.bak

      Can someone explain why this is happening? Is it really what I've asked
      glob to do?



      [Non-text portions of this message have been removed]
    • John Francini
      Sorry, that won t work. According to the Camel Book (Programming Perl): If you want to glob filenames that might contain whitespace, you ll need to use the
      Message 2 of 4 , Jul 3, 2007
      • 0 Attachment
        Sorry, that won't work.

        According to the Camel Book (Programming Perl):

        "If you want to glob filenames that might contain whitespace, you'll
        need to use the File::Glob module directly, since glob grandfathers
        the use of whitespace to separate multiple patterns such as <*.c
        *.h>. For details, see File::Glob in Chapter 32, "Standard Modules".

        "When you call open, Perl does not expand wildcards, including
        tildes. You need to glob the result first.

        open(MAILRC, "~/.mailrc") # WRONG: tilde is a shell thing
        or die "can't open ~/.mailrc: $!";

        open(MAILRC, (glob("~/.mailrc"))[0]) # expand tilde first
        or die "can't open ~/.mailrc: $!";


        Hope this helps!

        John


        At 8:41 -0400 7/3/07, Schelske, Steve K. wrote:
        >Active Perl/Win XP
        >
        >I ran into what I thought was strange behavior with a glob and was
        >hoping for an explanation.
        >
        >This first glob does what I expect - it populates the array with all of
        >the files in the globtest directory that start with test.
        >
        >@files = glob('c:/temp/globtest/test*');
        >print "@files\n";
        >
        >c:/temp/globtest/test .txt c:/temp/globtest/test 20070703082107.txt
        >c:/temp/glob
        >test/test 20070703082256.txt c:/temp/globtest/test20070703081959.txt
        >c:/temp/glo
        >btest/test20070703082010.txt
        >
        >When I put a space at the end of the string in this glob, @files gets
        >populated with the one file in globtest that matches (but not the file
        >file name, and not all of the files that match), but also with every
        >file in the current directory.
        >
        >@files = glob('c:/temp/globtest/test *');
        >print "@files\n";
        >
        >c:/temp/globtest/test acg_batch.pl acg_batch.pl.bak perl_env.pl
        >systest.pl test.
        >pl test.pl.bak test_ora.pl testarg.txt vpn.pl x.txt xrfwin.pl
        >xrfwin.pl.bak
        >
        >Can someone explain why this is happening? Is it really what I've asked
        >glob to do?
        >
        >[Non-text portions of this message have been removed]
        >
        >

        --
        John Francini, francini@...

        "The journey is more important than the destination -- that's part of life.
        If you only live for getting to the end, you're almost always disappointed."
        -- Donald Knuth

        [Non-text portions of this message have been removed]
      • Rob Biedenharn
        ... Yes, it is. You ve asked for two patterns: one is c:/temp/globtest/ test and the other is *. If you want files that begin with the five characters t, e,
        Message 3 of 4 , Jul 3, 2007
        • 0 Attachment
          On Jul 3, 2007, at 8:41 AM, Schelske, Steve K. wrote:
          > Active Perl/Win XP
          >
          > I ran into what I thought was strange behavior with a glob and was
          > hoping for an explanation.
          > ...
          > When I put a space at the end of the string in this glob, @files gets
          > populated with the one file in globtest that matches (but not the file
          > file name, and not all of the files that match), but also with every
          > file in the current directory.
          >
          > @files = glob('c:/temp/globtest/test *');
          > print "@files\n";
          >
          > c:/temp/globtest/test acg_batch.pl acg_batch.pl.bak perl_env.pl
          > systest.pl test.
          > pl test.pl.bak test_ora.pl testarg.txt vpn.pl x.txt xrfwin.pl
          > xrfwin.pl.bak
          >
          > Can someone explain why this is happening? Is it really what I've
          > asked
          > glob to do?

          Yes, it is. You've asked for two patterns: one is c:/temp/globtest/
          test and the other is *. If you want files that begin with the five
          characters t, e, s, t, and space, you'd have to glob('c:/temp/
          globtest/test\\ *')

          -Rob

          Rob Biedenharn http://agileconsultingllc.com
          Rob@...
        • Schelske, Steve K.
          Thank you to all who responded. It all makes sense now. I will strip whitespace off the end of the file name to make sure I m only asking for one pattern.
          Message 4 of 4 , Jul 3, 2007
          • 0 Attachment
            Thank you to all who responded. It all makes sense now.

            I will strip whitespace off the end of the file name to make sure I'm
            only asking for one pattern.

            Thanks again!

            -----Original Message-----
            From: perl-beginner@yahoogroups.com
            [mailto:perl-beginner@yahoogroups.com] On Behalf Of Rob Biedenharn
            Sent: Tuesday, July 03, 2007 9:28 AM
            To: perl-beginner@yahoogroups.com
            Subject: Re: [PBML] question on glob behavior


            On Jul 3, 2007, at 8:41 AM, Schelske, Steve K. wrote:
            > Active Perl/Win XP
            >
            > I ran into what I thought was strange behavior with a glob and was
            > hoping for an explanation. ...
            > When I put a space at the end of the string in this glob, @files gets
            > populated with the one file in globtest that matches (but not the file
            > file name, and not all of the files that match), but also with every
            > file in the current directory.
            >
            > @files = glob('c:/temp/globtest/test *');
            > print "@files\n";
            >
            > c:/temp/globtest/test acg_batch.pl acg_batch.pl.bak perl_env.pl
            > systest.pl test. pl test.pl.bak test_ora.pl testarg.txt vpn.pl x.txt
            > xrfwin.pl xrfwin.pl.bak
            >
            > Can someone explain why this is happening? Is it really what I've
            > asked
            > glob to do?

            Yes, it is. You've asked for two patterns: one is c:/temp/globtest/
            test and the other is *. If you want files that begin with the five
            characters t, e, s, t, and space, you'd have to glob('c:/temp/
            globtest/test\\ *')

            -Rob

            Rob Biedenharn http://agileconsultingllc.com
            Rob@...




            Unsubscribing info is here:
            http://help.yahoo.com/help/us/groups/groups-32.html
            Yahoo! Groups Links
          Your message has been successfully submitted and would be delivered to recipients shortly.