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

Re: [PBML] Help with variable

Expand Messages
  • Damien Carbery
    ... computed ... directory name ... that ... better to ... Shawn - that substitution is a bit drastic. It will convert spaces to underscores and spaces are
    Message 1 of 9 , Oct 5, 2005
    • 0 Attachment
      --- In perl-beginner@yahoogroups.com, Shawn Corey <shawn.corey@s...>
      wrote:
      > sarath wrote:
      > > Hi,
      > >
      > > thanks for the reply. But the problem is the directory name is
      computed
      > > in the script. It changes every time. I am extracting the
      directory name
      > > from a log file into $dirname. How can I exscape something that is
      > > stored in a variable like $dirname?
      >
      > Are you running under UNIX or MS DOS? Under UNIX the only character
      that
      > needs be escaped is '/'. MS DOS on the other hand has many special
      > characters, most of which cannot be used in a file name. It is
      better to
      > just replace them:
      >
      > $dirname =~ s/\W+/_/g;
      >
      >
      > --- Shawn

      Shawn - that substitution is a bit drastic. It will convert spaces to
      underscores and spaces are valid file name characters in Windows (and
      Unix).

      > and I am using print `mkdir C:/perl/$dirname` command to create it.
      Have you tried my suggestion?
      mkdir( "C:/perl/$dirname" );

      Please also provide us with some code from your script. Even better,
      write a small script that has the same problem, just set $dirname to a
      value that causes the problem.
      Something like:

      #!/usr/bin/perl -w
      use strict;
      my $dirname = '9.1.3C'; # Change this as nessary.
      # So you and we can see the value.
      print "OS: $^O, \$dirname: $dirname\n";
      mkdir "C:/Perl/$dirname";
      # 1 (one) is good. If it fails an error message will be printed.
      print "mkdir result: $? $!\n";

      If *you* run this and report back on the results to us we might be
      able to help you. Until then there'll be a lot of guessing.
    • Ray Brannam
      ... Actually that is not quite correct, there are many characters that need escaping e.g. , , , ,| to name a few. It would be better if you explain
      Message 2 of 9 , Oct 5, 2005
      • 0 Attachment
        Damien Carbery wrote:

        >--- In perl-beginner@yahoogroups.com, Shawn Corey <shawn.corey@s...>
        >wrote:
        >
        >
        >>sarath wrote:
        >>
        >>
        >>>Hi,
        >>>
        >>>thanks for the reply. But the problem is the directory name is
        >>>
        >>>
        >computed
        >
        >
        >>>in the script. It changes every time. I am extracting the
        >>>
        >>>
        >directory name
        >
        >
        >>>from a log file into $dirname. How can I exscape something that is
        >>>stored in a variable like $dirname?
        >>>
        >>>
        >>Are you running under UNIX or MS DOS? Under UNIX the only character
        >>
        >>
        >that
        >
        >
        >>needs be escaped is '/'. MS DOS on the other hand has many special
        >>characters, most of which cannot be used in a file name. It is
        >>
        >>
        >better to
        >
        >
        >>just replace them:
        >>
        >> $dirname =~ s/\W+/_/g;
        >>
        >>
        >> --- Shawn
        >>
        >>
        >
        >Shawn - that substitution is a bit drastic. It will convert spaces to
        >underscores and spaces are valid file name characters in Windows (and
        >Unix).
        >
        >
        >
        >>and I am using print `mkdir C:/perl/$dirname` command to create it.
        >>
        >>
        >Have you tried my suggestion?
        >mkdir( "C:/perl/$dirname" );
        >
        >Please also provide us with some code from your script. Even better,
        >write a small script that has the same problem, just set $dirname to a
        >value that causes the problem.
        >Something like:
        >
        >#!/usr/bin/perl -w
        >use strict;
        >my $dirname = '9.1.3C'; # Change this as nessary.
        ># So you and we can see the value.
        >print "OS: $^O, \$dirname: $dirname\n";
        >mkdir "C:/Perl/$dirname";
        ># 1 (one) is good. If it fails an error message will be printed.
        >print "mkdir result: $? $!\n";
        >
        >If *you* run this and report back on the results to us we might be
        >able to help you. Until then there'll be a lot of guessing.
        >
        >
        >

        Actually that is not quite correct, there are many characters that need escaping e.g. <spaces>, ',",\,| to name a few.

        It would be better if you explain what you are trying to do and provide some sample code. It is far easier if we understand what you are trying to accomplish.

        --Ray




        [Non-text portions of this message have been removed]
      • Shawn Corey
        ... That depends on whether you are doing in Perl or in a shell. In Perl, only / and (I missed this one) need be escaped by placing a backslash in front
        Message 3 of 9 , Oct 5, 2005
        • 0 Attachment
          Ray Brannam wrote:
          > Actually that is not quite correct, there are many characters that need
          > escaping e.g. <spaces>, ',",\,| to name a few.

          That depends on whether you are doing in Perl or in a shell. In Perl,
          only '/' and '\' (I missed this one) need be escaped by placing a
          backslash in front of it. This is because Perl uses them as directory
          separators. '/' is from UNIX and '\' is from MS DOS; Perl uses both
          interchangeably.

          In a shell, that depends on the shell. I would not recommend using it
          from a shell since the variable is tainted. I could call it something
          like 'foo; rm -rf / 2>/dev/null' with very unpleasant results. See
          perldoc -q taint


          --- Shawn
        Your message has been successfully submitted and would be delivered to recipients shortly.