Re: __DATA__ and scalars

2003-09-24 Thread Martin Bower
thanks all,  some good responses.

I loaded my hash as normal,  then ran Philips suggestion at the end.nice 
:-)

eval "\$sql{$_} = <

_
E-Mails sind Ihnen nicht schnell genug? http://messenger.msn.de MSN 
Messenger - Kommunikation in Echtzeit




Re: __DATA__ and scalars

2003-09-24 Thread Rafael Garcia-Suarez
Philip Newton wrote:
> I think Brian McCauley(sp?) has a solution to this (that he wanted to 
> get into the Perl FAQ) using here-docs. Something like this:
> 
> my $data = join '', ;
> eval "\$data = <;

(I tend to think that this is an argument in favor
of the syntax "do FILEHANDLE")



Re: __DATA__ and scalars

2003-09-24 Thread Steve Purkis
On Wednesday, September 24, 2003, at 03:50  pm, Simon Wistow wrote:

On Wed, Sep 24, 2003 at 03:20:46PM +0100, Dave Hinton said:
Doesn't work when $data contains any double quotes.
True. Hence the Icky.
eval quotemeta( $data ); # might work?

Still, Luis' suggestion to use placeholders seems most practical to me.

-Steve




Re: __DATA__ and scalars

2003-09-24 Thread Roger Burton West
On Wed, Sep 24, 2003 at 02:41:15PM +, Martin Bower wrote:
>Roger..I'd like to keep the SQL in the same script, so how would the 
>templating systems help ? don't they tend to use external templates ?  
>(scuse my ignorance if not)

Other people can tell you about other templating systems. HTML::Template
can quite happily read from a __DATA__ section:

my $t=HTML::Template->new(arrayref => []);

Roger



Re: __DATA__ and scalars

2003-09-24 Thread Tim Sweetman
Martin Bower wrote:
The parser module works fine,  but the final string contains
WHERE date = "$today_date"
instead of
WHERE date = "2003-09-22"
any ideas ?
Use placeholders? See the bind_param part of the DBI doc. (That way you 
don't need to substitute in the variable, but pass it to DBI when you 
execute & it'll be dropped in).

Cheers

ti




Re: __DATA__ and scalars

2003-09-24 Thread Simon Wistow
On Wed, Sep 24, 2003 at 03:50:24PM +0100, Simon Wistow said:
> This sort of works but can't evaluate variables from other packages.

Which is easily fixed.


package Bar;
use vars qw($somevar);
$somevar = 'stuff with "quotes"';


package Bar::Quux;
use vars qw($someothervar);
$someothervar = 'nooch';


package main;
my $foo= 'blah';
my $RARRR  = "fleeg with\nlinebreak";
print evalqq(join "",);



sub evalqq {
my $string = $_[0] || return undef;
$string  =~ s{(\\*)(\$[a-z]+(::[a-z]+)*)}
 { "\\" x (length($1) / 2) . (!(length($1) % 2) &&
 do { no strict 'vars'; eval $2 } || $2) }ieg;
return $string;
}

__DATA__
$Bar::somevar
$Bar::Quux::someothervar
Yo! $foo
$RARRR
$quirka
Blah
'rar'
"foobbb"
\$foo
\\$foo





Re: __DATA__ and scalars

2003-09-24 Thread Martin Bower
thanks for responses

Roger..I'd like to keep the SQL in the same script, so how would the 
templating systems help ? don't they tend to use external templates ?  
(scuse my ignorance if not)

whats the form if I post this on perlmonks as well ?  is it considered 
double posting ?

_
Use MSN Messenger to send music and pics to your friends 
http://www.msn.co.uk/messenger




Re: __DATA__ and scalars

2003-09-24 Thread Simon Wistow
On Wed, Sep 24, 2003 at 03:20:46PM +0100, Dave Hinton said:
> Doesn't work when $data contains any double quotes.

True. Hence the Icky.

 
> Ideally, perl would have an evalqq function to do this sort of thing.


This sort of works but can't evaluate variables from other packages.

-- script --

print evalqq(join "",);
sub evalqq {
my $string = $_[0] || return undef;
$string=~ s{(\\*)(\$[a-z]+)\W}
   { "\\" x (length($1) / 2) . (!(length($1) % 2) &&
 do { no strict 'vars'; eval $2 } || $2) }eg;

return $string;
}





__DATA__
Yo! $foo
Blah
'rar'
"foobbb"
\$foo
\\$foo



-- end script --


-- 
the illusion of knowledge without any of the difficult bits  



Re: __DATA__ and scalars

2003-09-24 Thread Philip Newton
On 24 Sep 2003 at 15:20, Dave Hinton wrote:

> On Wednesday, September 24, 2003, at 03:04 pm, Simon Wistow wrote:
> 
> > you could do something like
> >
> > my $data = join "", ;
> > eval "\$date = \"$data\";";
> >
> > which is icky but works
> 
> Doesn't work when $data contains any double quotes.

I think Brian McCauley(sp?) has a solution to this (that he wanted to 
get into the Perl FAQ) using here-docs. Something like this:

my $data = join '', ;
eval "\$data = <




Re: __DATA__ and scalars

2003-09-24 Thread Dave Hinton
On Wednesday, September 24, 2003, at 03:04 pm, Simon Wistow wrote:

On Wed, Sep 24, 2003 at 12:32:24PM +, Martin Bower said:
Can anyone point me inthe right direction, on how to get variable 
names
stored in __DATA__ to be evaluated ?
you could do something like

my $data = join "", ;
eval "\$date = \"$data\";";
which is icky but works
Doesn't work when $data contains any double quotes.

Ideally, perl would have an evalqq function to do this sort of thing.




Re: __DATA__ and scalars

2003-09-24 Thread Simon Wistow
On Wed, Sep 24, 2003 at 12:32:24PM +, Martin Bower said:
> Can anyone point me inthe right direction, on how to get variable names 
> stored in __DATA__ to be evaluated ?

you could do something like

my $data = join "", ;
eval "\$date = \"$data\";";

which is icky but works



Re: __DATA__ and scalars

2003-09-24 Thread Roger Burton West
On Wed, Sep 24, 2003 at 12:32:24PM +, Martin Bower wrote:
>Can anyone point me inthe right direction, on how to get variable names 
>stored in __DATA__ to be evaluated ?

No, but have you considered using a lightweight templating system
instead?

Roger



Re: __DATA__ and scalars

2003-09-24 Thread Luis Campos de Carvalho
Martin Bower wrote:
Can anyone point me inthe right direction, on how to get variable names 
stored in __DATA__ to be evaluated ?

I have some largish scripts with embedded SQL statements which are 
ugly.  My idea is to store them at the end of the program under 
__DATA__,  parse this,  and then refer to them in my code.
The parser module works fine,  but the final string contains
WHERE date = "$today_date"
instead of
WHERE date = "2003-09-22"

any ideas ?
  Hello, Martin.
  Use placeholders, if your database support it.
  Write
> __DATA__
> *load_this
>SELECT this,that
>FROM my_table
>WHERE date = ?
  In place of

> __DATA__
> *load_this
>SELECT this,that
>FROM my_table
>WHERE date = "$today_date"
  And read `perldoc DBI `, to discover how to pass values to 
placeholders when querying the database.

  Good luck.
  Regards.
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  Luis Campos de Carvalho is Computer Scientist,
  PerlMonk [SiteDocClan], Cascavel-pm Moderator,
  Unix Sys Admin && Certified Oracle DBA
  http://br.geocities.com/monsieur_champs/
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=



__DATA__ and scalars

2003-09-24 Thread Martin Bower
Can anyone point me inthe right direction, on how to get variable names 
stored in __DATA__ to be evaluated ?

I have some largish scripts with embedded SQL statements which are ugly.  My 
idea is to store them at the end of the program under __DATA__,  parse this, 
 and then refer to them in my code.
The parser module works fine,  but the final string contains
WHERE date = "$today_date"
instead of
WHERE date = "2003-09-22"

any ideas ?

Martin

#!/usr/local/bin/perl
use strict;
use warnings;
use some::Module
my $today_date = '2003-09-22';
my %sql_code = parse_sqldata(\*DATA);
foreach my $sql_line (keys %sql_code) {
   print "SQL name => $sql_line\n";
   print "$sql_code{$sql_line}\n";
}
__DATA__
*load_this
   SELECT this,that
   FROM my_table
   WHERE date = "$today_date"
*load_that
   SELECT this,that
   FROM other_my_table
   WHERE date = "$today_date"


some_module contains
sub parse_sqldata {
   my ($data_ref) = @_;
   my (%sql, $found, $sql_code);
   while(<$data_ref>) {
   chomp();
   next if (/^#/);
   if (/^\*(\w+)$/) {
   if (defined $found) {
   $sql_code =~ s/\n$//;   #remove last 
EOL character
   $sql_code =~ s/\t/ /g;  #replace 
tabs with spaces
   $sql{$found} = $sql_code;
   $found = $sql_code = '';
   }
   $found = $1;
   } else {
   $sql_code .= "$_\n";
   }
   }
   continue { # process the last sql statement after the read has 
finished
   $sql_code =~ s/\n$//;
   $sql_code =~ s/\t/ /g;
   $sql{$found} = $sql_code;
   }
   return(%sql);
}

_
Sign-up for a FREE BT Broadband connection today! 
http://www.msn.co.uk/specials/btbroadband