[28411] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 9775 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Sep 27 11:05:38 2006

Date: Wed, 27 Sep 2006 08:05:06 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Wed, 27 Sep 2006     Volume: 10 Number: 9775

Today's topics:
    Re: FA: Llama book (UK) <tadmc@augustmail.com>
        glob problem: escaped space seems to be significant too <David.Squire@no.spam.from.here.au>
        How to dynamically generate function name and call it? <shah@typhoon.xnet.com>
    Re: How to dynamically generate function name and call  <phaylon@dunkelheit.at>
    Re: How to dynamically generate function name and call  <tadmc@augustmail.com>
        How to initialize class data? <rmoritz@quantm.invalid.co.za>
    Re: How to initialize class data? <mritty@gmail.com>
        How to read perl-cgi-crash-outputs WITHOUT telnet acces (Patricia Mindanao)
    Re: Need help with pattern matching/substitution. <Peter@PSDT.com>
    Re: Passing object from subroutine problem <tadmc@augustmail.com>
    Re: Passing object from subroutine problem <peace.is.our.profession@gmx.de>
    Re: Problem with glob and filenames containing '[' and  anno4000@radom.zrz.tu-berlin.de
    Re: Problem with glob and filenames containing '[' and  (reading news)
    Re: Problem with glob and filenames containing '[' and  <David.Squire@no.spam.from.here.au>
    Re: Problem with glob and filenames containing '[' and  <David.Squire@no.spam.from.here.au>
    Re: Problem with glob and filenames containing '[' and  <mritty@gmail.com>
    Re: Problem with glob and filenames containing '[' and  <bik.mido@tiscalinet.it>
    Re: Problem with glob and filenames containing '[' and  <David.Squire@no.spam.from.here.au>
    Re: Reference to hash value <bik.mido@tiscalinet.it>
    Re: Splitting and keeping key/value (reading news)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Wed, 27 Sep 2006 07:41:10 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: FA: Llama book (UK)
Message-Id: <slrnehksb6.bnh.tadmc@magna.augustmail.com>

mark@tranchant.co.uk <mark@tranchant.co.uk> wrote:

> Randall Schwartz


His name is "Randal".


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


------------------------------

Date: Wed, 27 Sep 2006 14:43:18 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: glob problem: escaped space seems to be significant too (was Re: Problem with glob and filenames containing '[' and ']')
Message-Id: <efdv5m$3ic$1@gemini.csx.cam.ac.uk>

David Squire wrote:
> Hi folks,
> 
> I'm having trouble using glob to find filenames that contain '[' and
> ']', even though I am escaping those meta-characters. Here is an example
> script and output:

Hi again,

I have reduced this further, getting rid of de-url and a bunch of other
stuff related to my original context. Please see the reduced script and
output below. It seems that having an escaped space as well as an escape
'[' causes the failure to match. See the third last test case.

I hesitate to say it, but this begins to feel like a bug... (covers head).

----


#!/usr/bin/perl

use strict;
use warnings;

print "Directory contents:\n", `ls -1 f*`, "\n";
for my $GlobPattern (
    'fred*',
    'fred[1]*',
    'fred\[1\]*',
    'fred\[1]*',
    'fred[1\]*',
    'fre\ d*',
    'fre\ d\[*',
    'fre\ d\[1*',
    'fre\ d\[1\]*',
    'fre?d\[1\]*',
    'fre\ d?1\]*',
) {
    my @CandidateOrigFiles = glob ($GlobPattern);
    print "\n######################################\n";
    print "$GlobPattern: \@CandidateOrigFiles:\n", join "\n",
@CandidateOrigFiles;
}

----

Output:

Directory contents:
fred]
fred[1]
fre d[1].doc
fred[[1].doc
fred[1].doc


######################################
fred*: @CandidateOrigFiles:
fred[1]
fred[1].doc
fred[[1].doc
fred]
######################################
fred[1]*: @CandidateOrigFiles:

######################################
fred\[1\]*: @CandidateOrigFiles:
fred[1]
fred[1].doc
######################################
fred\[1]*: @CandidateOrigFiles:
fred[1]
fred[1].doc
######################################
fred[1\]*: @CandidateOrigFiles:
fred[1]
fred[1].doc
######################################
fre\ d*: @CandidateOrigFiles:
fre d[1].doc
######################################
fre\ d\[*: @CandidateOrigFiles:
fre d[1].doc
######################################
fre\ d\[1*: @CandidateOrigFiles:
fre d[1].doc
######################################
fre\ d\[1\]*: @CandidateOrigFiles:

######################################
fre?d\[1\]*: @CandidateOrigFiles:
fre d[1].doc
######################################
fre\ d?1\]*: @CandidateOrigFiles:
fre d[1].doc

----

DS


------------------------------

Date: Wed, 27 Sep 2006 13:31:02 +0000 (UTC)
From: Hemant Shah <shah@typhoon.xnet.com>
Subject: How to dynamically generate function name and call it?
Message-Id: <efduem$26t$1@new7.xnet.com>


Folks,

  How do I dynamically generate function and then execute the function?

  I have a perl script that is used to drop/create/update database tables.
  The script has drop/create/update functions for each table, and I
  have if statements to call appropriate functions:

  Example:

  if ($TableName eq "TRAC")
  {
     CreateTRACTable();
  }
  elsif ($TableName eq "TRCO")
  {
     CreateTRCOTable();
  }

  and so on. I have similar if statements for drop and update.
  Instead of having bunch of if statements, I can put the table
  names in a hash and call:

  if ($Action eq "Create")
  {
     Create${TableName}Table():
  }
  elsif ($Action eq "Drop")
  {
     Drop${TableName}Table():
  }

  and so on.


  How do I do this?

  Thanks.

-- 
Hemant Shah                           /"\  ASCII ribbon campaign
E-mail: NoJunkMailshah@xnet.com       \ /  --------------------- 
                                       X     against HTML mail
TO REPLY, REMOVE NoJunkMail           / \      and postings      
FROM MY E-MAIL ADDRESS.           
-----------------[DO NOT SEND UNSOLICITED BULK E-MAIL]------------------
I haven't lost my mind,                Above opinions are mine only.
it's backed up on tape somewhere.      Others can have their own.


------------------------------

Date: 27 Sep 2006 14:53:36 GMT
From: Robert 'phaylon' Sedlacek <phaylon@dunkelheit.at>
Subject: Re: How to dynamically generate function name and call it?
Message-Id: <451a9070$0$18486$9b4e6d93@newsspool3.arcor-online.net>

Hemant Shah <shah@typhoon.xnet.com> wrote
>   How do I dynamically generate function and then execute the function?

Two ideas: Either try utilising a hash as a lookup table:

>   Example:
> 
>   if ($TableName eq "TRAC")
>   {
>      CreateTRACTable();
>   }
>   elsif ($TableName eq "TRCO")
>   {
>      CreateTRCOTable();
>   }

  my %table_actions = (
    TRAC => \&CreateTRACTable,
    TRCO => \&CreateTRCOTable,
  );

  $table_actions{$TableName}->(@arguments);

which would mean you still have to define all actions. If this is
good or bad depends on your situation. If there's really no problem
or danger for you (e.g. nothing user supplied or invalidated, etc.)
then there's the symbolic reference way:

  { no strict 'refs'; # turn this off in smallest scope
    *{'Create' . $TableName . 'Table'}->(@arguments);
  }

This assumes that you're using strict and warnings, which you should
always have turned on.

Depending on your design you might also want to think about some
approach that doesn't need this (the second solution, that is). This can
lead to applications that are harder to maintain in my experience. But
it's imho really great for smaller software projects or prototyping.

hth,
phaylon

-- 
thou shallst fear ..



------------------------------

Date: Wed, 27 Sep 2006 09:58:54 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: How to dynamically generate function name and call it?
Message-Id: <slrnehl4de.c51.tadmc@magna.augustmail.com>

Hemant Shah <shah@typhoon.xnet.com> wrote:

>   How do I dynamically generate function and then execute the function?


By implementing a "dispatch table", usually a hash where the values
are all CODE references.


>   I have a perl script that is used to drop/create/update database tables.
>   The script has drop/create/update functions for each table, and I
>   have if statements to call appropriate functions:
> 
>   Example:
> 
>   if ($TableName eq "TRAC")
>   {
>      CreateTRACTable();
>   }
>   elsif ($TableName eq "TRCO")
>   {
>      CreateTRCOTable();
>   }
> 
>   and so on. 


>   How do I do this?


   my %actions = (    # a "dispatch table", untested
      TRAC => \&CreateTRACTable,
      TRCO => \&CreateTRCOTable
   );

   ...

   die "don't know what action to take for '$TableName'\n"
      unless exists $actions{ $TableName };

   $actions{ $TableName }->();  # call the appropriate function


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


------------------------------

Date: Wed, 27 Sep 2006 15:34:44 +0100
From: Ralph Moritz <rmoritz@quantm.invalid.co.za>
Subject: How to initialize class data?
Message-Id: <uu02te5tn.fsf@quantm.invalid.co.za>

Hi,

I've got some class data which I want to initialize once.
I was thinking of doing it like this:

{ package Foo;

  my $FirstTime = 1;

  sub new {
    my $class = shift;
    my $self = {};
    bless $self, $class;
    $class->init() if ($FirstTime);
  }

  sub init {
    # Initialize class data...
    $FirstTime = 0;
  }
}

Is this okay, or is there a generally accepted way
to do this?

Thanks,
Ralph

-- 
Ralph Moritz
Quantum Solutions       Ph: +27 315 629 557
GPG Public Key: http://ralphm.info/public.gpg


------------------------------

Date: 27 Sep 2006 07:39:51 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: How to initialize class data?
Message-Id: <1159367991.087764.166550@m7g2000cwm.googlegroups.com>

Ralph Moritz wrote:
> I've got some class data which I want to initialize once.
> I was thinking of doing it like this:
>
> { package Foo;
>
>   my $FirstTime = 1;
>
>   sub new {
>     my $class = shift;
>     my $self = {};
>     bless $self, $class;
>     $class->init() if ($FirstTime);
>   }
>
>   sub init {
>     # Initialize class data...
>     $FirstTime = 0;
>   }
> }
>
> Is this okay, or is there a generally accepted way
> to do this?

What do you mean by "class data" in this case?  Variables scoped to the
package, that all class-methods and object-methods would have access
to?  If that's the case, I have a dumb question - why aren't you just
declaring these variables to be initialized to whatever they need to be
initialized to?

That is, instead of:
{
  package Foo;
  my $FirstTime = 1;
  my $data;

  sub new {
      #...
      initialize_data() if $FirstTime;
  }

  sub initialize_data {
      $data = "Stuff";
      $FirstTime = 0;
  }
}

why aren't you just doing:

{
  package Foo;
  my $data = "Stuff";

  sub new {
     #...
  }
}

?

If I've over simplified things so as to remove the point of your
question, please give us a better idea of what it is you're trying to
accomplish.

Paul Lalli



------------------------------

Date: 27 Sep 2006 13:14:41 GMT
From: patmin@hotmail.com (Patricia Mindanao)
Subject: How to read perl-cgi-crash-outputs WITHOUT telnet access ?
Message-Id: <451a7941$0$17393$9b4e6d93@newsspool2.arcor-online.net>

I want to call cgi perl scripts on my web hosters server from 
my HTML web pages (on the the web hosters server too).
It occurs sometimes (especially during development phase) that these cgi-perl scripts
didn't work like intended. They crash because e.g.

- syntax errors
- wrong or changing pathes
- unexpected user input
- ...

In former times I could easily track these crashes resp. try these cgi-scripts directly
with a telnet account/window.

However nowadays telnet access is mostly seen as a security hole a telnet account is not available any more.

How can I get the information about what went worng with a certain cgi-script otherwise ?

The system log shows (AFAIK) only a line similar to "cgi script myscript123.pl dumped at 11:34:23 time"

How can I get the information at which line the cgi script crashed and what the variable values were
at crash time ?

Imagine I entered in the HTML page a wrong path to the perl cgi script.
In which log file is it written?

Patricia



------------------------------

Date: Wed, 27 Sep 2006 14:00:11 GMT
From: Peter Scott <Peter@PSDT.com>
Subject: Re: Need help with pattern matching/substitution.
Message-Id: <pan.2006.09.27.14.00.11.616466@PSDT.com>

On Tue, 26 Sep 2006 22:31:57 +0000, Hemant Shah wrote:
> I have a script that reads data from a file. Each line in a file is comma
> seperated list of values.
> 
> Example:
> 
> DWH_TRRM|'001','A000855747',100,'02-20-2006','CAN DELETE                    '
> DWH_TRRM|'002','A000855737',146,'02-20-2006','CAN'T DELETE                  '

That isn't valid in any CSV format I know.  To keep us from guessing,
please post the specification for the syntax of the lines you are parsing.

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/



------------------------------

Date: Wed, 27 Sep 2006 07:48:17 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Passing object from subroutine problem
Message-Id: <slrnehksoh.bnh.tadmc@magna.augustmail.com>

Paul Lalli <mritty@gmail.com> wrote:
> tim wrote:

>> #takes an active db handle, returns a new session object.
>> sub newSession {
>>     my $dbh = @_[1];
> 
> Always always always use warnings when devleoping Perl code.  This
> should have told you:
> Scalar value @_[1] better written as $_[1]


And that accesses the 2nd argument. What about the 1st argument?

   my $dbh = $_[0];

but a much better way to handle subroutine arguments is:

   my($dbh) = @_;  # don't forget the parenthesis

That makes it very easy to add an additional argument:

   my($dbh, $max_value) = @_;


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


------------------------------

Date: Wed, 27 Sep 2006 15:29:32 +0200
From: Mirco Wahab <peace.is.our.profession@gmx.de>
Subject: Re: Passing object from subroutine problem
Message-Id: <efdujl$5fi$1@mlucom4.urz.uni-halle.de>

Thus spoke Paul Lalli (on 2006-09-27 15:02):
> Mirco Wahab wrote:
>> Thus spoke tim (on 2006-09-27 14:08):
>> > #takes an active db handle, returns a new session object.
>> > sub newSession {
>> >     my $dbh = @_[1];
>>
>> Shouldn't the @_[1], which is
>> a array slice containing the
>> *second* argument of the caller,
>> read: $_[0] ... ?
> 
> No.  Please take a few moments to learn about method calls in Perl:
> perldoc perlmod

Uhmm, you are correct, he actually used
object call syntax, sorry.

>> Here you drop a single argument to your function ...
> 
> Yes, but Perl passed an additional first argument - the object itself.

About time for me to get used to "Perl Objects", I guess.

Thanks & Regards

Mirco


------------------------------

Date: 27 Sep 2006 13:57:26 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: Problem with glob and filenames containing '[' and ']'
Message-Id: <4nvea6Fc6raqU1@news.dfncis.de>

David Squire  <David.Squire@no.spam.from.here.au> wrote in comp.lang.perl.misc:
> Hi folks,
> 
> I'm having trouble using glob to find filenames that contain '[' and
> ']', even though I am escaping those meta-characters. Here is an example
> script and output:

I don't know what goes wrong for you.  It works for me as expected
(after replacing /damocles/documents/ENH1260/2006/2/Short assignment/
with something that exists on my box).

> ----
> 
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> use CGI::Deurl;
> 
> for my $EncodedFile (
>     '/damocles/documents/ENH1260/2006/2/Short
> assignment/20331975_week9%5B1%5D.txt',
>     '/damocles/documents/ENH1260/2006/2/Short
> assignment/20331975_week9.txt',
>     ) {
>     my $OriginalFileBase = deurlstr($EncodedFile);
>     $OriginalFileBase =~ s/\.[^.]+$//; # trim extension
>     $OriginalFileBase =~ s/([\[\]{}?*~\ ,'`"])/\\$1/g; # escape
> characters that are meta in glob;

You can use quotemeta() instead of your s///.  That quotes a little more
(most visibly "/"), but that doesn't hurt.

Anno

[remainder left for reference]

>     print "\$OriginalFileBase = $OriginalFileBase\n";
>     my @CandidateOrigFiles = glob ("$OriginalFileBase*");
>     print "\@CandidateOrigFiles:\n", join "\n", @CandidateOrigFiles;
>     print "\n###########################################################\n";
> }
> 
> ----
> 
> Output:
> 
> Sep 27 - 9:31pm % ./test.pl
>                 <ENTER THE CGI QUERY. End with CTRL+D>
> $OriginalFileBase = /damocles/documents/ENH1260/2006/2/Short\
> assignment/20331975_week9\[1\]
> @CandidateOrigFiles:
> 
> ###########################################################
> $OriginalFileBase = /damocles/documents/ENH1260/2006/2/Short\
> assignment/20331975_week9
> @CandidateOrigFiles:
> /damocles/documents/ENH1260/2006/2/Short
> assignment/20331975_week9%5B1%5D.txt
> /damocles/documents/ENH1260/2006/2/Short
> assignment/20331975_week9%5B1%5D.txt.webbed
> /damocles/documents/ENH1260/2006/2/Short assignment/20331975_week9[1].doc
> ###########################################################
> 
> 
> ----
> 
> As you can see, the first iteration of the for loop produces no matches.
> I have included the second, shortened filename, example to demonstrate
> that the file I want really does exist. Likewise, at the bash prompt I
> can do:
> 
> Sep 27 - 9:31pm % ls /damocles/documents/ENH1260/2006/2/Short\
> assignment/20331975_week9\[1\]*
> /damocles/documents/ENH1260/2006/2/Short assignment/20331975_week9[1].doc
> 
> I am at a loss...
> 
> 
> DS


------------------------------

Date: Wed, 27 Sep 2006 13:57:53 GMT
From: "Mumia W. (reading news)" <paduille.4058.mumia.w@earthlink.net>
Subject: Re: Problem with glob and filenames containing '[' and ']'
Message-Id: <BrvSg.13346$v%4.3419@newsread1.news.pas.earthlink.net>

On 09/27/2006 06:33 AM, David Squire wrote:
> Hi folks,
> 
> I'm having trouble using glob to find filenames that contain '[' and
> ']', even though I am escaping those meta-characters. Here is an example
> script and output:
> 
> ----
> 
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> use CGI::Deurl;
> 
> for my $EncodedFile (
>     '/damocles/documents/ENH1260/2006/2/Short
> assignment/20331975_week9%5B1%5D.txt',
>     '/damocles/documents/ENH1260/2006/2/Short
> assignment/20331975_week9.txt',

This creates two strings containing "Short \n assignment"

I think that's going to confuse glob big-time.

>     ) {
>     my $OriginalFileBase = deurlstr($EncodedFile);
>     $OriginalFileBase =~ s/\.[^.]+$//; # trim extension
>     $OriginalFileBase =~ s/([\[\]{}?*~\ ,'`"])/\\$1/g; # escape
> characters that are meta in glob;

Maybe quotemeta is better.

>     print "\$OriginalFileBase = $OriginalFileBase\n";
>     my @CandidateOrigFiles = glob ("$OriginalFileBase*");
>     print "\@CandidateOrigFiles:\n", join "\n", @CandidateOrigFiles;
>     print "\n###########################################################\n";
> }
> 
> ----
> 
> Output:
> 
> Sep 27 - 9:31pm % ./test.pl
>                 <ENTER THE CGI QUERY. End with CTRL+D>
> $OriginalFileBase = /damocles/documents/ENH1260/2006/2/Short\
> assignment/20331975_week9\[1\]
> @CandidateOrigFiles:
> 
> ###########################################################
> $OriginalFileBase = /damocles/documents/ENH1260/2006/2/Short\
> assignment/20331975_week9
> @CandidateOrigFiles:
> /damocles/documents/ENH1260/2006/2/Short
> assignment/20331975_week9%5B1%5D.txt
> /damocles/documents/ENH1260/2006/2/Short
> assignment/20331975_week9%5B1%5D.txt.webbed
> /damocles/documents/ENH1260/2006/2/Short assignment/20331975_week9[1].doc
> ###########################################################
> 
> 
> ----
> 
> As you can see, the first iteration of the for loop produces no matches.
> I have included the second, shortened filename, example to demonstrate
> that the file I want really does exist. Likewise, at the bash prompt I
> can do:
> 
> Sep 27 - 9:31pm % ls /damocles/documents/ENH1260/2006/2/Short\
> assignment/20331975_week9\[1\]*
> /damocles/documents/ENH1260/2006/2/Short assignment/20331975_week9[1].doc
> 
> I am at a loss...
> 
> 
> DS

I changed your program slightly to run on my system, and I get different 
output:

#!/usr/bin/perl

use strict;
use warnings;


if (1) {
     mkdir 'assignment';
     system ('touch','assignment/file-1.txt',
         'assignment/file[2].txt', 'assignment/file[3].txt');
}


for my $EncodedFile (
     'assignment/file[',
     '/damocles/documents/ENH1260/2006/2/Short
assignment/20331975_week9%5B1%5D.txt',
     '/damocles/documents/ENH1260/2006/2/Short
assignment/20331975_week9.txt',
     ) {
     my $OriginalFileBase = deurlstr($EncodedFile);
     $OriginalFileBase =~ s/\.[^.]+$//; # trim extension
     #$OriginalFileBase =~ s/([\[\]{}?*~\ ,'`"])/\\$1/g; # escape 
characters that are meta in glob;
     $OriginalFileBase = quotemeta $OriginalFileBase;
     print "\$OriginalFileBase = $OriginalFileBase\n";
     my @CandidateOrigFiles = glob ("$OriginalFileBase*");
     print "\@CandidateOrigFiles:\n", join "\n", @CandidateOrigFiles;
     print 
"\n###########################################################\n";
}

sub deurlstr {
     my $url = shift();
     $url =~ s/%([0-9a-f]{2})/chr(hex($1))/ige;
     $url
}

----------
Output:

$OriginalFileBase = assignment\/file\[
@CandidateOrigFiles:
assignment/file[2].txt
assignment/file[3].txt
###########################################################
$OriginalFileBase = \/damocles\/documents\/ENH1260\/2006\/2\/Short\
assignment\/20331975_week9\[1\]
@CandidateOrigFiles:

###########################################################
$OriginalFileBase = \/damocles\/documents\/ENH1260\/2006\/2\/Short\
assignment\/20331975_week9
@CandidateOrigFiles:

###########################################################

-------

As you see, the $OriginalFileBase has a newline in it on the last two 
iterations. If you quote the newline (using quotemeta like I did), glob 
gets nothing because the directory "Short assignment" has no newline 
characters in it (and it also doesn't exist on my system :-) )

I don't know what a newline character in a directory name means to glob, 
but it probably doesn't help ;-)

Build up the original file name strings this way instead:

'/damocles/documents/ENH1260/2006/2/Short '
 . 'assignment/20331975_week9%5B1%5D.txt'


HTH


-- 
paduille.4058.mumia.w@earthlink.net


------------------------------

Date: Wed, 27 Sep 2006 15:13:54 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: Problem with glob and filenames containing '[' and ']'
Message-Id: <efe0v2$726$1@gemini.csx.cam.ac.uk>

Mumia W. (reading news) wrote:
> On 09/27/2006 06:33 AM, David Squire wrote:
>> Hi folks,
>>
>> I'm having trouble using glob to find filenames that contain '[' and
>> ']', even though I am escaping those meta-characters. Here is an example
>> script and output:
>>
>> ----
>>
>> #!/usr/bin/perl
>>
>> use strict;
>> use warnings;
>>
>> use CGI::Deurl;
>>
>> for my $EncodedFile (
>>     '/damocles/documents/ENH1260/2006/2/Short
>> assignment/20331975_week9%5B1%5D.txt',
>>     '/damocles/documents/ENH1260/2006/2/Short
>> assignment/20331975_week9.txt',
> 
> This creates two strings containing "Short \n assignment"
> 
> I think that's going to confuse glob big-time.

No. That's just an artifact of word-wrapping in your newsreader. See my
second, simpler, example.


DS


------------------------------

Date: Wed, 27 Sep 2006 15:15:04 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: Problem with glob and filenames containing '[' and ']'
Message-Id: <efe119$726$2@gemini.csx.cam.ac.uk>

anno4000@radom.zrz.tu-berlin.de wrote:
> David Squire  <David.Squire@no.spam.from.here.au> wrote in comp.lang.perl.misc:
>> Hi folks,
>>
>> I'm having trouble using glob to find filenames that contain '[' and
>> ']', even though I am escaping those meta-characters. Here is an example
>> script and output:
> 
> I don't know what goes wrong for you.  It works for me as expected
> (after replacing /damocles/documents/ENH1260/2006/2/Short assignment/
> with something that exists on my box).

Thanks. Would you be able to try my second, simpler, example too? That
seems to narrow down the oddness.


DS


------------------------------

Date: 27 Sep 2006 07:26:28 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Problem with glob and filenames containing '[' and ']'
Message-Id: <1159367188.634741.79260@b28g2000cwb.googlegroups.com>

David Squire wrote:
> I'm having trouble using glob to find filenames that contain '[' and
> ']', even though I am escaping those meta-characters. Here is an example
> script and output:
>
> ----
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> use CGI::Deurl;
>
> for my $EncodedFile (
>     '/damocles/documents/ENH1260/2006/2/Short
> assignment/20331975_week9%5B1%5D.txt',
>     '/damocles/documents/ENH1260/2006/2/Short
> assignment/20331975_week9.txt',
>     ) {
>     my $OriginalFileBase = deurlstr($EncodedFile);
>     $OriginalFileBase =~ s/\.[^.]+$//; # trim extension
>     $OriginalFileBase =~ s/([\[\]{}?*~\ ,'`"])/\\$1/g; # escape
> characters that are meta in glob;
>     print "\$OriginalFileBase = $OriginalFileBase\n";
>     my @CandidateOrigFiles = glob ("$OriginalFileBase*");
>     print "\@CandidateOrigFiles:\n", join "\n", @CandidateOrigFiles;
>     print "\n###########################################################\n";
> }
>
> ----
>
> Output:
>
> Sep 27 - 9:31pm % ./test.pl
>                 <ENTER THE CGI QUERY. End with CTRL+D>
> $OriginalFileBase = /damocles/documents/ENH1260/2006/2/Short\
> assignment/20331975_week9\[1\]
> @CandidateOrigFiles:
>
> ###########################################################
> $OriginalFileBase = /damocles/documents/ENH1260/2006/2/Short\
> assignment/20331975_week9
> @CandidateOrigFiles:
> /damocles/documents/ENH1260/2006/2/Short
> assignment/20331975_week9%5B1%5D.txt
> /damocles/documents/ENH1260/2006/2/Short
> assignment/20331975_week9%5B1%5D.txt.webbed
> /damocles/documents/ENH1260/2006/2/Short assignment/20331975_week9[1].doc
> ###########################################################
>

Hmm.  Not sure I know what to tell you, as I don't seem able to
reproduce the results....

$ ls filewith\[bracket\]*
filewith[bracket].txt
$ perl -le'print for glob(q{filewith\[bracket\].*})'
filewith[bracket].txt

This is perl, v5.8.4 built for sun4-solaris

Paul Lalli



------------------------------

Date: 27 Sep 2006 16:27:22 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Problem with glob and filenames containing '[' and ']'
Message-Id: <2vvkh2ta3h511ehqhkr0dl8cposvumlusg@4ax.com>

On Wed, 27 Sep 2006 12:33:26 +0100, David Squire
<David.Squire@no.spam.from.here.au> wrote:

>I'm having trouble using glob to find filenames that contain '[' and

Well I'm a big fan of glob() myself, and I recommend using it
especially when I see people using lower level opendir() & C. in
situations in which it's not strictly necessary, but this may be a
situation in which it may indeed be good to do so.

>']', even though I am escaping those meta-characters. Here is an example
>script and output:

However, I don't seem to have that problem:

  C:\TEMP>touch foo[bar]
  
  C:\TEMP>touch foo[baz]
  
  C:\TEMP>perl -le "print for glob 'foo\\[*\\]'"
  foo[bar]
  foo[baz]


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


------------------------------

Date: Wed, 27 Sep 2006 15:30:20 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: Problem with glob and filenames containing '[' and ']'
Message-Id: <efe1ts$8r9$1@gemini.csx.cam.ac.uk>

Michele Dondi wrote:
> On Wed, 27 Sep 2006 12:33:26 +0100, David Squire
> <David.Squire@no.spam.from.here.au> wrote:
> 
>> I'm having trouble using glob to find filenames that contain '[' and
> 
> Well I'm a big fan of glob() myself, and I recommend using it
> especially when I see people using lower level opendir() & C. in
> situations in which it's not strictly necessary, but this may be a
> situation in which it may indeed be good to do so.

I've just written a work around to do so :)

>> ']', even though I am escaping those meta-characters. Here is an example
>> script and output:
> 
> However, I don't seem to have that problem:
> 
>   C:\TEMP>touch foo[bar]
>   
>   C:\TEMP>touch foo[baz]
>   
>   C:\TEMP>perl -le "print for glob 'foo\\[*\\]'"
>   foo[bar]
>   foo[baz]

Yeah, as you will see from my second post, the critical thing seems to
be the presence of an escaped space as well. Thanks.


DS


------------------------------

Date: 27 Sep 2006 16:27:18 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Reference to hash value
Message-Id: <cqvkh25u1i8irse3aog6d08p2bleicdr20@4ax.com>

On 27 Sep 2006 10:56:02 GMT, anno4000@radom.zrz.tu-berlin.de wrote:

>> No. Dereference and reference inverse functions. If you compose them
>> you obtain identity.
>
>...with one exception.  When $x is an object that overloads hash
>de-referencing  \%$x is in general different from $x.

Just another instance of Perl magic!


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


------------------------------

Date: Wed, 27 Sep 2006 13:57:54 GMT
From: "Mumia W. (reading news)" <paduille.4058.mumia.w@earthlink.net>
Subject: Re: Splitting and keeping key/value
Message-Id: <CrvSg.13347$v%4.7998@newsread1.news.pas.earthlink.net>

On 09/26/2006 08:57 AM, Sandman wrote:
> In article <3h9Sg.6492$UG4.1495@newsread2.news.pas.earthlink.net>,
>  "Mumia W. (reading news)" <paduille.4058.mumia.w@earthlink.net> 
>  wrote:
> 
>> I would use the substitution operator s/// to repeatedly suck off 
>> keyword and value segments and place them in a hash. The /e option to 
>> s/// allows you execute complicated expressions, and that's what I would 
>> use here.
>>
>> Try it yourself.
> 
> Yeah, that's pretty much how I've been doing it. I just thought that 
> there were a more modular approach. I'll try some more. Thanks :)
> 
> 
> 

Can you show me how you did it?

-- 
paduille.4058.mumia.w@earthlink.net


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 9775
***************************************


home help back first fref pref prev next nref lref last post