Re: Perl question: array member of referenced hash

2009-10-24 Thread Noam Rathaus
Hi Shachar,

First you can always use Data::Dumper:
use Data::Dumper;
print Dumper($ref);

To make sure that the data is stored correctly.

In regard to your question:

my $ref;
my %hash = %{$ref};
foreach my $ptrelem (keys %hash) {
 my @array = @{$ptrelem};
 foreach my $item (@array) {
  print $item;
 }
}

From my experience with perl, doing shortcuts don't work too well, and
doing it the long way, helps with debugging and finding out what the
problem is.

2009/10/24 Shachar Shemesh shac...@shemesh.biz:
 Hi all,

 $ref is a reference to a hash. Hash contains a component called a which is
 an array. I would like to iterate all elements of said array.

 I would like to do something along the lines of:
 foreach my $elem @%$ref{a}
 {
     print $elem\n;
 }

 Which, I think it clear, does not work. I also tried:
 foreach my $elem @(%$ref){a}
 {
     print $elem\n;
 }

 which complains about:
 Bareword a not allowed while strict subs in use

 and:
 foreach my $elem @(%$ref){'a'}
 {
     print $elem\n;
 }

 which complains about:
 Global symbol $elem requires explicit package name at line 3 (i.e. -
 inside the for).

 Any help would be appreciated.

 Thanks,
 Shachar

 --
 Shachar Shemesh
 Lingnu Open Source Consulting Ltd.
 http://www.lingnu.com

 --
 Shachar Shemesh
 Lingnu Open Source Consulting Ltd.
 http://www.lingnu.com

 ___
 Linux-il mailing list
 Linux-il@cs.huji.ac.il
 http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il



___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: Perl question: array member of referenced hash

2009-10-24 Thread Noam Rathaus
Sorry a mistake...

foreach my $ptrelem (keys %hash) {

Should be

foreach my $key (keys %hash) {
 my $ptritem = %hash-{$key};

On Sat, Oct 24, 2009 at 7:55 PM, Noam Rathaus no...@beyondsecurity.com wrote:
 Hi Shachar,

 First you can always use Data::Dumper:
 use Data::Dumper;
 print Dumper($ref);

 To make sure that the data is stored correctly.

 In regard to your question:

 my $ref;
 my %hash = %{$ref};
 foreach my $ptrelem (keys %hash) {
  my @array = @{$ptrelem};
  foreach my $item (@array) {
  print $item;
  }
 }

 From my experience with perl, doing shortcuts don't work too well, and
 doing it the long way, helps with debugging and finding out what the
 problem is.

 2009/10/24 Shachar Shemesh shac...@shemesh.biz:
 Hi all,

 $ref is a reference to a hash. Hash contains a component called a which is
 an array. I would like to iterate all elements of said array.

 I would like to do something along the lines of:
 foreach my $elem @%$ref{a}
 {
     print $elem\n;
 }

 Which, I think it clear, does not work. I also tried:
 foreach my $elem @(%$ref){a}
 {
     print $elem\n;
 }

 which complains about:
 Bareword a not allowed while strict subs in use

 and:
 foreach my $elem @(%$ref){'a'}
 {
     print $elem\n;
 }

 which complains about:
 Global symbol $elem requires explicit package name at line 3 (i.e. -
 inside the for).

 Any help would be appreciated.

 Thanks,
 Shachar

 --
 Shachar Shemesh
 Lingnu Open Source Consulting Ltd.
 http://www.lingnu.com

 --
 Shachar Shemesh
 Lingnu Open Source Consulting Ltd.
 http://www.lingnu.com

 ___
 Linux-il mailing list
 Linux-il@cs.huji.ac.il
 http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il




___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: Perl question: array member of referenced hash

2009-10-24 Thread Dov Grobgeld
Noam beat me to it, but here's perl solution without additional variables:

#!/usr/bin/perl

%hash = (a=['moo','goo','woo'],
 foo=3,
 baz=5);

$ref = \%hash;
foreach my $elem (@{$ref-{a}})
{
print $elem\n;
}

Regards,
Dov

2009/10/24 Shachar Shemesh shac...@shemesh.biz

  Hi all,

 $ref is a reference to a hash. Hash contains a component called a which
 is an array. I would like to iterate all elements of said array.

 I would like to do something along the lines of:
 foreach my $elem @%$ref{a}
 {
 print $elem\n;
 }

 Which, I think it clear, does not work. I also tried:
 foreach my $elem @(%$ref){a}
 {
 print $elem\n;
 }

 which complains about:
 Bareword a not allowed while strict subs in use

 and:
 foreach my $elem @(%$ref){'a'}
 {
 print $elem\n;
 }

 which complains about:
 Global symbol $elem requires explicit package name at line 3 (i.e. -
 inside the for).

 Any help would be appreciated.

 Thanks,
 Shachar

 --
 Shachar Shemesh
 Lingnu Open Source Consulting Ltd.http://www.lingnu.com


 --
 Shachar Shemesh
 Lingnu Open Source Consulting Ltd.http://www.lingnu.com


 ___
 Linux-il mailing list
 Linux-il@cs.huji.ac.il
 http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: Perl question: array member of referenced hash

2009-10-24 Thread Shachar Shemesh

Dov Grobgeld wrote:

Noam beat me to it, but here's perl solution without additional variables:

#!/usr/bin/perl

%hash = (a=['moo','goo','woo'],
 foo=3,
 baz=5);

$ref = \%hash;
foreach my $elem (@{$ref-{a}})

Hi Dov,

Yes, it works. Now can you, please, explain to me why? What is the role 
of each bracket you used (and its location)?


Shachar

--
Shachar Shemesh
Lingnu Open Source Consulting Ltd.
http://www.lingnu.com

___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: Perl question: array member of referenced hash

2009-10-24 Thread Noam Rathaus
Shachar,

{ } in Perl are casting when they surround a value
And the second set of { } around the 'a' mean variable of Hash


2009/10/24 Shachar Shemesh shac...@shemesh.biz:
 Dov Grobgeld wrote:

 Noam beat me to it, but here's perl solution without additional variables:

 #!/usr/bin/perl

 %hash = (a=['moo','goo','woo'],
  foo=3,
  baz=5);

 $ref = \%hash;
 foreach my $elem (@{$ref-{a}})

 Hi Dov,

 Yes, it works. Now can you, please, explain to me why? What is the role of
 each bracket you used (and its location)?

 Shachar

 --
 Shachar Shemesh
 Lingnu Open Source Consulting Ltd.
 http://www.lingnu.com

 ___
 Linux-il mailing list
 Linux-il@cs.huji.ac.il
 http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il



___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: Perl question: array member of referenced hash

2009-10-24 Thread Shachar Shemesh

Noam Rathaus wrote:

Shachar,

{ } in Perl are casting when they surround a value
And the second set of { } around the 'a' mean variable of Hash


  

Grumble grumble grumble

Okay, I'm sorry for being difficult. I really couldn't find the answer 
in the Perl documentation.


I understand the second set of curly braces. I also, somewhat, 
understand that the - replaces the % (i.e. - reference dereferencing). 
What I'm not so clear is what the first set of curly braces do (what do 
you mean by casting - casting to what? How is that decided?). I'm also 
not clear on why the surrounding round brackets are needed. I understand 
they are so this will be a list context, but I don't understand why it's 
needed once I put a @ to dereference the array.


Thanks,
Shachar



foreach my $elem (@{$ref-{a}})



--
Shachar Shemesh
Lingnu Open Source Consulting Ltd.
http://www.lingnu.com

___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: Perl question: array member of referenced hash

2009-10-24 Thread Gabor Szabo
2009/10/24 Shachar Shemesh shac...@shemesh.biz:
 Noam Rathaus wrote:

 Shachar,

 { } in Perl are casting when they surround a value
 And the second set of { } around the 'a' mean variable of Hash




 Grumble grumble grumble

not surprised as this is one of the funky places of Perl 5.


 Okay, I'm sorry for being difficult. I really couldn't find the answer in
 the Perl documentation.

 I understand the second set of curly braces. I also, somewhat, understand
 that the - replaces the % (i.e. - reference dereferencing). What I'm not so
 clear is what the first set of curly braces do (what do you mean by
 casting - casting to what? How is that decided?). I'm also not clear on
 why the surrounding round brackets are needed. I understand they are so this
 will be a list context, but I don't understand why it's needed once I put a
 @ to dereference the array.

 Thanks,
 Shachar

 foreach my $elem (@{$ref-{a}})


err, I don't think that casting is the right word to use here. What
{} does here is
disambiguates the expression. Here is a table

$x - scalar
@x - array
%x - hash

$ra = \...@x  reference to array

sticking @ infront of the reference to an array dereferences it
@x is the same as @$ra   (you could also write and @{$ra} but it is
not necessary)
$x[1]  is the same as  $$ra[1]   (element of array, replace @ by $ and
attach the index)
 but it is ugly so it can also be written as $ra-[1]


$rh = \%x  reference to hash

sticking % infront of the reference to a hash dereferences it
%x is the same as %$rh  (could be also written as %{$rh} but it is not
necessary)
$x{foo} is the same as $$fh{foo} which is the same as $fh-{foo}

Now what if you have two dimensions: first dimension is a hash second
dimension is an array.

%h  is a hash
@something = ('foo');
$h{a} = \...@something;

Which means
print $h{a}[0];# 'foo';


$ref = \%h;   reference to hash

%h is the same as %$ref
$h{a}  is the same as $$ref{a}  or as $ref-{a} which is the reference
to the array: \...@something

sticking @ in front of it would dereference the array which would yield

@$h{a}   or  @$$ref{a}  @$ref-{a}   which is the same as @something

but it is not clear what does either of these mean
(looking at the last one, @$ref could mean $ref is a reference to an array and
that you are dereferencing @$ref  and the resulting thingy is a hash ref)

So we wrap the reference in a curly brace to make it clear that is a
single variable:

@{$h{a}}   or  @{$$ref{a}}  @{$ref-{a}}  which is the same as @{something}

so the {} is basically around the name' of the variable.



Hope this helps
   Gabor

___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: Perl question: array member of referenced hash

2009-10-24 Thread Shachar Shemesh

Gabor Szabo wrote:


err, I don't think that casting is the right word to use here. What
{} does here is
disambiguates the expression.

Let me try to summarize what I understood from your excellent explanation:

Putting a modifier in front of a reference dereference it to the right 
type ($ for scalar etc.). Alternatively, putting a '-' (which is a 
unary operator, not a binary one) also dereferences it, no matter what 
it is pointing to.(at least for array and hash), so long as there is 
some reference to its content on the operator's right (the same as it is 
implemented in C++, only more confusing).


The curly braces act as a scoping operator, making the $/@/% relation to 
parts of the expression unique.


All that is left is understanding why the round braces around the whole 
expression.


Many thanks (the explanation was very useful)
Shachar

--
Shachar Shemesh
Lingnu Open Source Consulting Ltd.
http://www.lingnu.com

___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: Perl question: array member of referenced hash

2009-10-24 Thread Gabor Szabo
On Sat, Oct 24, 2009 at 11:25 PM, Shachar Shemesh shac...@shemesh.biz wrote:
 Let me try to summarize what I understood from your excellent explanation:

if that works for you :-)



 All that is left is understanding why the round braces around the whole
 expression.

Oh, the syntax of foreach has those parentheses

foreach my $iterator (things to iterate over) {
}

Where the things to iterate over can be a simple list of values, an array or
anything that returns a list of values which of course can be any expression.

foreach my $iterator (1,2,3,4,5) {
}

or for the lazy ones

foreach my $iterator (1..5) {
}

or for the really lazy ones:

for my $iterator (1..5) {
}


or

foreach my $iterator (@names) {
}


foreach my $iterator (@$ref) {
}

foreach my $iterator (function_that_returns_thingies()) {
}


Gabor

http://szabgab.com/blog.html
Perl Training Israel http://www.pti.co.il/

___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: Perl question

2004-04-10 Thread Gabor Szabo
On Fri, 9 Apr 2004, Shachar Shemesh wrote:

 Suppose I wanted to write this myself, how would I go about doing it? Is
 there any way of recursively using a dir or file handle name?

Probably you got the anwer from the responses of others already, in
any case you can use constructs such as theses in order to make sure
your filehandle ($fh) and directory handle ($dh) are scoped
to your current block.

open my($fh), , $input;

opendir my($dh), $dirname;

Instead of opendir you might also want to use the following
construct where the between the  you can put any shell wide-card:

foreach my $entry (*) {
}

see also perldoc -f glob

Gabor


=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word unsubscribe in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]



Re: Perl question

2004-04-09 Thread Shachar Shemesh
Shachar Shemesh wrote:

if( (($state[2]  ~)12) ==4 ) { # A directory - 
recurse

That's 0 of course, or even remove altogether and just leave the 
12 part. Same problem remains, however.

--
Shachar Shemesh
Lingnu OpenSource Consulting
http://www.lingnu.com/
=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word unsubscribe in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]


Re: Perl question

2004-04-09 Thread Muli Ben-Yehuda
On Fri, Apr 09, 2004 at 10:54:04AM +0300, Shachar Shemesh wrote:

 1. I know, I write perl like a C programmer, I can't help it. Feel free 
 to show me how it's done.

Not a perl guru by any stretch of the imagination, but behold my
google foo! 

#!/usr/bin/perl

@ARGV = qw(.) unless @ARGV;
use File::Find;
find sub { print $File::Find::name, -d  '/', \n }, @ARGV;

(copied straight out of
http://iis1.cps.unizar.es/Oreilly/perl/cookbook/ch09_08.htm. You were
working at the wrong level of abstraction.)

 2. For some strange reason, the moment I recurse once, the entire loop 
 structure exits. I suspect it's because the DIR handle is global. Will 
 any perl guru comment on how it's supposed to be done?

No idea, but something looks funky about the usage of DIR indeed. 

 3. What the @$([EMAIL PROTECTED] is the difference between my and local? Which 
 one should I use here?

http://prometheus.frii.com/~gnat/yapc/2000-stages/slide21.html

Cheers, 
Muli 
-- 
Muli Ben-Yehuda
http://www.mulix.org | http://mulix.livejournal.com/



signature.asc
Description: Digital signature


Re: Perl question

2004-04-09 Thread Shlomi Fish
On Friday 09 April 2004 10:54, you wrote:
 Hi,

 I'm trying to create a perl program that will recurse into

 subdirectories. I have:
  sub scandir {
  local $dirname=shift;
 

You should use my $dirname = shift; instead of local here. It's safer.

  opendir DIR, $dirname or die Couldn't open ${dirname}: $!\n;
 

Just for safeness, it would be better if you localize DIR here:


local (*DIR);
opendir DIR...


  local $direntry;

Again -  my $direntry;

  while( $direntry=readdir(DIR) ) {

Instead of using readdir in a loop, I strongly recommend that you read all the 
entries into an array and then loop on it:

my @entries = readdir(DIR);
closedir(DIR);
foreach $direntry (@entries)
{
..
}

Otherwise, many dirhandles will be opened, and I'm not sure if the results 
will be very expectful. (with local and all)

  if( $direntry!~/^\./ ) {

Do you want to avoid only . and .. or any directory that starts with .? 
Your code does the latter.

  local @state=stat($dirname.$direntry);
  print $dirname$direntry $state[2]\n;
  if( (($state[2]  ~)12) ==4 ) { # A directory - recurse

This can be simplified into: 


if (-d $dirname.$direntry). { # A directory - recurse

-d is a file check that checks if a file is a directory. Check perldoc -f 
-X for more information.  

  scandir($dirname.$direntry./);

This is good. Albeit many people like to use interpolation:

scandir($dirname$direntry/);

  }
  }
  }
 
  closedir DIR;
  }

 1. I know, I write perl like a C programmer, I can't help it. Feel free
 to show me how it's done.

:-)

 2. For some strange reason, the moment I recurse once, the entire loop
 structure exits. I suspect it's because the DIR handle is global. Will
 any perl guru comment on how it's supposed to be done?

If you localize *DIR, it won't happen.

 3. What the @$([EMAIL PROTECTED] is the difference between my and local? Which
 one should I use here?


my declares a lexically-scoped variable. It is similar to (define) in 
Scheme. The value of this variable is not propagated to subsequent function 
calls (unless they are closures). So for example:


#!/usr/bin/perl -w

use strict;

my $myvar = 3;

sub my_called_function
{
print In called: \$myvar = $myvar\n;

$myvar = 10;
}

sub my_caller_function
{
my $myvar;

$myvar = 500;

my_called_function();

print In caller: \$myvar = $myvar\n;
}

my_caller_function();

print In Global: \$myvar = $myvar\n;


will print:


In called: $myvar = 3
In caller: $myvar = 500
In Global: $myvar = 10


local creates a dynmically scoped localization. The value of the variable is 
recorded when the local is encountered, and restored once the block exits. It 
is propogated to all subsequent function calls. So for example:


#!/usr/bin/perl -w

$myvar = 3;

sub my_called_function
{
print In called: \$myvar = $myvar\n;

$myvar = 10;
}

sub my_caller_function
{
local $myvar;

$myvar = 500;

my_called_function();

print In caller: \$myvar = $myvar\n;
}

my_caller_function();

print In Global: \$myvar = $myvar\n;


prints:


In called: $myvar = 500
In caller: $myvar = 10
In Global: $myvar = 3


Generally, my should be used for all regular variables (scalars, arrays and 
hashes) because it's safer, while local should be used for filehandles, and 
built-in variables. (the ones in perldoc perlvar) This is because they 
cannot be effectively scoped with my.

Hope it helps. There's more information about it on the Web and in the Perl 
documentation.

As others noted, you can use the File::Find Perl module to traverse a 
directory tree.

Regards,

Shlomi Fish

-- 

-
Shlomi Fish  [EMAIL PROTECTED]
Homepage:http://shlomif.il.eu.org/

Quidquid latine dictum sit, altum viditur.
[Whatever is said in Latin sounds profound.]

=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word unsubscribe in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]



Re: Perl question

2004-04-09 Thread Oron Peled
On Friday 09 April 2004 11:27, Muli Ben-Yehuda wrote:
  3. What the @$([EMAIL PROTECTED] is the difference between my and local? Which 
  one should I use here?
 
 http://prometheus.frii.com/~gnat/yapc/2000-stages/slide21.html

I didn't like so much the dumbed down type of explatanion in this
slide, so here is my take:

1. my does early binding between variable name and variable 
   storage (when the variable is defined) while local does late binding
   (when the variable is used).

2. This is very similar to the logical difference between hard link and
   symbolic link in the file system.

3. Technically, my variables are allocated on the call stack and thus
   behaves just like automatic variables in C. local variables are
   actually global variables which are looked up through the symbol table.
   What the local keyword does is temporarily save the global value and
   restore it automatically when we exit the scope.

4. Until perl-5 arrived (circa 94) only local existed. That's the
   historical reason for the non-intuitive name. During perl-6 development
   it was suggested to create a better term, something like tempval
   which describes more accurately its semantics (I didn't follow
   through to check if it was accepted after all).

-- 
Oron Peled Voice/Fax: +972-4-8228492
[EMAIL PROTECTED]  http://www.actcom.co.il/~oron

Beware of bugs in the above code;
 I have only proved it correct, not tried it.
  -- Donald E. Knuth


=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word unsubscribe in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]



Re: Perl question

2004-04-09 Thread Gabor Szabo
On Fri, 9 Apr 2004, Shachar Shemesh wrote:

 1. I know, I write perl like a C programmer, I can't help it. Feel free
 to show me how it's done.
Probably you should use File::Find
Try this code as a starter:

use File::Find;
$dirname = shift @ARGV;
find(\myfunc, $dirname);

sub myfunc {
printf %s  %s\n, $_, -d $_;
}

also look at
perldoc File::Find

 2. For some strange reason, the moment I recurse once, the entire loop
 structure exits. I suspect it's because the DIR handle is global. Will
 any perl guru comment on how it's supposed to be done?
wrong question :-)
TMTOWTDI


 3. What the @$([EMAIL PROTECTED] is the difference between my and local? Which
 one should I use here?

local is nearly obsolete, you should nearly always use my.
my- gives you lexical scoping (within the same {} block);
local - gives you a scoping within the current {} AND all the
functions called from within this {} block.

try reading
perldoc -f local
perldoc -f my


See also
  www.perl.org.il

Gabor

=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word unsubscribe in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]



Re: Perl question

2004-04-09 Thread Shachar Shemesh
Gabor Szabo wrote:

On Fri, 9 Apr 2004, Shachar Shemesh wrote:

 

1. I know, I write perl like a C programmer, I can't help it. Feel free
to show me how it's done.
   

Probably you should use File::Find
Try this code as a starter:
use File::Find;
$dirname = shift @ARGV;
find(\myfunc, $dirname);
 

Suppose I wanted to write this myself, how would I go about doing it? Is 
there any way of recursively using a dir or file handle name?

 Shachar

--
Shachar Shemesh
Lingnu OpenSource Consulting
http://www.lingnu.com/
=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word unsubscribe in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]


Re: Perl question

2004-04-09 Thread Shachar Shemesh
Ok, thanks everyone.

ONYAWIRNWSTG (Oh No, Yet Anohter, Was It Really Neccessary, WebSite from 
Template Generator) is now ready thanks to your help. Yes, it seems I 
too rewrote my own tool, despite having other tools to rely on. Nothing 
was an exact match to what I was looking for.

In case anyone is interested, my tool is attached to this email. I've 
had some hard time with the array of hash references thing. I kept 
reusing the same hash throughout the iterations. If any of the list's 
perl gurus want to have a look and remark on an easier way to do it, 
please let me know.

If anyone wants to use this tool - it takes three arguments. A template 
directory, a content directory, and the directory with the resulting 
site. The templates are in HTML::Template format - just RTFM that. The 
content begins with the template's name, and continues with three types 
of commands - s name=value, in which case it substitutes name for value 
as per HTML::Template man. s name terminator, in which case all the 
following lines are substituted for name, until a line carrying only the 
terminator, and m name loopterm wholeterm, which fills in values to 
loops. Each loop holds commands from the first type, terminated by 
loopterm. When the entire loop should be terminated, wholeterm is 
used. Example:

templatedir/template.tmpl:
headtitleTMPL_VAR name=title/title/head
bodyh1tmpl_var name=header/h1
Lines:br
tmpl_loop name=bodytmpl_var name=subject: tmpl_var name=objectbr
/tmpl_loop
/body
contentdir/index.html:
template.tmpl
s title=Page title
s header EOH
This is the page header
EOH
m body EOL EOAL
s subject=Behold
s object=Line 1
EOL
s subject=Line 2
s object END
This is the second linebr
It is two lines in one
END
EOL
EOAL
running mksite.pl templatedir contentdir website will create a file 
that looks like this:
website/index.html:
headtitlePage title/title/head
bodyh1This is the page header/h1
Lines:br
Behold: Line 1br
Line 2: This is the second linebr
It is two lines in onebr
/body

If this brief manpage proves useful, please drop me a note.

Shachar

--
Shachar Shemesh
Lingnu OpenSource Consulting
http://www.lingnu.com/
#!/usr/bin/perl

use HTML::Template;
use File::Find;

# Syntax - mksite.pl template_dir content_dir production_dir [clean]
# clean is not yet implemented.

$tmpldir=shift;
$contdir=shift;
$dstdir=shift;

$contdir=~'^(.*[^/])/*$' or die Error parsing content dir\n;
$contdir=$1./;

$tmpldir=~'^(.*[^/])/*$' or die Error parsing template dir\n;
$tmpldir=$1./;

$dstdir=~'^(.*[^/])/*$' or die Error parsing destination dir\n;
$dstdir=$1./;

find({ wanted = \process, follow_fast = 1, no_chdir= 1}, $contdir );

sub trim_prefix {
return substr shift, length( $contdir );
}
sub process {
# skip hidden files
$File::Find::name=~'/\.[^/]*$' and return;
$File::Find::name=~'/CVS(/[^/]*)?$' and return;

# Only files are interesting
if(-f $File::Find::name) { # A regular file
if( $File::Find::name=~/\.html?$/ ) {
	expand( trim_prefix($File::Find::name));
} else {
system cp $File::Find::name $dstdir.trim_prefix($File::Find::name);
}
} elsif( -d $File::Find::name ) { # a directory
	mkdir( $dstdir./.trim_prefix($File::Find::name) );
}
}

# Expands the template to a file with the corresponding name
sub expand {
my $filename=shift;
open (CONTENT_FILE, , $contdir.$filename) or
	die Error opening content file $filename: $!\n;

open (RES_FILE, , $dstdir.$filename) or
	die Error opening result file $filename: $!\n;

my $line=readline CONTENT_FILE;
$line or die Content file $filename empty\n;
chomp $line;
my $template = HTML::Template-new(filename = $tmpldir$line);
my $multi_delimit, $multi_delim2;
my @multi_params=();
my $multi_name;
my $currrow;

while( CONTENT_FILE ) {
	chomp;
	# Find out what form the command takes
	if( m/^s\s+(\w+)\s*=\s*(.*)\s*$/ ) {
	# A single line single var
	if( $multi_name ) {
		$currrow-{$1}=$2;
	} else {
		$template-param( $1 = $2 );
	}
	} elsif( m/^s\s+(\w+)\s+(\w+)\s*$/ ) {
	# A multi line single var
	my ($varname,$delimit)=($1,$2);
	my $data;
	while( $line=CONTENT_FILE and $line ne $delimit.\n ) {
		chomp $line;
		$data and $data.=\n;
		$data.=$line;
	}
	if( $multi_name ) {
		$currrow-{$varname}=$data;
	} else {
		$template-param( $varname = $data );
	}
	} elsif( !$multi_delimit  m/^m\s+(\w+)\s+(\w+)\s+(\w+)\s*$/ ) {
$currrow={};
	($multi_name, $multi_delimit, $multi_delim2)=($1,$2, $3);
	} elsif( $multi_name  $_ eq $multi_delimit ) {
	push ( @multi_params, {%$currrow});
	} elsif( $multi_name  $_ eq $multi_delim2 ) {
	$template-param( $multi_name = [EMAIL PROTECTED] );
	$multi_name=undef;
	%multi_params=undef;
	$multi_delimit=undef;
	} else {
	print nothing matched\n;
	}
}

print RES_FILE $template-output;

close RES_FILE;
close CONTENT_FILE;
}


Re: perl question

2002-10-08 Thread Tzafrir Cohen

On Sun, 6 Oct 2002, Arie Folger wrote:

 This is useful for creating localizable error messages that contain runtime
 information.

On C (and actually, on PHP) gettext is used for this task. Isn't there a
ready gettext (or equivalent) module on perl?

Perl actually has quite a few language-dependent texts. Maybe begin with
'perdoc perllocale'

-- 
Tzafrir Cohen
mailto:[EMAIL PROTECTED]
http://www.technion.ac.il/~tzafrir



=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word unsubscribe in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]




Re: perl question

2002-10-07 Thread herouth

Quoting guy keren [EMAIL PROTECTED]:


 you can also use 'eval' - if the text contains valid perl code.

Even if it doesn't. You can get the text into a string $string1, then do
something like:

$string2 = \$string3 = \$string1\;
eval($string2)

and the result will be in $string3. Escape any quotes inside the original text
($string1).

Herouth

=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word unsubscribe in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]




Re: perl question

2002-10-07 Thread Shlomi Fish

On Mon, 7 Oct 2002 [EMAIL PROTECTED] wrote:

 Quoting guy keren [EMAIL PROTECTED]:


  you can also use 'eval' - if the text contains valid perl code.

 Even if it doesn't. You can get the text into a string $string1, then do
 something like:

 $string2 = \$string3 = \$string1\;
 eval($string2)

 and the result will be in $string3. Escape any quotes inside the original text
 ($string1).


eval is very dangerous as it can execute arbitrary Perl code found in the
expression. I'd recommend against using it.

Regards,

Shlomi Fish

 Herouth

 =
 To unsubscribe, send mail to [EMAIL PROTECTED] with
 the word unsubscribe in the message body, e.g., run the command
 echo unsubscribe | mail [EMAIL PROTECTED]




--
Shlomi Fish[EMAIL PROTECTED]
Home Page: http://t2.technion.ac.il/~shlomif/
Home E-mail:   [EMAIL PROTECTED]

Let's suppose you have a table with 2^n cups...
Wait a second - is n a natural number?


=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word unsubscribe in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]