using HTML::Template effectively

  • Follow


I've been using perl scripts to mechanize oft-repeated tasks for my 
website.  I tell my friend what I'm doing and he says, "oh you need 
HTML::Template."  What I hope to do with this thread is re-write it with 
modules, as well as providing a utility to make my choice of filenames 
less obvious, but still within the organizational scheme that will be 
this site.

First, though, I want to go back just a bit and clear up my last few 
difficulties with what has gone before:

#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
my $domain   = '';
my $username = '';
my $password = '';
my $word = "ceiling";
my $ftp      = Net::FTP->new( $domain, Debug => 1, Passive => 1 )
   or die "Can't connect: $@\n";
$ftp->login( $username, $password ) or die "Couldn't login\n";
$ftp->binary();

# get files from remote root that end in html:
my @remote_files = $ftp->ls();

# print "remote files are: @remote_files\n";
my @matching = map /ceiling_(\d+)\.html/, @remote_files;
print "matching is @matching\n";
push( @matching, 1 );
@matching = sort { $a <=> $b } @matching;
my $winner    = pop @matching;
my $newnum1   = $winner + 1;
my $html_file = "ceiling_$newnum1.html";
print "html file is  $html_file\n";

# create file for html stubouts
open( my $fh, '>', $html_file )
   or die("Can't open $html_file for writing: $!");
print $fh "<html>\n";
print $fh "<head>\n";
print $fh "<title>Lutherhaven Renovation</title>\n";
print $fh "</head>\n";
print $fh "<body bgcolor=white>\n";
print $fh "<h1>Basement Ceiling Materials</h1>\n";

# get files from Desktop/images/
my $path  = '/home/dan/Desktop/upload_luther/';
my @files = <$path*>;

# get ls from remote image directory
$ftp->cwd('/images/') or die "cwd failed $@\n";
my @list = $ftp->ls();

# main control
for my $name (@files) {
     print "name is $name\n";
     my ($ext) = $name =~ /([^.]*)$/;
     print "ext is $ext\n";

     @matching = map /image_(\d+)\.$ext$/, @list;
     print "matching is @matching\n";
     push( @matching, 1 );
     @matching = sort { $a <=> $b } @matching;
     $winner = pop @matching;
     my $newnum    = $winner + 1;
     my $new_file2 = "image_$newnum.$ext";
     print "newfile is $new_file2\n";
     $ftp->put( $name, $new_file2 ) or die "put failed $!\n";
     push( @list, $new_file2 );
     unlink($name);

     print $fh "<img src=\"/images/$new_file2\"/>\n\n";
     print $fh "<p>caption for $new_file2 <\/p>\n";

}

close $fh;
$ftp->cdup() or die "cdup failed $@\n";
$ftp->put($html_file) or die "put failed $@\n";
my @r = $ftp->ls();
print "@r\n";

Q1)  I continue to have difficulties combining the syntax of lexical 
variables and RE's:
my $word = "ceiling";
my @matching = map /ceiling_(\d+)\.html/, @remote_files;
my $html_file = "ceiling_$newnum1.html";

How would I write those last 2 lines of code with $word instead of 
"ceiling?"

Q2)  There's a huge amount of reading out there on this topic, which I'm 
grateful for.  Anyone have a link that they think is particularly 
effective at studying up on this?

Q3)  This one's a little out there.  Right after the unlink statement up 
there, or better yet, just before the close statement, what would happen 
if you wrote:

open( my $gh, '>', ceiling1.pl) or die("Can't open ceiling1.pl for 
writing: $!");

where the name of the script itself is ceiling1.pl,and then attempted to 
write from  $gh to $fh linewise?  I know it's illegal in C, so I don't 
want to just try it and see what happens.  It's too much work cleaning 
up after the nasal demons when they get on your keyboard.

Thanks for your comment,
-- 
Cal
0
Reply cal819 (188) 6/26/2012 7:31:01 PM

On 06/26/12 14:31, Cal Dershowitz wrote:
> I've been using perl scripts to mechanize oft-repeated tasks for my
> website. I tell my friend what I'm doing and he says, "oh you need
> HTML::Template."

Have your friend help you first and do we need to know what your friend 
says??..

 >What I hope to do with this thread is re-write it with
> modules, as well as providing a utility to make my choice of filenames
> less obvious, but still within the organizational scheme that will be
> this site.
>
> First, though, I want to go back just a bit and clear up my last few
> difficulties with what has gone before:

First though, this has nothing to do with the Subject!!!!!!!!  Folks
who know a lot about HTML::Template just wasted their time reading
your post.

>
> Q1) I continue to have difficulties combining the syntax of lexical
> variables and RE's:
> my $word = "ceiling";
> my @matching = map /ceiling_(\d+)\.html/, @remote_files;
> my $html_file = "ceiling_$newnum1.html";
>
> How would I write those last 2 lines of code with $word instead of
> "ceiling?"

This is all you needed to post.  Code showing the issue and
your question.

Use  ${word} where you want it to occur, instead of $word.

my $html_file = "${word}_$newnum1.html";

Since you could have a variable $word_, you need to show
that you really only want it to use $word.


>
> Q2) There's a huge amount of reading out there on this topic, which I'm
> grateful for. Anyone have a link that they think is particularly
> effective at studying up on this?
>
> Q3) This one's a little out there. Right after the unlink statement up
> there, or better yet, just before the close statement, what would happen
> if you wrote:
>
> open( my $gh, '>', ceiling1.pl) or die("Can't open ceiling1.pl for
> writing: $!");

No need for () there..
    die "Can't open ceiling.pl for writing: $!"

>
> where the name of the script itself is ceiling1.pl,and then attempted to
> write from $gh to $fh linewise? I know it's illegal in C, so I don't
> want to just try it and see what happens.

Bye bye ceiling.pl..

Think about it.....
What do you think '>' will do?
Why do you think you want do to that?
Actually, I'm not sure what you're asking.. How do you 'write from $gh 
to $fh'?

You can answer this yourself.  Be sure to have a copy of ceiling.pl 
first though.

> It's too much work cleaning up...
Do you think anyone will continue reading your post if put in
more garbage???..

0
Reply glex_no-spam (67) 6/26/2012 8:25:29 PM


Quoth Cal Dershowitz <cal@example.invalid>:
> 
> Q1)  I continue to have difficulties combining the syntax of lexical 
> variables and RE's:
> my $word = "ceiling";
> my @matching = map /ceiling_(\d+)\.html/, @remote_files;
> my $html_file = "ceiling_$newnum1.html";
> 
> How would I write those last 2 lines of code with $word instead of 
> "ceiling?"

    my @matching = map /${word}_(\d+)\.html/, @remote_files;
    my $html_file = "${ceiling}_${newnum1}.html";

When you are interpolating a variable into a string (a pattern is just a
funny kind of string), and the character following the variable name
could legitimately be part of the name, you need the braces to
disambiguate. If you find it easier, you could get into the habit of
always putting them in: they never do any harm, and sometimes make
things clearer even when they aren't strictly ambiguous.

> Q2)  There's a huge amount of reading out there on this topic, which I'm 
> grateful for.  Anyone have a link that they think is particularly 
> effective at studying up on this?

I would say you want to buy and read the book 'Learning Perl'
(1-4493-0358-7; the reference in perlfaq2 is for the previous edition). I
have yet to see a good web tutorial for Perl (which isn't to say they
don't exist, of course).

> Q3)  This one's a little out there.  Right after the unlink statement up 
> there, or better yet, just before the close statement, what would happen 
> if you wrote:
> 
> open( my $gh, '>', ceiling1.pl) or die("Can't open ceiling1.pl for 
> writing: $!");
> 
> where the name of the script itself is ceiling1.pl,and then attempted to 
> write from  $gh to $fh linewise?

Do you mean the other way around (overwriting the script file itself)?
In that case: nothing, except you'd've just overwritten your script
file. Once perl gets to the point of actually running your program, it's
already read and compiled the whole thing; you can't affect the compiled
program by overwriting the source.

'What happens if I overwrite the source from a BEGIN block, before it's
finished compiling?' is a somewhat more interesting question. I'm not
entirely certain, but I believe the answer is just that perl reads
blocks from the file, and whatever happens to be there at that moment is
passed to the compiler.

> I know it's illegal in C, so I don't want to just try it and see what
> happens.

What you're talking about is more akin to writing a C program that
overwrites its own .c source, than one which overwrites its own running
executable.

> It's too much work cleaning up after the nasal demons when they get on
> your keyboard.

In principle there are no nasal demons in Perl. If perl ever does
something undefined, that's a bug in perl (or possibly in an XS module
you've loaded).

(This is not quite as true as it ought to be: that is, there are still
some remaining bugs in perl. They're not easy to fall into by accident,
though.)

Ben

0
Reply ben6057 (864) 6/26/2012 8:42:09 PM

Ben Morrow <ben@morrow.me.uk> writes:

>> It's too much work cleaning up after the nasal demons when they get on
>> your keyboard.
>
> In principle there are no nasal demons in Perl. If perl ever does
> something undefined, that's a bug in perl (or possibly in an XS module
> you've loaded).

The result of my having both compile-time and run-time effects comes
pretty close to being not really defined and not really a bug.

At least perlsub from 5.8.8 does not specify what happens when you
by-pass the run-time effects, but I'm am pretty sure it would be
considered a bug to remove this "feature" and 5.10 provided a new
keyword just to make this feature official and clear.

//Makholm 

0
Reply peter7366 (49) 6/27/2012 7:25:41 AM

Ben Morrow <ben@morrow.me.uk> writes:

[...]

>> It's too much work cleaning up after the nasal demons when they get on
>> your keyboard.
>
> In principle there are no nasal demons in Perl. If perl ever does
> something undefined, that's a bug in perl (or possibly in an XS module
> you've loaded).

It is generally impossible to 'do something undefined' (although
certain people who frequent C-related newsgroups apparently can't ever
get the meaning of this adjective into their head). When the C
standard states that 'in such-and-such a case, the behaviour is
undefined', this is defined as 'the C standard imposes no requirements
for this situation', or, in other words, it contains no specific
information regarding it. Insofar such information is desired, it can
usually be obtained in some other way. A C implementation which would
cause a computer to turn into a dancing icebear in some situation where
the C standard 'leaves the behaviour undefined' would not be
considered non-compliant because of this. But this doesn't mean it
such an implementation is possible and completely wild speculations
of this kind belong in the realm of fiction.

That said, there's at least one case where the behaviour of perl
(5.10.1) is not defined. The documentation of the sort operator states
that

	In list context, this sorts the LIST and returns the sorted
	list value.In scalar context, the behaviour of "sort()" is
	undefined.
0
Reply rweikusat (2678) 6/27/2012 9:47:55 PM

Quoth Rainer Weikusat <rweikusat@mssgmbh.com>:
> Ben Morrow <ben@morrow.me.uk> writes:
> 
> > In principle there are no nasal demons in Perl. If perl ever does
> > something undefined, that's a bug in perl (or possibly in an XS module
> > you've loaded).
> 
> That said, there's at least one case where the behaviour of perl
> (5.10.1) is not defined. The documentation of the sort operator states
> that
> 
> 	In list context, this sorts the LIST and returns the sorted
> 	list value.In scalar context, the behaviour of "sort()" is
> 	undefined.

sort in scalar context returns undef (and does no sorting), and has done
since at least 5.000 (I just checked).

There are a small number of people in the p5p community who seem to
think that putting 'undefined' in the Perl documentation, rather than
actually documenting current and potential future behaviour, is helpful.
This is what you would probably call 'FUD', and in this instance I would
agree with you. 

'Undefined', in the stdc sense, is only a useful concept when you are
standardising multiple implementations; there is only one implementation
of perl 5, there will (almost certainly) only ever *be* one
implementation (cf Ponie), and in practice the current behaviour of the
interpreter is a much better guide to probable future behaviour than the
documentation. (That is, a great many of the discrepancies between code
and documentation are eventually resolved in favour of the code, since
to do otherwise runs the risk of breaking people's working programs.)
More generally, perl's documentation is descriptive, unlike the C
standard which is prescriptive, so ideas like 'the implementation is
free to do anything it likes if you do this' simply don't apply.

In this case, I presume there was an idea at some point that sort in
scalar (void?) context could be used to sort an array in place, and
rather than explain that properly someone thought it would be better
just to throw some 'undefined's around. In fact, I very much doubt this
will ever happen, given that

    @ary = sort @ary;

is already optimised to sort in place.

Ben

0
Reply ben6057 (864) 6/28/2012 1:45:17 AM

On 06/27/2012 07:45 PM, Ben Morrow wrote:
>
> Quoth Rainer Weikusat<rweikusat@mssgmbh.com>:
>> Ben Morrow<ben@morrow.me.uk>  writes:
>>
>>> In principle there are no nasal demons in Perl. If perl ever does
>>> something undefined, that's a bug in perl (or possibly in an XS module
>>> you've loaded).
>>
>> That said, there's at least one case where the behaviour of perl
>> (5.10.1) is not defined. The documentation of the sort operator states
>> that
>>
>> 	In list context, this sorts the LIST and returns the sorted
>> 	list value.In scalar context, the behaviour of "sort()" is
>> 	undefined.
>
> sort in scalar context returns undef (and does no sorting), and has done
> since at least 5.000 (I just checked).

I don't have really have anything to say here;  I'm just editing the way 
I do.  I assume that rainer has something to say about this, and he can 
use this edit to do so.
>
> There are a small number of people in the p5p community who seem to
> think that putting 'undefined' in the Perl documentation, rather than
> actually documenting current and potential future behaviour, is helpful.
> This is what you would probably call 'FUD', and in this instance I would
> agree with you.

What is FUD?
>
> 'Undefined', in the stdc sense, is only a useful concept when you are
> standardising multiple implementations; there is only one implementation
> of perl 5, there will (almost certainly) only ever *be* one
> implementation (cf Ponie), and in practice the current behaviour of the
> interpreter is a much better guide to probable future behaviour than the
> documentation. (That is, a great many of the discrepancies between code
> and documentation are eventually resolved in favour of the code, since
> to do otherwise runs the risk of breaking people's working programs.)
> More generally, perl's documentation is descriptive, unlike the C
> standard which is prescriptive, so ideas like 'the implementation is
> free to do anything it likes if you do this' simply don't apply.

OK
>
> In this case, I presume there was an idea at some point that sort in
> scalar (void?) context could be used to sort an array in place, and
> rather than explain that properly someone thought it would be better
> just to throw some 'undefined's around. In fact, I very much doubt this
> will ever happen, given that
>
>      @ary = sort @ary;
>
> is already optimised to sort in place.

Ok, it might give Juergen Gleichner a stroke, but why did that sort fail 
in my last script as opposed to the ones with the a and the b?

Off to karaoke stardom ...
-- 
Cal

"Big win for US today."

0
Reply cal819 (188) 6/29/2012 4:02:14 AM

On 06/26/2012 02:25 PM, J. Gleixner wrote:
> On 06/26/12 14:31, Cal Dershowitz wrote:

[snip]
>  >What I hope to do with this thread is re-write it with
>> modules, as well as providing a utility to make my choice of filenames
>> less obvious, but still within the organizational scheme that will be
>> this site.
>>
>> First, though, I want to go back just a bit and clear up my last few
>> difficulties with what has gone before:
>
> First though, this has nothing to do with the Subject!!!!!!!! Folks
> who know a lot about HTML::Template just wasted their time reading
> your post.

Vielen, vielen Dank f�r Ihre Antwort, J�rgen, ich habe massiv f�r Ihre 
F�rbitte zu danken.
>
>>
>> Q1) I continue to have difficulties combining the syntax of lexical
>> variables and RE's:
>> my $word = "ceiling";
>> my @matching = map /ceiling_(\d+)\.html/, @remote_files;
>> my $html_file = "ceiling_$newnum1.html";
>>
>> How would I write those last 2 lines of code with $word instead of
>> "ceiling?"
>
> This is all you needed to post. Code showing the issue and
> your question.

Das ist Ihre Meinung.
>
> Use ${word} where you want it to occur, instead of $word.
>
> my $html_file = "${word}_$newnum1.html";
>
> Since you could have a variable $word_, you need to show
> that you really only want it to use $word.

Indeed this is the correct syntax, thank you.
-- 
Cal
0
Reply cal819 (188) 6/29/2012 4:21:19 AM

On 2012-06-29, Cal Dershowitz <cal@example.invalid> wrote:
>
> What is FUD?

http://lmgtfy.com/?q=fud&l=1


   Justin.

-- 
Justin C, by the sea.
0
Reply justin.1207 (11) 6/29/2012 10:42:48 AM

>>>>> "Ben" == Ben Morrow <ben@morrow.me.uk> writes:

Ben> In this case, I presume there was an idea at some point that sort in
Ben> scalar (void?) context could be used to sort an array in place, and
Ben> rather than explain that properly someone thought it would be better
Ben> just to throw some 'undefined's around. In fact, I very much doubt this
Ben> will ever happen, given that

Ben>     @ary = sort @ary;

Ben> is already optimised to sort in place.

There was also a patch submitted at one time (tongue-in-cheek) to have
sort in a scalar context invoke "nethack", based on a sentence in one of
the early editions of Learning Perl.

print "Just another Perl hacker,"; # the original

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
0
Reply merlyn1 (1433) 6/29/2012 2:27:11 PM

Ben Morrow <ben@morrow.me.uk> writes:
> Quoth Rainer Weikusat <rweikusat@mssgmbh.com>:
>> Ben Morrow <ben@morrow.me.uk> writes:
>> 
>> > In principle there are no nasal demons in Perl. If perl ever does
>> > something undefined, that's a bug in perl (or possibly in an XS module
>> > you've loaded).
>> 
>> That said, there's at least one case where the behaviour of perl
>> (5.10.1) is not defined. The documentation of the sort operator states
>> that
>> 
>> 	In list context, this sorts the LIST and returns the sorted
>> 	list value.In scalar context, the behaviour of "sort()" is
>> 	undefined.
>
> sort in scalar context returns undef (and does no sorting), and has done
> since at least 5.000 (I just checked).
>
> There are a small number of people in the p5p community who seem to
> think that putting 'undefined' in the Perl documentation, rather than
> actually documenting current and potential future behaviour, is helpful.
> This is what you would probably call 'FUD', and in this instance I would
> agree with you.

In this instance, I wouldn't, because I never subscribed to the
opinion that "undefined" means "some kind of nameless evil man should
shy away from for fear for his immortal soul". In the given case, I
would paraphrase "undefined" as "The actual behaviour is arguably not
useful and there is no consensus regarding what kind of behaviour
could be useful instead. Therefore, the issue is left officially
undecided until future developments". And that seems a very
common-sense driven approach to me.
0
Reply rweikusat (2678) 6/29/2012 3:16:07 PM

On 06/29/2012 04:42 AM, Justin C wrote:
> On 2012-06-29, Cal Dershowitz<cal@example.invalid>  wrote:
>>
>> What is FUD?
>
> http://lmgtfy.com/?q=fud&l=1
>
>
>     Justin.
>

http://perlmonks.org/index.pl?node_id=46976
-- 
0
Reply cal819 (188) 7/1/2012 4:11:52 AM

On 06/27/2012 01:25 AM, Peter Makholm wrote:
> Ben Morrow<ben@morrow.me.uk>  writes:
>
>>> It's too much work cleaning up after the nasal demons when they get on
>>> your keyboard.
>>
>> In principle there are no nasal demons in Perl. If perl ever does
>> something undefined, that's a bug in perl (or possibly in an XS module
>> you've loaded).
>
> The result of my having both compile-time and run-time effects comes
> pretty close to being not really defined and not really a bug.
>
> At least perlsub from 5.8.8 does not specify what happens when you
> by-pass the run-time effects, but I'm am pretty sure it would be
> considered a bug to remove this "feature" and 5.10 provided a new
> keyword just to make this feature official and clear.

Thx,  Makholm, I finally have output (talking too much about C reminds 
me of bad times)

$ perltidy -b temp1.pl
$ perl temp1.pl
Content-Type: text/html

            <html>
            <head><title>Test Template</title></head>
            <body>
            My Home Directory is /home/dan
            <p>
            My Path is set to 
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
            </body>
            </html>

$ cat temp1.pl
#!/usr/bin/perl -w
use strict;

use HTML::Template;

# open the html template
my $template = HTML::Template->new( filename => 'template.tmpl' );

# fill in some parameters
$template->param( HOME => $ENV{HOME} );
$template->param( PATH => $ENV{PATH} );

# send the obligatory Content-Type and print the template output
print "Content-Type: text/html\n\n", $template->output;

$


I get this, and I have to tell you, what the obligatory content of an 
html page was a bit of a mystery to me.

I'm enjoying the reading.  I got YAMl installed along the way.  It had 
never taken before.  questions:

q1)  I rent a web domain from www.1and1.com .  Would they be able to 
replicate what I just did with my own machine?  Who types in perl 
temp1.pl when a browser wants to see a web page.

remark:  notice how unmunged the html is with the perltidy command!

q2)  How would you perltidy something that was supposed to look like 
well-crafted html?

I'm sure I've made 30 mistakes in this post, and I'd like to say that I 
could get on this right away, but I can't, because the real world 
beckons, which is a happy occasion, because I got the lutherhaven bid, 
in no small part to being able to show what I do on the web using perl 
as a platform.

Peace.
-- 
Cal
0
Reply cal819 (188) 7/2/2012 8:34:04 AM

On 06/29/2012 09:16 AM, Rainer Weikusat wrote:
> Ben Morrow<ben@morrow.me.uk>  writes:
>> Quoth Rainer Weikusat<rweikusat@mssgmbh.com>:
>>> Ben Morrow<ben@morrow.me.uk>  writes:
>>>
>>>> In principle there are no nasal demons in Perl. If perl ever does
>>>> something undefined, that's a bug in perl (or possibly in an XS module
>>>> you've loaded).
>>>
>>> That said, there's at least one case where the behaviour of perl
>>> (5.10.1) is not defined. The documentation of the sort operator states
>>> that
>>>
>>> 	In list context, this sorts the LIST and returns the sorted
>>> 	list value.In scalar context, the behaviour of "sort()" is
>>> 	undefined.
>>
>> sort in scalar context returns undef (and does no sorting), and has done
>> since at least 5.000 (I just checked).
>>
>> There are a small number of people in the p5p community who seem to
>> think that putting 'undefined' in the Perl documentation, rather than
>> actually documenting current and potential future behaviour, is helpful.
>> This is what you would probably call 'FUD', and in this instance I would
>> agree with you.
>
> In this instance, I wouldn't, because I never subscribed to the
> opinion that "undefined" means "some kind of nameless evil man should
> shy away from for fear for his immortal soul". In the given case, I
> would paraphrase "undefined" as "The actual behaviour is arguably not
> useful and there is no consensus regarding what kind of behaviour
> could be useful instead. Therefore, the issue is left officially
> undecided until future developments". And that seems a very
> common-sense driven approach to me.

Are things that are "illegal" in C grandfathered into perl in some 
fashion?  Let me ask a specific question from my misadventures in perl 
autodidactics tonight.

I created such beauties tonight in what I can only call regressing:

Net::FTP=GLOB(0x83871cc)<<< 226 Transfer complete
.. .. logs index.html wsb6121022001 energy green2.m4v Video 151.wmv 
false.wmv vids images zen rev1.html luther1.html lh_1.html lh_17.html 
lh_18.html lh_19.html ceiling_2.html ceiling_2_files {word}_2.html 
{la_veta}_2.html

I seem not to have been able to find my latest working script and need 
to move to different tools to get a page up that I need to do tonight. 
I think Filezilla is wonderful.

Are there illegal filenames in perl?  What is the character class of 
legal characters in a filename?

Also, if you were going to design a character class such that it was to 
be used in randomly-generating prefixes for files, which would you use?

As for me, o O 0 ~ ` < > i 1 I { } [ ] | wouldn't make the first cut.

I certainly wouldn't start a random file with an underscore.  It's been 
a while since I read Plauger's _The Standard C Library_, highly 
recommended.  Such a nice guy, he'll send you the source.

My problems with interpolation seem to continue.  I have _Learning perl" 
and will have plenty of time to read up there.

The monsoon began yesterday in the SW, which is very good news for 
firefighting efforts.  Cheers,
-- 
Cal
0
Reply cal819 (188) 7/4/2012 6:35:52 AM

>>>>> "Cal" == Cal Dershowitz <cal@example.invalid> writes:

Cal> Are there illegal filenames in perl?  What is the character class of legal
Cal> characters in a filename?

Perl defers those choices to the underlying operating system.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
0
Reply merlyn1 (1433) 7/4/2012 6:55:03 AM

Quoth Cal Dershowitz <cal@example.invalid>:
> 
> I seem not to have been able to find my latest working script and need 
> to move to different tools to get a page up that I need to do tonight. 
> I think Filezilla is wonderful.

I found lftp useful for this sort of thing when I still did anything
over FTP.

> Are there illegal filenames in perl?  What is the character class of 
> legal characters in a filename?

That is entirely at the discretion of your OS. Perl just passes the
strings you give it off to the OS filesystem APIs, and passes back the
results.

On Unix systems, all 8-bit bytes are allowed in filenames except for "/"
and "\0".

> Also, if you were going to design a character class such that it was to 
> be used in randomly-generating prefixes for files, which would you use?

[a-zA-Z0-9] unless I had some reason to do otherwise.

> As for me, o O 0 ~ ` < > i 1 I { } [ ] | wouldn't make the first cut.

If you're excluding lookalikes you probably want 'l' as well.
[a-hjkmnp-zA-HJKMNP-Z2-9] ought to be OK.

Ben

0
Reply ben6057 (864) 7/4/2012 7:08:07 AM

>>>>> Cal Dershowitz <cal@example.invalid> writes:

[...]

 > Also, if you were going to design a character class such that it was
 > to be used in randomly-generating prefixes for files, which would you
 > use?

 > As for me, o O 0 ~ ` < > i 1 I { } [ ] | wouldn't make the first cut.

	Depending on the task, I'd generate a wide enough random number
	(or a UUID), and encode it with a Base32 variant, such as the
	Crockford's one.  Like, e. g.:

#!/usr/bin/perl

use strict;
use warnings;

require Convert::Base32::Crockford;
require UUID;

UUID::generate (my $uuid);
UUID::unparse ($uuid, my $s);
my $fn
    = Convert::Base32::Crockford::encode_base32 ($uuid);
print ($fn, "\n");

	Then, when looking the file up by the user's supplied name, I'd
	convert the latter into upper case, and replace [oO] with 0 and
	[iIlL] with 1.

	Alternatively, the RFC 4648 Base32 variant may be used, which
	includes B but not 8 (which could be confused with the former.)

http://en.wikipedia.org/wiki/Base32

[...]

-- 
FSF associate member #7257
0
Reply oneingray (253) 7/7/2012 4:56:24 AM

16 Replies
46 Views

(page loaded in 0.299 seconds)


Reply: